index.js 6.4 KB

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