modules-spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { expect } from 'chai'
  2. import * as path from 'path'
  3. import * as fs from 'fs'
  4. import { BrowserWindow } from 'electron'
  5. import { ifdescribe, ifit } from './spec-helpers'
  6. import { closeAllWindows } from './window-helpers'
  7. import * as childProcess from 'child_process'
  8. const Module = require('module')
  9. const features = process.electronBinding('features')
  10. const nativeModulesEnabled = !process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS
  11. describe('modules support', () => {
  12. const fixtures = path.join(__dirname, 'fixtures')
  13. describe('third-party module', () => {
  14. ifdescribe(nativeModulesEnabled)('echo', () => {
  15. afterEach(closeAllWindows)
  16. it('can be required in renderer', async () => {
  17. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
  18. w.loadURL('about:blank')
  19. await expect(w.webContents.executeJavaScript(`{ require('echo') }`)).to.be.fulfilled()
  20. })
  21. ifit(features.isRunAsNodeEnabled())('can be required in node binary', function (done) {
  22. const child = childProcess.fork(path.join(fixtures, 'module', 'echo.js'))
  23. child.on('message', (msg) => {
  24. expect(msg).to.equal('ok')
  25. done()
  26. })
  27. })
  28. ifit(process.platform === 'win32')('can be required if electron.exe is renamed', () => {
  29. const testExecPath = path.join(path.dirname(process.execPath), 'test.exe')
  30. fs.copyFileSync(process.execPath, testExecPath)
  31. try {
  32. const fixture = path.join(fixtures, 'module', 'echo-renamed.js')
  33. expect(fs.existsSync(fixture)).to.be.true()
  34. const child = childProcess.spawnSync(testExecPath, [fixture])
  35. expect(child.status).to.equal(0)
  36. } finally {
  37. fs.unlinkSync(testExecPath)
  38. }
  39. })
  40. })
  41. describe('q', () => {
  42. const Q = require('q')
  43. describe('Q.when', () => {
  44. it('emits the fullfil callback', (done) => {
  45. Q(true).then((val: boolean) => {
  46. expect(val).to.be.true()
  47. done()
  48. })
  49. })
  50. })
  51. })
  52. describe('coffeescript', () => {
  53. it('can be registered and used to require .coffee files', () => {
  54. expect(() => {
  55. require('coffeescript').register()
  56. }).to.not.throw()
  57. expect(require('./fixtures/module/test.coffee')).to.be.true()
  58. })
  59. })
  60. })
  61. describe('global variables', () => {
  62. describe('process', () => {
  63. it('can be declared in a module', () => {
  64. expect(require('./fixtures/module/declare-process')).to.equal('declared process')
  65. })
  66. })
  67. describe('global', () => {
  68. it('can be declared in a module', () => {
  69. expect(require('./fixtures/module/declare-global')).to.equal('declared global')
  70. })
  71. })
  72. describe('Buffer', () => {
  73. it('can be declared in a module', () => {
  74. expect(require('./fixtures/module/declare-buffer')).to.equal('declared Buffer')
  75. })
  76. })
  77. })
  78. describe('Module._nodeModulePaths', () => {
  79. describe('when the path is inside the resources path', () => {
  80. it('does not include paths outside of the resources path', () => {
  81. let modulePath = process.resourcesPath
  82. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  83. path.join(process.resourcesPath, 'node_modules')
  84. ])
  85. modulePath = process.resourcesPath + '-foo'
  86. const nodeModulePaths = Module._nodeModulePaths(modulePath)
  87. expect(nodeModulePaths).to.include(path.join(modulePath, 'node_modules'))
  88. expect(nodeModulePaths).to.include(path.join(modulePath, '..', 'node_modules'))
  89. modulePath = path.join(process.resourcesPath, 'foo')
  90. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  91. path.join(process.resourcesPath, 'foo', 'node_modules'),
  92. path.join(process.resourcesPath, 'node_modules')
  93. ])
  94. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo')
  95. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  96. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  97. path.join(process.resourcesPath, 'node_modules')
  98. ])
  99. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar')
  100. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  101. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  102. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  103. path.join(process.resourcesPath, 'node_modules')
  104. ])
  105. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar')
  106. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  107. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  108. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  109. path.join(process.resourcesPath, 'node_modules')
  110. ])
  111. })
  112. })
  113. describe('when the path is outside the resources path', () => {
  114. it('includes paths outside of the resources path', () => {
  115. const modulePath = path.resolve('/foo')
  116. expect(Module._nodeModulePaths(modulePath)).to.deep.equal([
  117. path.join(modulePath, 'node_modules'),
  118. path.resolve('/node_modules')
  119. ])
  120. })
  121. })
  122. })
  123. describe('require', () => {
  124. describe('when loaded URL is not file: protocol', () => {
  125. afterEach(closeAllWindows)
  126. it('searches for module under app directory', async () => {
  127. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } })
  128. w.loadURL('about:blank')
  129. const result = await w.webContents.executeJavaScript('typeof require("q").when')
  130. expect(result).to.equal('function')
  131. })
  132. })
  133. })
  134. })