index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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: 'http-like', privileges: { standard: true, secure: true, corsEnabled: true, supportFetchAPI: true } },
  37. { scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
  38. { scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
  39. { scheme: 'no-cors', privileges: { supportFetchAPI: true } },
  40. { scheme: 'no-fetch', privileges: { corsEnabled: true } },
  41. { scheme: 'stream', privileges: { standard: true, stream: true } },
  42. { scheme: 'foo', privileges: { standard: true } },
  43. { scheme: 'bar', privileges: { standard: true } }
  44. ]);
  45. app.whenReady().then(async () => {
  46. require('ts-node/register');
  47. const argv = require('yargs')
  48. .boolean('ci')
  49. .array('files')
  50. .string('g').alias('g', 'grep')
  51. .boolean('i').alias('i', 'invert')
  52. .argv;
  53. const Mocha = require('mocha');
  54. const mochaOptions = {
  55. forbidOnly: process.env.CI
  56. };
  57. if (process.env.CI) {
  58. mochaOptions.retries = 3;
  59. }
  60. if (process.env.MOCHA_REPORTER) {
  61. mochaOptions.reporter = process.env.MOCHA_REPORTER;
  62. }
  63. if (process.env.MOCHA_MULTI_REPORTERS) {
  64. mochaOptions.reporterOptions = {
  65. reporterEnabled: process.env.MOCHA_MULTI_REPORTERS
  66. };
  67. }
  68. // The MOCHA_GREP and MOCHA_INVERT are used in some vendor builds for sharding
  69. // tests.
  70. if (process.env.MOCHA_GREP) {
  71. mochaOptions.grep = process.env.MOCHA_GREP;
  72. }
  73. if (process.env.MOCHA_INVERT) {
  74. mochaOptions.invert = process.env.MOCHA_INVERT === 'true';
  75. }
  76. const mocha = new Mocha(mochaOptions);
  77. // Add a root hook on mocha to skip any tests that are disabled
  78. const disabledTests = new Set(
  79. JSON.parse(
  80. fs.readFileSync(path.join(__dirname, 'disabled-tests.json'), 'utf8')
  81. )
  82. );
  83. mocha.suite.beforeEach(function () {
  84. // TODO(clavin): add support for disabling *suites* by title, not just tests
  85. if (disabledTests.has(this.currentTest?.fullTitle())) {
  86. this.skip();
  87. }
  88. });
  89. // The cleanup method is registered this way rather than through an
  90. // `afterEach` at the top level so that it can run before other `afterEach`
  91. // methods.
  92. //
  93. // The order of events is:
  94. // 1. test completes,
  95. // 2. `defer()`-ed methods run, in reverse order,
  96. // 3. regular `afterEach` hooks run.
  97. const { runCleanupFunctions } = require('./lib/spec-helpers');
  98. mocha.suite.on('suite', function attach (suite) {
  99. suite.afterEach('cleanup', runCleanupFunctions);
  100. suite.on('suite', attach);
  101. });
  102. if (!process.env.MOCHA_REPORTER) {
  103. mocha.ui('bdd').reporter('tap');
  104. }
  105. const mochaTimeout = process.env.MOCHA_TIMEOUT || 30000;
  106. mocha.timeout(mochaTimeout);
  107. if (argv.grep) mocha.grep(argv.grep);
  108. if (argv.invert) mocha.invert();
  109. const baseElectronDir = path.resolve(__dirname, '..');
  110. const validTestPaths = argv.files && argv.files.map(file =>
  111. path.isAbsolute(file)
  112. ? path.relative(baseElectronDir, file)
  113. : file);
  114. const filter = (file) => {
  115. if (!/-spec\.[tj]s$/.test(file)) {
  116. return false;
  117. }
  118. // This allows you to run specific modules only:
  119. // npm run test -match=menu
  120. const moduleMatch = process.env.npm_config_match
  121. ? new RegExp(process.env.npm_config_match, 'g')
  122. : null;
  123. if (moduleMatch && !moduleMatch.test(file)) {
  124. return false;
  125. }
  126. if (validTestPaths && !validTestPaths.includes(path.relative(baseElectronDir, file))) {
  127. return false;
  128. }
  129. return true;
  130. };
  131. const { getFiles } = require('./get-files');
  132. const testFiles = await getFiles(__dirname, { filter });
  133. for (const file of testFiles.sort()) {
  134. mocha.addFile(file);
  135. }
  136. if (validTestPaths && validTestPaths.length > 0 && testFiles.length === 0) {
  137. console.error('Test files were provided, but they did not match any searched files');
  138. console.error('provided file paths (relative to electron/):', validTestPaths);
  139. process.exit(1);
  140. }
  141. const cb = () => {
  142. // Ensure the callback is called after runner is defined
  143. process.nextTick(() => {
  144. process.exit(runner.failures);
  145. });
  146. };
  147. // Set up chai in the correct order
  148. const chai = require('chai');
  149. chai.use(require('chai-as-promised'));
  150. chai.use(require('dirty-chai'));
  151. // Show full object diff
  152. // https://github.com/chaijs/chai/issues/469
  153. chai.config.truncateThreshold = 0;
  154. const runner = mocha.run(cb);
  155. }).catch((err) => {
  156. console.error('An error occurred while running the spec runner');
  157. console.error(err);
  158. process.exit(1);
  159. });