Skip to content

Commit 684d0ef

Browse files
authored
Fixes (#5667)
1 parent 5b2517c commit 684d0ef

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

lib/rerun.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { resolveImportModulePath } from './utils.js'
1010
const require = createRequire(import.meta.url)
1111

1212
class CodeceptRerunner extends BaseCodecept {
13+
rerunId = 0
14+
1315
async runOnce(test) {
1416
await container.started()
1517

@@ -40,23 +42,23 @@ class CodeceptRerunner extends BaseCodecept {
4042
mocha.suite.suites = []
4143
mocha.suite.tests = []
4244

43-
// Manually load each test file by importing it
45+
// Load every test into the fresh Mocha instance. The lifecycle events
46+
// provide Feature/Scenario to test files when noGlobals is enabled.
4447
for (const file of filesToRun) {
48+
const absolutePath = fsPath.resolve(file)
49+
mocha.suite.emit('pre-require', global, absolutePath, mocha)
50+
4551
try {
46-
// Clear CommonJS cache if available (for mixed environments)
47-
try {
48-
delete require.cache[file]
49-
} catch (e) {
50-
// ESM modules don't have require.cache, ignore
51-
}
52-
53-
// Force reload the module by using a cache-busting query parameter
54-
const fileUrl = `${fsPath.resolve(file)}`
55-
const resolvedPath = resolveImportModulePath(fileUrl)
56-
await import(resolvedPath)
57-
} catch (e) {
58-
console.error(`Error loading test file ${file}:`, e)
59-
}
52+
delete require.cache[require.resolve(absolutePath)]
53+
} catch {}
54+
55+
const resolvedPath = resolveImportModulePath(absolutePath)
56+
const fileUrl = new URL(resolvedPath)
57+
fileUrl.searchParams.set('codeceptjsRerun', String(++this.rerunId))
58+
const testModule = await import(fileUrl.href)
59+
60+
mocha.suite.emit('require', testModule, absolutePath, mocha)
61+
mocha.suite.emit('post-require', global, absolutePath, mocha)
6062
}
6163

6264
const done = () => {

test/data/sandbox/configs/run-rerun/codecept.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export const config = {
22
tests: './*_test.js',
33
output: './output',
4+
noGlobals: true,
45
helpers: {
56
CustomHelper: {
67
require: './customHelper.js',

test/runner/run_rerun_test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('run-rerun command', function () {
5050
expect(stdout).toContain('Process run 1 of max 3, success runs 1/3');
5151
expect(stdout).toContain('Process run 2 of max 3, success runs 2/3');
5252
expect(stdout).toContain('Process run 3 of max 3, success runs 3/3');
53-
expect(stdout).toContain('1 passed');
53+
expect(stdout.match(/OK\s+\|\s+1 passed/g)).toHaveLength(3);
5454
expect(err).toBeNull();
5555
});
5656

@@ -77,7 +77,9 @@ describe('run-rerun command', function () {
7777
const { err, stdout } = await safeExec(`${codecept_run_config('codecept.conf.fail_test.js', '@RunRerun - Fail all attempt')} --debug`);
7878

7979
expect(stdout).toContain('Fail run 1 of max 3, success runs 0/2');
80-
expect(stdout).toContain('Process run 3 of max 3, success runs 2/2');
80+
expect(stdout).toContain('Fail run 2 of max 3, success runs 0/2');
81+
expect(stdout).toContain('Fail run 3 of max 3, success runs 0/2');
82+
expect(stdout).toContain('Flaky tests detected!');
8183
expect(err.code).toBe(1);
8284
});
8385

@@ -88,7 +90,8 @@ describe('run-rerun command', function () {
8890
);
8991

9092
expect(stdout).toContain('Process run 1 of max 3, success runs 1/2');
91-
expect(stdout).toContain('Process run 2 of max 3, success runs 2/2');
93+
expect(stdout).toContain('Fail run 2 of max 3, success runs 1/2');
94+
expect(stdout).toContain('Process run 3 of max 3, success runs 2/2');
9295
expect(err).toBeNull();
9396
});
9497

@@ -99,6 +102,9 @@ describe('run-rerun command', function () {
99102
);
100103

101104
expect(stdout).toContain('Process run 1 of max 3, success runs 1/3');
102-
expect(stdout).toContain('Process run 3 of max 3, success runs 3/3');
105+
expect(stdout).toContain('Fail run 2 of max 3, success runs 1/3');
106+
expect(stdout).toContain('Process run 3 of max 3, success runs 2/3');
107+
expect(stdout).toContain('Flaky tests detected!');
108+
expect(err.code).toBe(1);
103109
});
104110
});

0 commit comments

Comments
 (0)