modules-spec.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.electronBinding('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. describe('q', () => {
  41. describe('Q.when', () => {
  42. it('emits the fullfil callback', (done) => {
  43. const Q = require('q');
  44. Q(true).then((val: boolean) => {
  45. expect(val).to.be.true();
  46. done();
  47. });
  48. });
  49. });
  50. });
  51. describe('coffeescript', () => {
  52. it('can be registered and used to require .coffee files', () => {
  53. expect(() => {
  54. require('coffeescript').register();
  55. }).to.not.throw();
  56. expect(require('./fixtures/module/test.coffee')).to.be.true();
  57. });
  58. });
  59. });
  60. describe('global variables', () => {
  61. describe('process', () => {
  62. it('can be declared in a module', () => {
  63. expect(require('./fixtures/module/declare-process')).to.equal('declared process');
  64. });
  65. });
  66. describe('global', () => {
  67. it('can be declared in a module', () => {
  68. expect(require('./fixtures/module/declare-global')).to.equal('declared global');
  69. });
  70. });
  71. describe('Buffer', () => {
  72. it('can be declared in a module', () => {
  73. expect(require('./fixtures/module/declare-buffer')).to.equal('declared Buffer');
  74. });
  75. });
  76. });
  77. describe('Module._nodeModulePaths', () => {
  78. describe('when the path is inside the resources path', () => {
  79. it('does not include paths outside of the resources path', () => {
  80. let modulePath = process.resourcesPath;
  81. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  82. path.join(process.resourcesPath, 'node_modules')
  83. ]);
  84. modulePath = process.resourcesPath + '-foo';
  85. const nodeModulePaths = Module._nodeModulePaths(modulePath);
  86. expect(nodeModulePaths).to.include(path.join(modulePath, 'node_modules'));
  87. expect(nodeModulePaths).to.include(path.join(modulePath, '..', 'node_modules'));
  88. modulePath = path.join(process.resourcesPath, 'foo');
  89. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  90. path.join(process.resourcesPath, 'foo', 'node_modules'),
  91. path.join(process.resourcesPath, 'node_modules')
  92. ]);
  93. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo');
  94. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  95. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  96. path.join(process.resourcesPath, 'node_modules')
  97. ]);
  98. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar');
  99. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  100. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  101. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  102. path.join(process.resourcesPath, 'node_modules')
  103. ]);
  104. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar');
  105. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  106. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  107. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  108. path.join(process.resourcesPath, 'node_modules')
  109. ]);
  110. });
  111. });
  112. describe('when the path is outside the resources path', () => {
  113. it('includes paths outside of the resources path', () => {
  114. const modulePath = path.resolve('/foo');
  115. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  116. path.join(modulePath, 'node_modules'),
  117. path.resolve('/node_modules')
  118. ]);
  119. });
  120. });
  121. });
  122. describe('require', () => {
  123. describe('when loaded URL is not file: protocol', () => {
  124. afterEach(closeAllWindows);
  125. it('searches for module under app directory', async () => {
  126. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
  127. w.loadURL('about:blank');
  128. const result = await w.webContents.executeJavaScript('typeof require("q").when');
  129. expect(result).to.equal('function');
  130. });
  131. });
  132. });
  133. });