modules-spec.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. describe('ffi', () => {
  26. before(function () {
  27. if (!nativeModulesEnabled || process.platform === 'win32' ||
  28. process.arch === 'arm64') {
  29. this.skip()
  30. }
  31. })
  32. it('does not crash', () => {
  33. const ffi = require('ffi')
  34. const libm = ffi.Library('libm', {
  35. ceil: ['double', ['double']]
  36. })
  37. assert.equal(libm.ceil(1.5), 2)
  38. })
  39. })
  40. describe('q', () => {
  41. const Q = require('q')
  42. describe('Q.when', () => {
  43. it('emits the fullfil callback', (done) => {
  44. Q(true).then((val) => {
  45. assert.equal(val, true)
  46. done()
  47. })
  48. })
  49. })
  50. })
  51. describe('coffee-script', () => {
  52. it('can be registered and used to require .coffee files', () => {
  53. assert.doesNotThrow(() => {
  54. require('coffee-script').register()
  55. })
  56. assert.strictEqual(require('./fixtures/module/test.coffee'), true)
  57. })
  58. })
  59. })
  60. describe('global variables', () => {
  61. describe('process', () => {
  62. it('can be declared in a module', () => {
  63. assert.strictEqual(require('./fixtures/module/declare-process'), 'declared process')
  64. })
  65. })
  66. describe('global', () => {
  67. it('can be declared in a module', () => {
  68. assert.strictEqual(require('./fixtures/module/declare-global'), 'declared global')
  69. })
  70. })
  71. describe('Buffer', () => {
  72. it('can be declared in a module', () => {
  73. assert.strictEqual(require('./fixtures/module/declare-buffer'), '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. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  82. path.join(process.resourcesPath, 'node_modules')
  83. ])
  84. modulePath = process.resourcesPath + '-foo'
  85. const nodeModulePaths = Module._nodeModulePaths(modulePath)
  86. assert(nodeModulePaths.includes(path.join(modulePath, 'node_modules')))
  87. assert(nodeModulePaths.includes(path.join(modulePath, '..', 'node_modules')))
  88. modulePath = path.join(process.resourcesPath, 'foo')
  89. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  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. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  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. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  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. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  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. let modulePath = path.resolve('/foo')
  115. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  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. let w
  125. beforeEach(() => {
  126. w = new BrowserWindow({show: false})
  127. })
  128. afterEach(async () => {
  129. await closeWindow(w)
  130. w = null
  131. })
  132. it('searches for module under app directory', async () => {
  133. w.loadURL('about:blank')
  134. const result = await w.webContents.executeJavaScript('typeof require("q").when')
  135. assert.equal(result, 'function')
  136. })
  137. })
  138. })
  139. })