Skip to content

Commit c3bf125

Browse files
fix(workers): skip loadTests in run() when suite is already populated
Worker threads call loadTests() in filterTests() before codecept.run(). A second loadTests() in run() duplicated Gherkin suites and executed each assigned test twice (regression since ESM loadTests in #5640). Add regression tests for JS and Gherkin worker assignments. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6c45750 commit c3bf125

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

‎lib/codecept.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,12 @@ class Codecept {
314314
})
315315
}
316316

317-
await loadTests(mocha)
317+
// Worker threads pre-load and filter tests before run(); reloading duplicates Gherkin suites.
318+
if (!mocha.suite.total()) {
319+
await loadTests(mocha)
320+
} else {
321+
mocha.lazyLoadFiles(true)
322+
}
318323

319324
return new Promise((resolve, reject) => {
320325
const done = async (failures) => {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const config = {
2+
tests: './workers-gherkin/_placeholder.js',
3+
timeout: 10000,
4+
output: './output',
5+
helpers: {
6+
BDD: {
7+
require: './support/bdd_helper.js',
8+
},
9+
Workers: {
10+
require: './workers_helper.js',
11+
},
12+
},
13+
gherkin: {
14+
features: './workers-gherkin/*.feature',
15+
steps: ['./workers-gherkin/steps.js'],
16+
},
17+
include: {},
18+
bootstrap: false,
19+
mocha: {},
20+
name: 'sandbox-workers-gherkin',
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Gherkin-only worker sandbox; JS tests path must exist for config.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feature: Worker gherkin single scenario
2+
3+
Scenario: runs once @worker_gherkin_once
4+
Given I open a browser on a site
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Given } from '../../../../lib/mocha/bdd.js'
2+
3+
Given('I open a browser on a site', () => {})

‎test/unit/worker_test.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,56 @@ describe('Workers', function () {
417417
const actualFiles = testFiles.map(f => path.resolve(f))
418418
expect(actualFiles).to.deep.equal(expectedFiles, 'loadTests() should preserve original glob order without sorting')
419419
})
420+
421+
it('should run each assigned gherkin worker test once after pre-load', done => {
422+
const workerConfig = {
423+
by: 'test',
424+
testConfig: './test/data/sandbox/codecept.workers-gherkin.conf.js',
425+
options: {
426+
grep: '@worker_gherkin_once',
427+
},
428+
}
429+
430+
let passedCount = 0
431+
const workers = new Workers(1, workerConfig)
432+
433+
workers.on(event.test.passed, () => {
434+
passedCount += 1
435+
})
436+
437+
workers.run()
438+
439+
workers.on(event.all.result, result => {
440+
expect(passedCount).to.equal(1)
441+
expect(result.stats.tests).to.equal(1)
442+
expect(result.stats.passes).to.equal(1)
443+
done()
444+
})
445+
})
446+
447+
it('should run each assigned worker test once after pre-load (no duplicate loadTests in run)', done => {
448+
const workerConfig = {
449+
by: 'test',
450+
testConfig: './test/data/sandbox/codecept.workers.conf.js',
451+
options: {
452+
grep: 'From worker @1_grep print message 1',
453+
},
454+
}
455+
456+
let passedCount = 0
457+
const workers = new Workers(1, workerConfig)
458+
459+
workers.on(event.test.passed, () => {
460+
passedCount += 1
461+
})
462+
463+
workers.run()
464+
465+
workers.on(event.all.result, result => {
466+
expect(passedCount).to.equal(1)
467+
expect(result.stats.tests).to.equal(1)
468+
expect(result.stats.passes).to.equal(1)
469+
done()
470+
})
471+
})
420472
})

0 commit comments

Comments
 (0)