index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. app.commandLine.appendSwitch('host-rules', 'MAP localhost2 127.0.0.1');
  27. global.standardScheme = 'app';
  28. global.zoomScheme = 'zoom';
  29. global.serviceWorkerScheme = 'sw';
  30. protocol.registerSchemesAsPrivileged([
  31. { scheme: global.standardScheme, privileges: { standard: true, secure: true, stream: false } },
  32. { scheme: global.zoomScheme, privileges: { standard: true, secure: true } },
  33. { scheme: global.serviceWorkerScheme, privileges: { allowServiceWorkers: true, standard: true, secure: true } },
  34. { scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
  35. { scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
  36. { scheme: 'no-cors', privileges: { supportFetchAPI: true } },
  37. { scheme: 'no-fetch', privileges: { corsEnabled: true } },
  38. { scheme: 'stream', privileges: { standard: true, stream: true } },
  39. { scheme: 'foo', privileges: { standard: true } },
  40. { scheme: 'bar', privileges: { standard: true } }
  41. ]);
  42. app.whenReady().then(async () => {
  43. require('ts-node/register');
  44. const argv = require('yargs')
  45. .boolean('ci')
  46. .array('files')
  47. .string('g').alias('g', 'grep')
  48. .boolean('i').alias('i', 'invert')
  49. .argv;
  50. const Mocha = require('mocha');
  51. const mochaOptions = {};
  52. if (process.env.MOCHA_REPORTER) {
  53. mochaOptions.reporter = process.env.MOCHA_REPORTER;
  54. }
  55. if (process.env.MOCHA_MULTI_REPORTERS) {
  56. mochaOptions.reporterOptions = {
  57. reporterEnabled: process.env.MOCHA_MULTI_REPORTERS
  58. };
  59. }
  60. const mocha = new Mocha(mochaOptions);
  61. // The cleanup method is registered this way rather than through an
  62. // `afterEach` at the top level so that it can run before other `afterEach`
  63. // methods.
  64. //
  65. // The order of events is:
  66. // 1. test completes,
  67. // 2. `defer()`-ed methods run, in reverse order,
  68. // 3. regular `afterEach` hooks run.
  69. const { runCleanupFunctions } = require('./spec-helpers');
  70. mocha.suite.on('suite', function attach (suite) {
  71. suite.afterEach('cleanup', runCleanupFunctions);
  72. suite.on('suite', attach);
  73. });
  74. if (!process.env.MOCHA_REPORTER) {
  75. mocha.ui('bdd').reporter('tap');
  76. }
  77. const mochaTimeout = process.env.MOCHA_TIMEOUT || 30000;
  78. mocha.timeout(mochaTimeout);
  79. if (argv.grep) mocha.grep(argv.grep);
  80. if (argv.invert) mocha.invert();
  81. const filter = (file) => {
  82. if (!/-spec\.[tj]s$/.test(file)) {
  83. return false;
  84. }
  85. // This allows you to run specific modules only:
  86. // npm run test -match=menu
  87. const moduleMatch = process.env.npm_config_match
  88. ? new RegExp(process.env.npm_config_match, 'g')
  89. : null;
  90. if (moduleMatch && !moduleMatch.test(file)) {
  91. return false;
  92. }
  93. const baseElectronDir = path.resolve(__dirname, '..');
  94. if (argv.files && !argv.files.includes(path.relative(baseElectronDir, file))) {
  95. return false;
  96. }
  97. return true;
  98. };
  99. const getFiles = require('../spec/static/get-files');
  100. const testFiles = await getFiles(__dirname, { filter });
  101. testFiles.sort().forEach((file) => {
  102. mocha.addFile(file);
  103. });
  104. const cb = () => {
  105. // Ensure the callback is called after runner is defined
  106. process.nextTick(() => {
  107. process.exit(runner.failures);
  108. });
  109. };
  110. // Set up chai in the correct order
  111. const chai = require('chai');
  112. chai.use(require('chai-as-promised'));
  113. chai.use(require('dirty-chai'));
  114. // Show full object diff
  115. // https://github.com/chaijs/chai/issues/469
  116. chai.config.truncateThreshold = 0;
  117. const runner = mocha.run(cb);
  118. }).catch((err) => {
  119. console.error('An error occurred while running the spec-main spec runner');
  120. console.error(err);
  121. process.exit(1);
  122. });