You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(core): load Mocha test files as ES Modules so internal-API imports resolve to the live instance
Test files were loaded with synchronous require(), which under tsx/cjs
created a second CommonJS copy of lib/* modules. A test's
`import { config } from "codeceptjs"` then resolved to a disconnected copy
whose module-level singletons were never populated (#5636).
Load test files through `await import()` instead — the same way the
container already loads helpers — so the whole run shares one ES module
graph and plain imports are honest. New lib/mocha/loadTests.js mirrors
Mocha's loadFiles (lazyLoadFiles + per-file pre-require/require/post-require)
to preserve teardown hooks, the gherkin .feature path, and the
dup/missing-Feature validation.
All synchronous mocha.loadFiles() sites converted: codecept.run(), rerun,
workers (parent grouping + worker threads), dry-run, and check.
TypeScript: tsx/cjs is a require hook and can no longer transpile test
files loaded via import(). requireModules() auto-maps tsx/cjs -> tsx/esm
with a deprecation notice; tsx/esm needs "type": "module" in package.json
(init now writes it, and an ERR_REQUIRE_CYCLE_MODULE guard points users to it).
BREAKING CHANGE: the programmatic Workers grouping API
(createGroupsOfTests, createGroupsOfSuites, addTestFiles, splitTestsByGroups)
is now async. TypeScript projects must set "type": "module" in package.json.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/configuration.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,16 +71,18 @@ For TypeScript test files in CodeceptJS 4.x, use the [`tsx`](https://tsx.is) loa
71
71
// codecept.conf.ts
72
72
exportconst config = {
73
73
tests: './**/*_test.ts',
74
-
require: ['tsx/cjs'],
74
+
require: ['tsx/esm'],
75
75
helpers: {},
76
76
include: {},
77
77
}
78
78
```
79
79
80
+
This requires `"type": "module"` in `package.json` so `.ts` test files are compiled as ES Modules.
81
+
80
82
Combine several modules:
81
83
82
84
```ts
83
-
require: ['tsx/cjs', 'should', './lib/testSetup']
85
+
require: ['tsx/esm', 'should', './lib/testSetup']
84
86
```
85
87
86
88
The config file itself (`codecept.conf.ts`) and helpers are transpiled automatically — only test files need the loader. See [TypeScript](/typescript) for the full setup.
const workers = new Workers(null, { testConfig: './codecept.conf.js' })
117
117
118
118
// split the suite into 2 groups, run each group on two browsers
119
-
const groups = workers.createGroupsOfSuites(2)
119
+
const groups = await workers.createGroupsOfSuites(2)
120
120
for (const browser of ['chromium', 'firefox']) {
121
121
for (const group of groups) {
122
122
const worker = workers.spawn()
@@ -139,7 +139,7 @@ try {
139
139
Building blocks:
140
140
141
141
- `new Workers(N, { testConfig, options })`— `N` workers; pass `null` to spawn them yourself with `spawn()`.
142
-
- `createGroupsOfTests(n)`/ `createGroupsOfSuites(n)` — split the suite into `n` groups.
142
+
- `await createGroupsOfTests(n)` / `await createGroupsOfSuites(n)` — split the suite into `n` groups (async: test files are loaded as ES Modules).
143
143
- `worker.addTests(group)`/ `worker.addConfig(partialConfig)` — assign tests and config overrides to a spawned worker.
144
144
- `bootstrapAll()`→ `run()` → `teardownAll()` — lifecycle (wrap `run()` in `try/finally` so teardown always runs).
145
145
- Events on the `workers` object: `event.test.passed`, `event.test.failed`, `event.all.result`, plus `'message'` for anything a child worker sends. `printResults()` prints the standard summary; `result.hasFailed()` and `result.stats` give the totals.
It writes `codecept.conf.ts` and `*_test.ts` files. The **config file** and helpers are transpiled automatically. **Test files** need a loader — CodeceptJS 4.x is ESM, and Mocha loads test files through CommonJS hooks, so use [`tsx`](https://tsx.is) (fast, esbuild-based, no `tsconfig.json` required):
18
+
It writes `codecept.conf.ts` and `*_test.ts` files. The **config file** and helpers are transpiled automatically. **Test files** need a loader — CodeceptJS 4.x is ESM and loads test files as ES Modules, so use [`tsx`](https://tsx.is) (fast, esbuild-based, no `tsconfig.json` required):
19
19
20
20
```sh
21
21
npm i tsx --save-dev
@@ -25,16 +25,16 @@ npm i tsx --save-dev
25
25
// codecept.conf.ts
26
26
exportconst config = {
27
27
tests: './**/*_test.ts',
28
-
require: ['tsx/cjs'], // loads the *_test.ts files
28
+
require: ['tsx/esm'], // loads the *_test.ts files as ES Modules
Set `"type": "module"` in `package.json` so `tsx` compiles your `.ts` test files as ES Modules. Then run the tests with `npx codeceptjs run`.
36
36
37
-
> Adding TypeScript to an existing project: set `"type": "module"` in `package.json`, rename the config to `codecept.conf.ts` with `export const config = {}`, install `tsx`, and add `require: ['tsx/cjs']`.
37
+
> Adding TypeScript to an existing project: set `"type": "module"` in `package.json`, rename the config to `codecept.conf.ts` with `export const config = {}`, install `tsx`, and add `require: ['tsx/esm']`.
> **Cannot find module** or **Unexpected token** while running tests means the loader isn't wired up — check that `tsx` is installed and `require: ['tsx/cjs']` is in the config.
62
+
> **Cannot find module** or **Unexpected token** while running tests means the loader isn't wired up — check that `tsx` is installed and `require: ['tsx/esm']` is in the config.
63
+
>
64
+
> **`ERR_REQUIRE_CYCLE_MODULE`** means `tsx` is compiling your `.ts` tests as CommonJS — add `"type": "module"` to the nearest `package.json`.
output.print(output.styles.debug('`tsx/cjs` is deprecated for test files. Using `tsx/esm` instead. Update your config `require` to `tsx/esm` and add `"type": "module"` to package.json.'))
0 commit comments