modules-spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 } });
  19. w.loadURL('about:blank');
  20. await expect(w.webContents.executeJavaScript('{ require(\'echo\'); null }')).to.be.fulfilled();
  21. });
  22. ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
  23. const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js'));
  24. const [msg] = await emittedOnce(child, 'message');
  25. expect(msg).to.equal('ok');
  26. });
  27. ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
  28. const testExecPath = path.join(path.dirname(process.execPath), 'test.exe');
  29. fs.copyFileSync(process.execPath, testExecPath);
  30. try {
  31. const fixture = path.join(fixtures, 'module', 'echo-renamed.js');
  32. expect(fs.existsSync(fixture)).to.be.true();
  33. const child = childProcess.spawnSync(testExecPath, [fixture]);
  34. expect(child.status).to.equal(0);
  35. } finally {
  36. fs.unlinkSync(testExecPath);
  37. }
  38. });
  39. });
  40. const enablePlatforms: NodeJS.Platform[] = [
  41. 'linux',
  42. 'darwin',
  43. 'win32'
  44. ];
  45. ifdescribe(nativeModulesEnabled && enablePlatforms.includes(process.platform))('module that use uv_dlopen', () => {
  46. it('can be required in renderer', async () => {
  47. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
  48. w.loadURL('about:blank');
  49. await expect(w.webContents.executeJavaScript('{ require(\'uv-dlopen\'); null }')).to.be.fulfilled();
  50. });
  51. ifit(features.isRunAsNodeEnabled())('can be required in node binary', async function () {
  52. const child = childProcess.fork(path.join(fixtures, 'module', 'uv-dlopen.js'));
  53. await new Promise(resolve => child.once('exit', (exitCode) => {
  54. expect(exitCode).to.equal(0);
  55. resolve();
  56. }));
  57. });
  58. });
  59. describe('q', () => {
  60. describe('Q.when', () => {
  61. it('emits the fullfil callback', (done) => {
  62. const Q = require('q');
  63. Q(true).then((val: boolean) => {
  64. expect(val).to.be.true();
  65. done();
  66. });
  67. });
  68. });
  69. });
  70. describe('coffeescript', () => {
  71. it('can be registered and used to require .coffee files', () => {
  72. expect(() => {
  73. require('coffeescript').register();
  74. }).to.not.throw();
  75. expect(require('./fixtures/module/test.coffee')).to.be.true();
  76. });
  77. });
  78. });
  79. describe('global variables', () => {
  80. describe('process', () => {
  81. it('can be declared in a module', () => {
  82. expect(require('./fixtures/module/declare-process')).to.equal('declared process');
  83. });
  84. });
  85. describe('global', () => {
  86. it('can be declared in a module', () => {
  87. expect(require('./fixtures/module/declare-global')).to.equal('declared global');
  88. });
  89. });
  90. describe('Buffer', () => {
  91. it('can be declared in a module', () => {
  92. expect(require('./fixtures/module/declare-buffer')).to.equal('declared Buffer');
  93. });
  94. });
  95. });
  96. describe('Module._nodeModulePaths', () => {
  97. describe('when the path is inside the resources path', () => {
  98. it('does not include paths outside of the resources path', () => {
  99. let modulePath = process.resourcesPath;
  100. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  101. path.join(process.resourcesPath, 'node_modules')
  102. ]);
  103. modulePath = process.resourcesPath + '-foo';
  104. const nodeModulePaths = Module._nodeModulePaths(modulePath);
  105. expect(nodeModulePaths).to.include(path.join(modulePath, 'node_modules'));
  106. expect(nodeModulePaths).to.include(path.join(modulePath, '..', 'node_modules'));
  107. modulePath = path.join(process.resourcesPath, 'foo');
  108. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  109. path.join(process.resourcesPath, 'foo', 'node_modules'),
  110. path.join(process.resourcesPath, 'node_modules')
  111. ]);
  112. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo');
  113. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  114. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  115. path.join(process.resourcesPath, 'node_modules')
  116. ]);
  117. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar');
  118. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  119. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  120. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  121. path.join(process.resourcesPath, 'node_modules')
  122. ]);
  123. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar');
  124. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  125. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  126. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  127. path.join(process.resourcesPath, 'node_modules')
  128. ]);
  129. });
  130. });
  131. describe('when the path is outside the resources path', () => {
  132. it('includes paths outside of the resources path', () => {
  133. const modulePath = path.resolve('/foo');
  134. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  135. path.join(modulePath, 'node_modules'),
  136. path.resolve('/node_modules')
  137. ]);
  138. });
  139. });
  140. });
  141. describe('require', () => {
  142. describe('when loaded URL is not file: protocol', () => {
  143. afterEach(closeAllWindows);
  144. it('searches for module under app directory', async () => {
  145. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
  146. w.loadURL('about:blank');
  147. const result = await w.webContents.executeJavaScript('typeof require("q").when');
  148. expect(result).to.equal('function');
  149. });
  150. });
  151. });
  152. });