index.js 5.0 KB

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