From f1c8f53d7dc6ceaf5060b9b61e71c5471c5fed2b Mon Sep 17 00:00:00 2001 From: Denys Kuchma Date: Wed, 29 Jul 2026 19:05:49 +0200 Subject: [PATCH] Fixes --- lib/rerun.js | 32 ++++++++++--------- .../configs/run-rerun/codecept.conf.js | 1 + test/runner/run_rerun_test.js | 14 +++++--- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/rerun.js b/lib/rerun.js index 4d296338f..d3179bc55 100644 --- a/lib/rerun.js +++ b/lib/rerun.js @@ -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() @@ -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 = () => { diff --git a/test/data/sandbox/configs/run-rerun/codecept.conf.js b/test/data/sandbox/configs/run-rerun/codecept.conf.js index b4b9f31d0..018b92b68 100644 --- a/test/data/sandbox/configs/run-rerun/codecept.conf.js +++ b/test/data/sandbox/configs/run-rerun/codecept.conf.js @@ -1,6 +1,7 @@ export const config = { tests: './*_test.js', output: './output', + noGlobals: true, helpers: { CustomHelper: { require: './customHelper.js', diff --git a/test/runner/run_rerun_test.js b/test/runner/run_rerun_test.js index 138991e27..a6e0b1107 100644 --- a/test/runner/run_rerun_test.js +++ b/test/runner/run_rerun_test.js @@ -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(); }); @@ -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); }); @@ -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(); }); @@ -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); }); });