index.js 4.3 KB

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