Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/rerun.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { resolveImportModulePath } from './utils.js'
const require = createRequire(import.meta.url)

class CodeceptRerunner extends BaseCodecept {
rerunId = 0

async runOnce(test) {
await container.started()

Expand Down Expand Up @@ -40,23 +42,23 @@ class CodeceptRerunner extends BaseCodecept {
mocha.suite.suites = []
mocha.suite.tests = []

// Manually load each test file by importing it
// Load every test into the fresh Mocha instance. The lifecycle events
// provide Feature/Scenario to test files when noGlobals is enabled.
for (const file of filesToRun) {
const absolutePath = fsPath.resolve(file)
mocha.suite.emit('pre-require', global, absolutePath, mocha)

try {
// Clear CommonJS cache if available (for mixed environments)
try {
delete require.cache[file]
} catch (e) {
// ESM modules don't have require.cache, ignore
}

// Force reload the module by using a cache-busting query parameter
const fileUrl = `${fsPath.resolve(file)}`
const resolvedPath = resolveImportModulePath(fileUrl)
await import(resolvedPath)
} catch (e) {
console.error(`Error loading test file ${file}:`, e)
}
delete require.cache[require.resolve(absolutePath)]
} catch {}

const resolvedPath = resolveImportModulePath(absolutePath)
const fileUrl = new URL(resolvedPath)
fileUrl.searchParams.set('codeceptjsRerun', String(++this.rerunId))
const testModule = await import(fileUrl.href)

mocha.suite.emit('require', testModule, absolutePath, mocha)
mocha.suite.emit('post-require', global, absolutePath, mocha)
}

const done = () => {
Expand Down
1 change: 1 addition & 0 deletions test/data/sandbox/configs/run-rerun/codecept.conf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const config = {
tests: './*_test.js',
output: './output',
noGlobals: true,
helpers: {
CustomHelper: {
require: './customHelper.js',
Expand Down
14 changes: 10 additions & 4 deletions test/runner/run_rerun_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('run-rerun command', function () {
expect(stdout).toContain('Process run 1 of max 3, success runs 1/3');
expect(stdout).toContain('Process run 2 of max 3, success runs 2/3');
expect(stdout).toContain('Process run 3 of max 3, success runs 3/3');
expect(stdout).toContain('1 passed');
expect(stdout.match(/OK\s+\|\s+1 passed/g)).toHaveLength(3);
expect(err).toBeNull();
});

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

expect(stdout).toContain('Fail run 1 of max 3, success runs 0/2');
expect(stdout).toContain('Process run 3 of max 3, success runs 2/2');
expect(stdout).toContain('Fail run 2 of max 3, success runs 0/2');
expect(stdout).toContain('Fail run 3 of max 3, success runs 0/2');
expect(stdout).toContain('Flaky tests detected!');
expect(err.code).toBe(1);
});

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

expect(stdout).toContain('Process run 1 of max 3, success runs 1/2');
expect(stdout).toContain('Process run 2 of max 3, success runs 2/2');
expect(stdout).toContain('Fail run 2 of max 3, success runs 1/2');
expect(stdout).toContain('Process run 3 of max 3, success runs 2/2');
expect(err).toBeNull();
});

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

expect(stdout).toContain('Process run 1 of max 3, success runs 1/3');
expect(stdout).toContain('Process run 3 of max 3, success runs 3/3');
expect(stdout).toContain('Fail run 2 of max 3, success runs 1/3');
expect(stdout).toContain('Process run 3 of max 3, success runs 2/3');
expect(stdout).toContain('Flaky tests detected!');
expect(err.code).toBe(1);
});
});