Skip to content

Commit c7561d2

Browse files
author
DavertMik
committed
fixed workers issues
1 parent 660c7e8 commit c7561d2

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

lib/command/workers/runTests.js

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const tty = require('tty')
1+
import tty from 'tty'
22

33
if (!tty.getWindowSize) {
44
// this is really old method, long removed from Node, but Mocha
@@ -7,22 +7,18 @@ if (!tty.getWindowSize) {
77
tty.getWindowSize = () => [40, 80]
88
}
99

10-
const { parentPort, workerData } = require('worker_threads')
11-
const eventModule = require('../../event')
12-
const event = eventModule.default || eventModule
13-
const containerModule = require('../../container')
14-
const container = containerModule.default || containerModule
15-
const { getConfig } = require('../utils')
16-
const utilsModule = require('../../utils')
17-
const { tryOrDefault, deepMerge } = utilsModule.default || utilsModule
10+
import { parentPort, workerData } from 'worker_threads'
11+
import event from '../../event.js'
12+
import container from '../../container.js'
13+
import { getConfig } from '../utils.js'
14+
import { tryOrDefault, deepMerge } from '../../utils.js'
1815

1916
let stdout = ''
2017

2118
const stderr = ''
2219

23-
// Requiring of Codecept need to be after tty.getWindowSize is available.
24-
const CodeceptModule = require(process.env.CODECEPT_CLASS_PATH || '../../codecept')
25-
const Codecept = CodeceptModule.default || CodeceptModule
20+
// Importing of Codecept need to be after tty.getWindowSize is available.
21+
import Codecept from '../../codecept.js'
2622

2723
const { options, tests, testRoot, workerIndex } = workerData
2824

@@ -39,44 +35,45 @@ const overrideConfigs = tryOrDefault(() => JSON.parse(options.override), {})
3935
const config = deepMerge(getConfig(options.config || testRoot), overrideConfigs)
4036

4137
// Load test and run
42-
const codecept = new Codecept(config, options)
43-
codecept.init(testRoot)
44-
codecept.loadTests()
45-
const mocha = container.mocha()
46-
filterTests()
47-
48-
// run tests
4938
;(async function () {
50-
if (mocha.suite.total()) {
51-
await runTests()
39+
const codecept = new Codecept(config, options)
40+
await codecept.init(testRoot)
41+
codecept.loadTests()
42+
const mocha = container.mocha()
43+
44+
function filterTests() {
45+
const files = codecept.testFiles
46+
mocha.files = files
47+
mocha.loadFiles()
48+
49+
for (const suite of mocha.suite.suites) {
50+
suite.tests = suite.tests.filter(test => tests.indexOf(test.uid) >= 0)
51+
}
5252
}
53-
})()
5453

55-
async function runTests() {
56-
try {
57-
await codecept.bootstrap()
58-
} catch (err) {
59-
throw new Error(`Error while running bootstrap file :${err}`)
60-
}
61-
listenToParentThread()
62-
initializeListeners()
63-
disablePause()
64-
try {
65-
await codecept.run()
66-
} finally {
67-
await codecept.teardown()
54+
async function runTests() {
55+
try {
56+
await codecept.bootstrap()
57+
} catch (err) {
58+
throw new Error(`Error while running bootstrap file :${err}`)
59+
}
60+
listenToParentThread()
61+
initializeListeners()
62+
disablePause()
63+
try {
64+
await codecept.run()
65+
} finally {
66+
await codecept.teardown()
67+
}
6868
}
69-
}
7069

71-
function filterTests() {
72-
const files = codecept.testFiles
73-
mocha.files = files
74-
mocha.loadFiles()
70+
filterTests()
7571

76-
for (const suite of mocha.suite.suites) {
77-
suite.tests = suite.tests.filter(test => tests.indexOf(test.uid) >= 0)
72+
// run tests
73+
if (mocha.suite.total()) {
74+
await runTests()
7875
}
79-
}
76+
})()
8077

8178
function initializeListeners() {
8279
// suite

lib/mocha/test.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Test from 'mocha/lib/test.js'
22
import Suite from 'mocha/lib/suite.js'
33
import { genTestId, serializeError, clearString, relativeDir } from '../utils.js'
4-
import step from '../step/base.js'
4+
import Step from '../step/base.js'
55
import { enhanceMochaSuite } from './suite.js'
66
import { test as testWrapper } from './asyncWrapper.js'
77
/**
@@ -166,14 +166,7 @@ function testToFileName(test, { suffix = '', unique = false } = {}) {
166166
return fileName
167167
}
168168

169-
export {
170-
createTest,
171-
testToFileName,
172-
enhanceMochaTest,
173-
serializeTest,
174-
deserializeTest,
175-
cloneTest,
176-
}
169+
export { createTest, testToFileName, enhanceMochaTest, serializeTest, deserializeTest, cloneTest }
177170

178171
export default {
179172
createTest,

lib/workers.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,27 @@ class Workers extends EventEmitter {
235235
constructor(numberOfWorkers, config = { by: 'test' }) {
236236
super()
237237
this.setMaxListeners(50)
238-
this.codecept = initializeCodecept(config.testConfig, config.options)
238+
this.codeceptPromise = initializeCodecept(config.testConfig, config.options)
239+
this.codecept = null
239240
this.errors = []
240241
this.numberOfWorkers = 0
241242
this.closedWorkers = 0
242243
this.workers = []
243244
this.testGroups = []
245+
this.config = config
246+
this.numberOfWorkersRequested = numberOfWorkers
244247

245248
createOutputDir(config.testConfig)
246-
if (numberOfWorkers) this._initWorkers(numberOfWorkers, config)
249+
// Defer worker initialization until codecept is ready
250+
}
251+
252+
async _ensureInitialized() {
253+
if (!this.codecept) {
254+
this.codecept = await this.codeceptPromise
255+
if (this.numberOfWorkersRequested) {
256+
this._initWorkers(this.numberOfWorkersRequested, this.config)
257+
}
258+
}
247259
}
248260

249261
_initWorkers(numberOfWorkers, config) {
@@ -344,14 +356,17 @@ class Workers extends EventEmitter {
344356
}
345357

346358
async bootstrapAll() {
359+
await this._ensureInitialized()
347360
return runHook(this.codecept.config.bootstrapAll, 'bootstrapAll')
348361
}
349362

350363
async teardownAll() {
364+
await this._ensureInitialized()
351365
return runHook(this.codecept.config.teardownAll, 'teardownAll')
352366
}
353367

354-
run() {
368+
async run() {
369+
await this._ensureInitialized()
355370
recorder.startUnlessRunning()
356371
event.dispatcher.emit(event.workers.before)
357372
process.env.RUNS_WITH_WORKERS = 'true'

0 commit comments

Comments
 (0)