modules-spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 './spec-helpers';
  6. import { closeAllWindows } from './window-helpers';
  7. import { emittedOnce } from './events-helpers';
  8. import * as childProcess from 'child_process';
  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 emittedOnce(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. await new Promise<void>(resolve => child.once('exit', (exitCode) => {
  58. expect(exitCode).to.equal(0);
  59. resolve();
  60. }));
  61. });
  62. });
  63. describe('q', () => {
  64. describe('Q.when', () => {
  65. it('emits the fullfil callback', (done) => {
  66. const Q = require('q');
  67. Q(true).then((val: boolean) => {
  68. expect(val).to.be.true();
  69. done();
  70. });
  71. });
  72. });
  73. });
  74. describe('coffeescript', () => {
  75. it('can be registered and used to require .coffee files', () => {
  76. expect(() => {
  77. require('coffeescript').register();
  78. }).to.not.throw();
  79. expect(require('./fixtures/module/test.coffee')).to.be.true();
  80. });
  81. });
  82. });
  83. describe('global variables', () => {
  84. describe('process', () => {
  85. it('can be declared in a module', () => {
  86. expect(require('./fixtures/module/declare-process')).to.equal('declared process');
  87. });
  88. });
  89. describe('global', () => {
  90. it('can be declared in a module', () => {
  91. expect(require('./fixtures/module/declare-global')).to.equal('declared global');
  92. });
  93. });
  94. describe('Buffer', () => {
  95. it('can be declared in a module', () => {
  96. expect(require('./fixtures/module/declare-buffer')).to.equal('declared Buffer');
  97. });
  98. });
  99. });
  100. describe('Module._nodeModulePaths', () => {
  101. // Work around the hack in spec/global-paths.
  102. beforeEach(() => {
  103. Module.ignoreGlobalPathsHack = true;
  104. });
  105. afterEach(() => {
  106. Module.ignoreGlobalPathsHack = false;
  107. });
  108. describe('when the path is inside the resources path', () => {
  109. it('does not include paths outside of the resources path', () => {
  110. let modulePath = process.resourcesPath;
  111. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  112. path.join(process.resourcesPath, 'node_modules')
  113. ]);
  114. modulePath = process.resourcesPath + '-foo';
  115. const nodeModulePaths = Module._nodeModulePaths(modulePath);
  116. expect(nodeModulePaths).to.include(path.join(modulePath, 'node_modules'));
  117. expect(nodeModulePaths).to.include(path.join(modulePath, '..', 'node_modules'));
  118. modulePath = path.join(process.resourcesPath, 'foo');
  119. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  120. path.join(process.resourcesPath, 'foo', 'node_modules'),
  121. path.join(process.resourcesPath, 'node_modules')
  122. ]);
  123. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo');
  124. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  125. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  126. path.join(process.resourcesPath, 'node_modules')
  127. ]);
  128. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar');
  129. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  130. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  131. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  132. path.join(process.resourcesPath, 'node_modules')
  133. ]);
  134. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar');
  135. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  136. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  137. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  138. path.join(process.resourcesPath, 'node_modules')
  139. ]);
  140. });
  141. });
  142. describe('when the path is outside the resources path', () => {
  143. it('includes paths outside of the resources path', () => {
  144. const modulePath = path.resolve('/foo');
  145. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  146. path.join(modulePath, 'node_modules'),
  147. path.resolve('/node_modules')
  148. ]);
  149. });
  150. });
  151. });
  152. describe('require', () => {
  153. describe('when loaded URL is not file: protocol', () => {
  154. afterEach(closeAllWindows);
  155. it('searches for module under app directory', async () => {
  156. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } });
  157. w.loadURL('about:blank');
  158. const result = await w.webContents.executeJavaScript('typeof require("q").when');
  159. expect(result).to.equal('function');
  160. });
  161. });
  162. });
  163. });