index.js 4.8 KB

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