index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const Module = require('module');
  2. const path = require('path');
  3. const v8 = require('v8');
  4. Module.globalPaths.push(path.resolve(__dirname, '../spec/node_modules'));
  5. // Extra module paths which can be used to load Mocha reporters
  6. if (process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS) {
  7. for (const modulePath of process.env.ELECTRON_TEST_EXTRA_MODULE_PATHS.split(':')) {
  8. Module.globalPaths.push(modulePath);
  9. }
  10. }
  11. // We want to terminate on errors, not throw up a dialog
  12. process.on('uncaughtException', (err) => {
  13. console.error('Unhandled exception in main spec runner:', err);
  14. process.exit(1);
  15. });
  16. // Tell ts-node which tsconfig to use
  17. process.env.TS_NODE_PROJECT = path.resolve(__dirname, '../tsconfig.spec.json');
  18. process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';
  19. const { app, protocol } = require('electron');
  20. v8.setFlagsFromString('--expose_gc');
  21. app.commandLine.appendSwitch('js-flags', '--expose_gc');
  22. // Prevent the spec runner quiting when the first window closes
  23. app.on('window-all-closed', () => null);
  24. // Use fake device for Media Stream to replace actual camera and microphone.
  25. app.commandLine.appendSwitch('use-fake-device-for-media-stream');
  26. global.standardScheme = 'app';
  27. global.zoomScheme = 'zoom';
  28. protocol.registerSchemesAsPrivileged([
  29. { scheme: global.standardScheme, privileges: { standard: true, secure: true } },
  30. { scheme: global.zoomScheme, privileges: { standard: true, secure: true } },
  31. { scheme: 'cors-blob', privileges: { corsEnabled: true, supportFetchAPI: true } },
  32. { scheme: 'cors', privileges: { corsEnabled: true, supportFetchAPI: true } },
  33. { scheme: 'no-cors', privileges: { supportFetchAPI: true } },
  34. { scheme: 'no-fetch', privileges: { corsEnabled: true } },
  35. { scheme: 'foo', privileges: { standard: true } },
  36. { scheme: 'bar', privileges: { standard: true } }
  37. ]);
  38. app.whenReady().then(async () => {
  39. require('ts-node/register');
  40. const argv = require('yargs')
  41. .boolean('ci')
  42. .array('files')
  43. .string('g').alias('g', 'grep')
  44. .boolean('i').alias('i', 'invert')
  45. .argv;
  46. const Mocha = require('mocha');
  47. const mochaOptions = {};
  48. if (process.env.MOCHA_REPORTER) {
  49. mochaOptions.reporter = process.env.MOCHA_REPORTER;
  50. }
  51. if (process.env.MOCHA_MULTI_REPORTERS) {
  52. mochaOptions.reporterOptions = {
  53. reporterEnabled: process.env.MOCHA_MULTI_REPORTERS
  54. };
  55. }
  56. const mocha = new Mocha(mochaOptions);
  57. if (!process.env.MOCHA_REPORTER) {
  58. mocha.ui('bdd').reporter('tap');
  59. }
  60. const mochaTimeout = process.env.MOCHA_TIMEOUT || 30000;
  61. mocha.timeout(mochaTimeout);
  62. if (argv.grep) mocha.grep(argv.grep);
  63. if (argv.invert) mocha.invert();
  64. const filter = (file) => {
  65. if (!/-spec\.[tj]s$/.test(file)) {
  66. return false;
  67. }
  68. // This allows you to run specific modules only:
  69. // npm run test -match=menu
  70. const moduleMatch = process.env.npm_config_match
  71. ? new RegExp(process.env.npm_config_match, 'g')
  72. : null;
  73. if (moduleMatch && !moduleMatch.test(file)) {
  74. return false;
  75. }
  76. const baseElectronDir = path.resolve(__dirname, '..');
  77. if (argv.files && !argv.files.includes(path.relative(baseElectronDir, file))) {
  78. return false;
  79. }
  80. return true;
  81. };
  82. const getFiles = require('../spec/static/get-files');
  83. const testFiles = await getFiles(__dirname, { filter });
  84. sortToEnd(testFiles, f => f.includes('crash-reporter')).forEach((file) => {
  85. mocha.addFile(file);
  86. });
  87. const cb = () => {
  88. // Ensure the callback is called after runner is defined
  89. process.nextTick(() => {
  90. process.exit(runner.failures);
  91. });
  92. };
  93. // Set up chai in the correct order
  94. const chai = require('chai');
  95. chai.use(require('chai-as-promised'));
  96. chai.use(require('dirty-chai'));
  97. const runner = mocha.run(cb);
  98. });
  99. function partition (xs, f) {
  100. const trues = [];
  101. const falses = [];
  102. xs.forEach(x => (f(x) ? trues : falses).push(x));
  103. return [trues, falses];
  104. }
  105. function sortToEnd (xs, f) {
  106. const [end, beginning] = partition(xs, f);
  107. return beginning.concat(end);
  108. }