index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. const Module = require('module')
  2. const path = require('path')
  3. const v8 = require('v8')
  4. Module.globalPaths.push(path.resolve(__dirname, '../spec/node_modules'))
  5. // We want to terminate on errors, not throw up a dialog
  6. process.on('uncaughtException', (err) => {
  7. console.error('Unhandled exception in main spec runner:', err)
  8. process.exit(1)
  9. })
  10. // Tell ts-node which tsconfig to use
  11. process.env.TS_NODE_PROJECT = path.resolve(__dirname, '../tsconfig.spec.json')
  12. process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
  13. const { app, protocol } = require('electron')
  14. v8.setFlagsFromString('--expose_gc')
  15. app.commandLine.appendSwitch('js-flags', '--expose_gc')
  16. // Prevent the spec runner quiting when the first window closes
  17. app.on('window-all-closed', () => null)
  18. // TODO: This API should _probably_ only be enabled for the specific test that needs it
  19. // not the entire test suite
  20. app.commandLine.appendSwitch('ignore-certificate-errors')
  21. global.standardScheme = 'app'
  22. protocol.registerSchemesAsPrivileged([
  23. { scheme: global.standardScheme, privileges: { standard: true, secure: true } },
  24. { scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
  25. { scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
  26. { scheme: 'no-cors', privileges: { supportFetchAPI: true } },
  27. { scheme: 'no-fetch', privileges: { corsEnabled: true } }
  28. ])
  29. app.whenReady().then(() => {
  30. require('ts-node/register')
  31. const argv = require('yargs')
  32. .boolean('ci')
  33. .string('g').alias('g', 'grep')
  34. .boolean('i').alias('i', 'invert')
  35. .argv
  36. const isCi = !!argv.ci
  37. global.isCI = isCi
  38. const Mocha = require('mocha')
  39. const mochaOptions = {}
  40. if (process.env.MOCHA_REPORTER) {
  41. mochaOptions.reporter = process.env.MOCHA_REPORTER
  42. }
  43. if (process.env.MOCHA_MULTI_REPORTERS) {
  44. mochaOptions.reporterOptions = {
  45. reporterEnabled: process.env.MOCHA_MULTI_REPORTERS
  46. }
  47. }
  48. const mocha = new Mocha(mochaOptions)
  49. if (!process.env.MOCHA_REPORTER) {
  50. mocha.ui('bdd').reporter('tap')
  51. }
  52. mocha.timeout(isCi ? 30000 : 10000)
  53. if (argv.grep) mocha.grep(argv.grep)
  54. if (argv.invert) mocha.invert()
  55. // Read all test files.
  56. const walker = require('walkdir').walk(__dirname, {
  57. no_recurse: true
  58. })
  59. // This allows you to run specific modules only:
  60. // npm run test -match=menu
  61. const moduleMatch = process.env.npm_config_match
  62. ? new RegExp(process.env.npm_config_match, 'g')
  63. : null
  64. const testFiles = []
  65. walker.on('file', (file) => {
  66. if (/-spec\.[tj]s$/.test(file) &&
  67. (!moduleMatch || moduleMatch.test(file))) {
  68. testFiles.push(file)
  69. }
  70. })
  71. walker.on('end', () => {
  72. testFiles.sort()
  73. testFiles.forEach((file) => mocha.addFile(file))
  74. const cb = () => {
  75. // Ensure the callback is called after runner is defined
  76. process.nextTick(() => {
  77. process.exit(runner.failures)
  78. })
  79. }
  80. const runner = mocha.run(cb)
  81. })
  82. })