From 6d1b1c68446906690a9830b27410f8a213a13153 Mon Sep 17 00:00:00 2001 From: Devraj Mehta Date: Sun, 1 Oct 2023 11:35:40 -0400 Subject: [PATCH] test: add utility process mocha runner to run net module tests --- spec/api-utility-process-spec.ts | 7 +++ spec/fixtures/api/utility-process/mocha.js | 61 ++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 spec/fixtures/api/utility-process/mocha.js diff --git a/spec/api-utility-process-spec.ts b/spec/api-utility-process-spec.ts index bfa1c1f7c31c04..447f4a6979dd52 100644 --- a/spec/api-utility-process-spec.ts +++ b/spec/api-utility-process-spec.ts @@ -435,4 +435,11 @@ describe('utilityProcess module', () => { await exit; }); }); + + it('should pass the api-net-spec tests', async () => { + const child = utilityProcess.fork(path.join(fixturesPath, 'mocha.js')); + child.postMessage('spec/api-net-spec.ts'); + const [code] = await once(child, 'exit'); + expect(code).to.equal(0); + }); }); diff --git a/spec/fixtures/api/utility-process/mocha.js b/spec/fixtures/api/utility-process/mocha.js new file mode 100644 index 00000000000000..eae1a6d885bddc --- /dev/null +++ b/spec/fixtures/api/utility-process/mocha.js @@ -0,0 +1,61 @@ +const path = require('node:path'); +const v8 = require('node:v8'); + +require('ts-node/register'); + +v8.setFlagsFromString('--expose_gc'); + +const Mocha = require('mocha'); +const mochaOptions = { + forbidOnly: process.env.CI +}; +if (process.env.CI) { + mochaOptions.retries = 3; +} +if (process.env.MOCHA_REPORTER) { + mochaOptions.reporter = process.env.MOCHA_REPORTER; +} +if (process.env.MOCHA_MULTI_REPORTERS) { + mochaOptions.reporterOptions = { + reporterEnabled: process.env.MOCHA_MULTI_REPORTERS + }; +} +const mocha = new Mocha(mochaOptions); + +const { runCleanupFunctions } = require('../../../lib/spec-helpers'); +mocha.suite.on('suite', function attach (suite) { + suite.afterEach('cleanup', runCleanupFunctions); + suite.on('suite', attach); +}); + +if (!process.env.MOCHA_REPORTER) { + mocha.ui('bdd').reporter('tap'); +} + +const mochaTimeout = process.env.MOCHA_TIMEOUT || 30000; +mocha.timeout(mochaTimeout); + +const baseElectronDir = path.resolve(__dirname, '../../../..'); + +process.parentPort.on('message', (e) => { + console.log('child received', e); + console.log('path', path.join(baseElectronDir, e.data)); + + mocha.addFile(path.join(baseElectronDir, e.data)); + + // Set up chai in the correct order + const chai = require('chai'); + chai.use(require('chai-as-promised')); + chai.use(require('dirty-chai')); + + // Show full object diff + // https://github.com/chaijs/chai/issues/469 + chai.config.truncateThreshold = 0; + + mocha.run((failures) => { + // Ensure the callback is called after runner is defined + process.nextTick(() => { + process.exit(failures); + }); + }); +});