index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. const fs = require('node:fs');
  2. const path = require('node:path');
  3. const v8 = require('node:v8');
  4. // We want to terminate on errors, not throw up a dialog
  5. process.on('uncaughtException', (err) => {
  6. console.error('Unhandled exception in main spec runner:', err);
  7. process.exit(1);
  8. });
  9. // Tell ts-node which tsconfig to use
  10. process.env.TS_NODE_PROJECT = path.resolve(__dirname, '../tsconfig.spec.json');
  11. process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';
  12. const { app, protocol } = require('electron');
  13. // Some Linux machines have broken hardware acceleration support.
  14. if (process.env.ELECTRON_TEST_DISABLE_HARDWARE_ACCELERATION) {
  15. app.disableHardwareAcceleration();
  16. }
  17. v8.setFlagsFromString('--expose_gc');
  18. app.commandLine.appendSwitch('js-flags', '--expose_gc');
  19. // Prevent the spec runner quitting when the first window closes
  20. app.on('window-all-closed', () => null);
  21. // Use fake device for Media Stream to replace actual camera and microphone.
  22. app.commandLine.appendSwitch('use-fake-device-for-media-stream');
  23. app.commandLine.appendSwitch('host-rules', 'MAP localhost2 127.0.0.1');
  24. app.commandLine.appendSwitch('host-resolver-rules', [
  25. 'MAP ipv4.localhost2 10.0.0.1',
  26. 'MAP ipv6.localhost2 [::1]',
  27. 'MAP notfound.localhost2 ~NOTFOUND'
  28. ].join(', '));
  29. global.standardScheme = 'app';
  30. global.zoomScheme = 'zoom';
  31. global.serviceWorkerScheme = 'sw';
  32. protocol.registerSchemesAsPrivileged([
  33. { scheme: global.standardScheme, privileges: { standard: true, secure: true, stream: false } },
  34. { scheme: global.zoomScheme, privileges: { standard: true, secure: true } },
  35. { scheme: global.serviceWorkerScheme, privileges: { allowServiceWorkers: true, standard: true, secure: true } },
  36. { scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
  37. { scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
  38. { scheme: 'no-cors', privileges: { supportFetchAPI: true } },
  39. { scheme: 'no-fetch', privileges: { corsEnabled: true } },
  40. { scheme: 'stream', privileges: { standard: true, stream: true } },
  41. { scheme: 'foo', privileges: { standard: true } },
  42. { scheme: 'bar', privileges: { standard: true } }
  43. ]);
  44. app.whenReady().then(async () => {
  45. require('ts-node/register');
  46. const argv = require('yargs')
  47. .boolean('ci')
  48. .array('files')
  49. .string('g').alias('g', 'grep')
  50. .boolean('i').alias('i', 'invert')
  51. .argv;
  52. const Mocha = require('mocha');
  53. const mochaOptions = {
  54. forbidOnly: process.env.CI
  55. };
  56. if (process.env.CI) {
  57. mochaOptions.retries = 3;
  58. }
  59. if (process.env.MOCHA_REPORTER) {
  60. mochaOptions.reporter = process.env.MOCHA_REPORTER;
  61. }
  62. if (process.env.MOCHA_MULTI_REPORTERS) {
  63. mochaOptions.reporterOptions = {
  64. reporterEnabled: process.env.MOCHA_MULTI_REPORTERS
  65. };
  66. }
  67. // The MOCHA_GREP and MOCHA_INVERT are used in some vendor builds for sharding
  68. // tests.
  69. if (process.env.MOCHA_GREP) {
  70. mochaOptions.grep = process.env.MOCHA_GREP;
  71. }
  72. if (process.env.MOCHA_INVERT) {
  73. mochaOptions.invert = process.env.MOCHA_INVERT === 'true';
  74. }
  75. const mocha = new Mocha(mochaOptions);
  76. // Add a root hook on mocha to skip any tests that are disabled
  77. const disabledTests = new Set(
  78. JSON.parse(
  79. fs.readFileSync(path.join(__dirname, 'disabled-tests.json'), 'utf8')
  80. )
  81. );
  82. mocha.suite.beforeEach(function () {
  83. // TODO(clavin): add support for disabling *suites* by title, not just tests
  84. if (disabledTests.has(this.currentTest?.fullTitle())) {
  85. this.skip();
  86. }
  87. });
  88. // The cleanup method is registered this way rather than through an
  89. // `afterEach` at the top level so that it can run before other `afterEach`
  90. // methods.
  91. //
  92. // The order of events is:
  93. // 1. test completes,
  94. // 2. `defer()`-ed methods run, in reverse order,
  95. // 3. regular `afterEach` hooks run.
  96. const { runCleanupFunctions } = require('./lib/spec-helpers');
  97. mocha.suite.on('suite', function attach (suite) {
  98. suite.afterEach('cleanup', runCleanupFunctions);
  99. suite.on('suite', attach);
  100. });
  101. if (!process.env.MOCHA_REPORTER) {
  102. mocha.ui('bdd').reporter('tap');
  103. }
  104. const mochaTimeout = process.env.MOCHA_TIMEOUT || 30000;
  105. mocha.timeout(mochaTimeout);
  106. if (argv.grep) mocha.grep(argv.grep);
  107. if (argv.invert) mocha.invert();
  108. const baseElectronDir = path.resolve(__dirname, '..');
  109. const validTestPaths = argv.files && argv.files.map(file =>
  110. path.isAbsolute(file)
  111. ? path.relative(baseElectronDir, file)
  112. : file);
  113. const filter = (file) => {
  114. if (!/-spec\.[tj]s$/.test(file)) {
  115. return false;
  116. }
  117. // This allows you to run specific modules only:
  118. // npm run test -match=menu
  119. const moduleMatch = process.env.npm_config_match
  120. ? new RegExp(process.env.npm_config_match, 'g')
  121. : null;
  122. if (moduleMatch && !moduleMatch.test(file)) {
  123. return false;
  124. }
  125. if (validTestPaths && !validTestPaths.includes(path.relative(baseElectronDir, file))) {
  126. return false;
  127. }
  128. return true;
  129. };
  130. const { getFiles } = require('./get-files');
  131. const testFiles = await getFiles(__dirname, { filter });
  132. for (const file of testFiles.sort()) {
  133. mocha.addFile(file);
  134. }
  135. if (validTestPaths && validTestPaths.length > 0 && testFiles.length === 0) {
  136. console.error('Test files were provided, but they did not match any searched files');
  137. console.error('provided file paths (relative to electron/):', validTestPaths);
  138. process.exit(1);
  139. }
  140. const cb = () => {
  141. // Ensure the callback is called after runner is defined
  142. process.nextTick(() => {
  143. process.exit(runner.failures);
  144. });
  145. };
  146. // Set up chai in the correct order
  147. const chai = require('chai');
  148. chai.use(require('chai-as-promised'));
  149. chai.use(require('dirty-chai'));
  150. // Show full object diff
  151. // https://github.com/chaijs/chai/issues/469
  152. chai.config.truncateThreshold = 0;
  153. const runner = mocha.run(cb);
  154. }).catch((err) => {
  155. console.error('An error occurred while running the spec runner');
  156. console.error(err);
  157. process.exit(1);
  158. });