modules-spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { expect } from 'chai';
  2. import * as path from 'path';
  3. import * as fs from 'fs';
  4. import { BrowserWindow } from 'electron/main';
  5. import { ifdescribe, ifit } from './lib/spec-helpers';
  6. import { closeAllWindows } from './lib/window-helpers';
  7. import * as childProcess from 'child_process';
  8. import { once } from 'events';
  9. const Module = require('module');
  10. const features = process._linkedBinding('electron_common_features');
  11. const nativeModulesEnabled = !process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS;
  12. describe('modules support', () => {
  13. const fixtures = path.join(__dirname, 'fixtures');
  14. describe('third-party module', () => {
  15. ifdescribe(nativeModulesEnabled)('echo', () => {
  16. afterEach(closeAllWindows);
  17. it('can be required in renderer', async () => {
  18. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  19. w.loadURL('about:blank');
  20. await expect(
  21. w.webContents.executeJavaScript(
  22. "{ require('@electron-ci/echo'); null }"
  23. )
  24. ).to.be.fulfilled();
  25. });
  26. ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
  27. const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js'));
  28. const [msg] = await once(child, 'message');
  29. expect(msg).to.equal('ok');
  30. });
  31. ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
  32. const testExecPath = path.join(path.dirname(process.execPath), 'test.exe');
  33. fs.copyFileSync(process.execPath, testExecPath);
  34. try {
  35. const fixture = path.join(fixtures, 'module', 'echo-renamed.js');
  36. expect(fs.existsSync(fixture)).to.be.true();
  37. const child = childProcess.spawnSync(testExecPath, [fixture]);
  38. expect(child.status).to.equal(0);
  39. } finally {
  40. fs.unlinkSync(testExecPath);
  41. }
  42. });
  43. });
  44. const enablePlatforms: NodeJS.Platform[] = [
  45. 'linux',
  46. 'darwin',
  47. 'win32'
  48. ];
  49. ifdescribe(nativeModulesEnabled && enablePlatforms.includes(process.platform))('module that use uv_dlopen', () => {
  50. it('can be required in renderer', async () => {
  51. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  52. w.loadURL('about:blank');
  53. await expect(w.webContents.executeJavaScript('{ require(\'@electron-ci/uv-dlopen\'); null }')).to.be.fulfilled();
  54. });
  55. ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
  56. const child = childProcess.fork(path.join(fixtures, 'module', 'uv-dlopen.js'));
  57. const [exitCode] = await once(child, 'exit');
  58. expect(exitCode).to.equal(0);
  59. });
  60. });
  61. describe('q', () => {
  62. describe('Q.when', () => {
  63. it('emits the fullfil callback', (done) => {
  64. const Q = require('q');
  65. Q(true).then((val: boolean) => {
  66. expect(val).to.be.true();
  67. done();
  68. });
  69. });
  70. });
  71. });
  72. describe('require(\'electron/...\')', () => {
  73. it('require(\'electron/lol\') should throw in the main process', () => {
  74. expect(() => {
  75. require('electron/lol');
  76. }).to.throw(/Cannot find module 'electron\/lol'/);
  77. });
  78. it('require(\'electron/lol\') should throw in the renderer process', async () => {
  79. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  80. w.loadURL('about:blank');
  81. await expect(w.webContents.executeJavaScript('{ require(\'electron/lol\'); null }')).to.eventually.be.rejected();
  82. });
  83. it('require(\'electron\') should not throw in the main process', () => {
  84. expect(() => {
  85. require('electron');
  86. }).to.not.throw();
  87. });
  88. it('require(\'electron\') should not throw in the renderer process', async () => {
  89. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  90. w.loadURL('about:blank');
  91. await expect(w.webContents.executeJavaScript('{ require(\'electron\'); null }')).to.be.fulfilled();
  92. });
  93. it('require(\'electron/main\') should not throw in the main process', () => {
  94. expect(() => {
  95. require('electron/main');
  96. }).to.not.throw();
  97. });
  98. it('require(\'electron/main\') should not throw in the renderer process', async () => {
  99. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  100. w.loadURL('about:blank');
  101. await expect(w.webContents.executeJavaScript('{ require(\'electron/main\'); null }')).to.be.fulfilled();
  102. });
  103. it('require(\'electron/renderer\') should not throw in the main process', () => {
  104. expect(() => {
  105. require('electron/renderer');
  106. }).to.not.throw();
  107. });
  108. it('require(\'electron/renderer\') should not throw in the renderer process', async () => {
  109. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  110. w.loadURL('about:blank');
  111. await expect(w.webContents.executeJavaScript('{ require(\'electron/renderer\'); null }')).to.be.fulfilled();
  112. });
  113. it('require(\'electron/common\') should not throw in the main process', () => {
  114. expect(() => {
  115. require('electron/common');
  116. }).to.not.throw();
  117. });
  118. it('require(\'electron/common\') should not throw in the renderer process', async () => {
  119. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  120. w.loadURL('about:blank');
  121. await expect(w.webContents.executeJavaScript('{ require(\'electron/common\'); null }')).to.be.fulfilled();
  122. });
  123. });
  124. describe('coffeescript', () => {
  125. it('can be registered and used to require .coffee files', () => {
  126. expect(() => {
  127. require('coffeescript').register();
  128. }).to.not.throw();
  129. expect(require('./fixtures/module/test.coffee')).to.be.true();
  130. });
  131. });
  132. });
  133. describe('global variables', () => {
  134. describe('process', () => {
  135. it('can be declared in a module', () => {
  136. expect(require('./fixtures/module/declare-process')).to.equal('declared process');
  137. });
  138. });
  139. describe('global', () => {
  140. it('can be declared in a module', () => {
  141. expect(require('./fixtures/module/declare-global')).to.equal('declared global');
  142. });
  143. });
  144. describe('Buffer', () => {
  145. it('can be declared in a module', () => {
  146. expect(require('./fixtures/module/declare-buffer')).to.equal('declared Buffer');
  147. });
  148. });
  149. });
  150. describe('Module._nodeModulePaths', () => {
  151. describe('when the path is inside the resources path', () => {
  152. it('does not include paths outside of the resources path', () => {
  153. let modulePath = process.resourcesPath;
  154. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  155. path.join(process.resourcesPath, 'node_modules')
  156. ]);
  157. modulePath = process.resourcesPath + '-foo';
  158. const nodeModulePaths = Module._nodeModulePaths(modulePath);
  159. expect(nodeModulePaths).to.include(path.join(modulePath, 'node_modules'));
  160. expect(nodeModulePaths).to.include(path.join(modulePath, '..', 'node_modules'));
  161. modulePath = path.join(process.resourcesPath, 'foo');
  162. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  163. path.join(process.resourcesPath, 'foo', 'node_modules'),
  164. path.join(process.resourcesPath, 'node_modules')
  165. ]);
  166. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo');
  167. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  168. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  169. path.join(process.resourcesPath, 'node_modules')
  170. ]);
  171. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar');
  172. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  173. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  174. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  175. path.join(process.resourcesPath, 'node_modules')
  176. ]);
  177. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar');
  178. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  179. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  180. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  181. path.join(process.resourcesPath, 'node_modules')
  182. ]);
  183. });
  184. });
  185. describe('when the path is outside the resources path', () => {
  186. it('includes paths outside of the resources path', () => {
  187. const modulePath = path.resolve('/foo');
  188. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  189. path.join(modulePath, 'node_modules'),
  190. path.resolve('/node_modules')
  191. ]);
  192. });
  193. });
  194. });
  195. describe('require', () => {
  196. describe('when loaded URL is not file: protocol', () => {
  197. afterEach(closeAllWindows);
  198. it('searches for module under app directory', async () => {
  199. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  200. w.loadURL('about:blank');
  201. const result = await w.webContents.executeJavaScript('typeof require("q").when');
  202. expect(result).to.equal('function');
  203. });
  204. });
  205. });
  206. describe('esm', () => {
  207. it('can load the built-in "electron" module via ESM import', async () => {
  208. await expect(import('electron')).to.eventually.be.ok();
  209. });
  210. it('the built-in "electron" module loaded via ESM import has the same exports as the CJS module', async () => {
  211. const esmElectron = await import('electron');
  212. const cjsElectron = require('electron');
  213. expect(Object.keys(esmElectron)).to.deep.equal(Object.keys(cjsElectron));
  214. });
  215. });
  216. });