modules-spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. const assert = require('assert')
  2. const Module = require('module')
  3. const path = require('path')
  4. const {remote} = require('electron')
  5. const {BrowserWindow} = remote
  6. const {closeWindow} = require('./window-helpers')
  7. const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
  8. describe('modules support', () => {
  9. const fixtures = path.join(__dirname, 'fixtures')
  10. describe('third-party module', () => {
  11. describe('runas', () => {
  12. if (!nativeModulesEnabled) return
  13. it('can be required in renderer', () => {
  14. require('runas')
  15. })
  16. it('can be required in node binary', (done) => {
  17. const runas = path.join(fixtures, 'module', 'runas.js')
  18. const child = require('child_process').fork(runas)
  19. child.on('message', (msg) => {
  20. assert.equal(msg, 'ok')
  21. done()
  22. })
  23. })
  24. })
  25. // TODO(alexeykuzmin): Disabled during the Chromium 62 (Node.js 9) upgrade.
  26. // Enable it back when "ffi" module supports Node.js 9.
  27. // https://github.com/electron/electron/issues/11274
  28. xdescribe('ffi', () => {
  29. before(function () {
  30. if (!nativeModulesEnabled || process.platform === 'win32' ||
  31. process.arch === 'arm64') {
  32. this.skip()
  33. }
  34. })
  35. it('does not crash', () => {
  36. const ffi = require('ffi')
  37. const libm = ffi.Library('libm', {
  38. ceil: ['double', ['double']]
  39. })
  40. assert.equal(libm.ceil(1.5), 2)
  41. })
  42. })
  43. describe('q', () => {
  44. const Q = require('q')
  45. describe('Q.when', () => {
  46. it('emits the fullfil callback', (done) => {
  47. Q(true).then((val) => {
  48. assert.equal(val, true)
  49. done()
  50. })
  51. })
  52. })
  53. })
  54. describe('coffee-script', () => {
  55. it('can be registered and used to require .coffee files', () => {
  56. assert.doesNotThrow(() => {
  57. require('coffee-script').register()
  58. })
  59. assert.strictEqual(require('./fixtures/module/test.coffee'), true)
  60. })
  61. })
  62. })
  63. describe('global variables', () => {
  64. describe('process', () => {
  65. it('can be declared in a module', () => {
  66. assert.strictEqual(require('./fixtures/module/declare-process'), 'declared process')
  67. })
  68. })
  69. describe('global', () => {
  70. it('can be declared in a module', () => {
  71. assert.strictEqual(require('./fixtures/module/declare-global'), 'declared global')
  72. })
  73. })
  74. describe('Buffer', () => {
  75. it('can be declared in a module', () => {
  76. assert.strictEqual(require('./fixtures/module/declare-buffer'), 'declared Buffer')
  77. })
  78. })
  79. })
  80. describe('Module._nodeModulePaths', () => {
  81. describe('when the path is inside the resources path', () => {
  82. it('does not include paths outside of the resources path', () => {
  83. let modulePath = process.resourcesPath
  84. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  85. path.join(process.resourcesPath, 'node_modules')
  86. ])
  87. modulePath = process.resourcesPath + '-foo'
  88. const nodeModulePaths = Module._nodeModulePaths(modulePath)
  89. assert(nodeModulePaths.includes(path.join(modulePath, 'node_modules')))
  90. assert(nodeModulePaths.includes(path.join(modulePath, '..', 'node_modules')))
  91. modulePath = path.join(process.resourcesPath, 'foo')
  92. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  93. path.join(process.resourcesPath, 'foo', 'node_modules'),
  94. path.join(process.resourcesPath, 'node_modules')
  95. ])
  96. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo')
  97. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  98. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  99. path.join(process.resourcesPath, 'node_modules')
  100. ])
  101. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar')
  102. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  103. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  104. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  105. path.join(process.resourcesPath, 'node_modules')
  106. ])
  107. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar')
  108. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  109. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  110. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  111. path.join(process.resourcesPath, 'node_modules')
  112. ])
  113. })
  114. })
  115. describe('when the path is outside the resources path', () => {
  116. it('includes paths outside of the resources path', () => {
  117. let modulePath = path.resolve('/foo')
  118. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  119. path.join(modulePath, 'node_modules'),
  120. path.resolve('/node_modules')
  121. ])
  122. })
  123. })
  124. })
  125. describe('require', () => {
  126. describe('when loaded URL is not file: protocol', () => {
  127. let w
  128. beforeEach(() => {
  129. w = new BrowserWindow({show: false})
  130. })
  131. afterEach(async () => {
  132. await closeWindow(w)
  133. w = null
  134. })
  135. it('searches for module under app directory', async () => {
  136. w.loadURL('about:blank')
  137. const result = await w.webContents.executeJavaScript('typeof require("q").when')
  138. assert.equal(result, 'function')
  139. })
  140. })
  141. })
  142. })