index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Extra module paths which can be used to load Mocha reporters
  6. if (process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS) {
  7. for (const modulePath of process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS.split(':')) {
  8. Module.globalPaths.push(modulePath);
  9. }
  10. }
  11. // We want to terminate on errors, not throw up a dialog
  12. process.on('uncaughtException', (err) => {
  13. console.error('Unhandled exception in main spec runner:', err);
  14. process.exit(1);
  15. });
  16. // Tell ts-node which tsconfig to use
  17. process.env.TS_NODE_PROJECT = path.resolve(__dirname, '../tsconfig.spec.json');
  18. process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';
  19. const { app, protocol } = require('electron');
  20. v8.setFlagsFromString('--expose_gc');
  21. app.commandLine.appendSwitch('js-flags', '--expose_gc');
  22. // Prevent the spec runner quiting when the first window closes
  23. app.on('window-all-closed', () => null);
  24. // Use fake device for Media Stream to replace actual camera and microphone.
  25. app.commandLine.appendSwitch('use-fake-device-for-media-stream');
  26. global.standardScheme = 'app';
  27. global.zoomScheme = 'zoom';
  28. global.serviceWorkerScheme = 'sw';
  29. protocol.registerSchemesAsPrivileged([
  30. { scheme: global.standardScheme, privileges: { standard: true, secure: true, stream: false } },
  31. { scheme: global.zoomScheme, privileges: { standard: true, secure: true } },
  32. { scheme: global.serviceWorkerScheme, privileges: { allowServiceWorkers: true, standard: true, secure: true } },
  33. { scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
  34. { scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
  35. { scheme: 'no-cors', privileges: { supportFetchAPI: true } },
  36. { scheme: 'no-fetch', privileges: { corsEnabled: true } },
  37. { scheme: 'stream', privileges: { standard: true, stream: true } },
  38. { scheme: 'foo', privileges: { standard: true } },
  39. { scheme: 'bar', privileges: { standard: true } }
  40. ]);
  41. app.whenReady().then(async () => {
  42. require('ts-node/register');
  43. const argv = require('yargs')
  44. .boolean('ci')
  45. .array('files')
  46. .string('g').alias('g', 'grep')
  47. .boolean('i').alias('i', 'invert')
  48. .argv;
  49. const Mocha = require('mocha');
  50. const mochaOptions = {};
  51. if (process.env.MOCHA_REPORTER) {
  52. mochaOptions.reporter = process.env.MOCHA_REPORTER;
  53. }
  54. if (process.env.MOCHA_MULTI_REPORTERS) {
  55. mochaOptions.reporterOptions = {
  56. reporterEnabled: process.env.MOCHA_MULTI_REPORTERS
  57. };
  58. }
  59. const mocha = new Mocha(mochaOptions);
  60. // The cleanup method is registered this way rather than through an
  61. // `afterEach` at the top level so that it can run before other `afterEach`
  62. // methods.
  63. //
  64. // The order of events is:
  65. // 1. test completes,
  66. // 2. `defer()`-ed methods run, in reverse order,
  67. // 3. regular `afterEach` hooks run.
  68. const { runCleanupFunctions } = require('./spec-helpers');
  69. mocha.suite.on('suite', function attach (suite) {
  70. suite.afterEach('cleanup', runCleanupFunctions);
  71. suite.on('suite', attach);
  72. });
  73. if (!process.env.MOCHA_REPORTER) {
  74. mocha.ui('bdd').reporter('tap');
  75. }
  76. const mochaTimeout = process.env.MOCHA_TIMEOUT || 30000;
  77. mocha.timeout(mochaTimeout);
  78. if (argv.grep) mocha.grep(argv.grep);
  79. if (argv.invert) mocha.invert();
  80. const filter = (file) => {
  81. if (!/-spec\.[tj]s$/.test(file)) {
  82. return false;
  83. }
  84. // This allows you to run specific modules only:
  85. // npm run test -match=menu
  86. const moduleMatch = process.env.npm_config_match
  87. ? new RegExp(process.env.npm_config_match, 'g')
  88. : null;
  89. if (moduleMatch && !moduleMatch.test(file)) {
  90. return false;
  91. }
  92. const baseElectronDir = path.resolve(__dirname, '..');
  93. if (argv.files && !argv.files.includes(path.relative(baseElectronDir, file))) {
  94. return false;
  95. }
  96. return true;
  97. };
  98. const getFiles = require('../spec/static/get-files');
  99. const testFiles = await getFiles(__dirname, { filter });
  100. testFiles.sort().forEach((file) => {
  101. mocha.addFile(file);
  102. });
  103. const cb = () => {
  104. // Ensure the callback is called after runner is defined
  105. process.nextTick(() => {
  106. process.exit(runner.failures);
  107. });
  108. };
  109. // Set up chai in the correct order
  110. const chai = require('chai');
  111. chai.use(require('chai-as-promised'));
  112. chai.use(require('dirty-chai'));
  113. // Show full object diff
  114. // https://github.com/chaijs/chai/issues/469
  115. chai.config.truncateThreshold = 0;
  116. const runner = mocha.run(cb);
  117. }).catch((err) => {
  118. console.error('An error occurred while running the spec-main spec runner');
  119. console.error(err);
  120. process.exit(1);
  121. });