index.js 6.5 KB

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