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
feat: Add TypeScript test support with tsx loader (v4.0.0-beta.19)
- Implemented Option 1A: tsx/cjs loader for TypeScript test files in ESM
- Added comprehensive TypeScript test support documentation
- Created lib/utils/loaderCheck.js utility for TypeScript setup validation
- Updated init command to include tsx and configure require: ['tsx/cjs']
- Added tsx as devDependency and optional peerDependency
- Improved requireModules() to resolve packages from user's directory
- Added helpful error messages when TypeScript tests found without loader
- Created comprehensive test suite demonstrating TS features (enums, interfaces, imports)
- All unit tests passing (488 passing)
- TypeScript tests verified working with tsx/cjs loader
Key Changes:
* docs/typescript.md: New section on TypeScript tests in ESM with tsx/cjs
* docs/configuration.md: Updated require section with ESM examples
* lib/codecept.js: Added TypeScript validation and improved module resolution
* lib/command/init.js: Auto-configure tsx/cjs for TypeScript projects
* lib/utils/loaderCheck.js: NEW - TypeScript loader validation utility
* package.json: Bumped version to 4.0.0-beta.19, added tsx
* test/data/typescript-static-imports/: Complete TypeScript test examples
Technical Details:
- Uses tsx/cjs (CommonJS hooks) instead of tsx/esm (ESM loaders)
- Works because Mocha internally uses require() for test loading
- tsx/cjs intercepts require() calls and transpiles .ts files on-the-fly
- Supports all TypeScript features: enums, interfaces, types, decorators
- Zero configuration required (no tsconfig.json needed)
- Fast transpilation using esbuild
Copy file name to clipboardExpand all lines: docs/configuration.md
+58-20Lines changed: 58 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,40 +33,78 @@ After running `codeceptjs init` it should be saved in test root.
33
33
34
34
## Require
35
35
36
-
Requires described module before run. This option is useful for assertion libraries, so you may `--require should` instead of manually invoking `require('should')` within each test file. It can be used with relative paths, e.g. `"require": ["/lib/somemodule"]`, and installed packages.
36
+
Requires modules before running tests. This is useful for:
37
+
-**Assertion libraries:** e.g., `'should'` instead of manually requiring it in each test
38
+
-**TypeScript loaders:** Enable TypeScript test files in ESM or CommonJS projects
Modules are loaded in the order specified, before any tests run.
107
+
70
108
## Dynamic Configuration
71
109
72
110
By default `codecept.json` is used for configuration. You can override its values in runtime by using `--override` or `-o` option in command line, passing valid JSON as a value:
Copy file name to clipboardExpand all lines: docs/typescript.md
+170Lines changed: 170 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,176 @@ Then a config file and new tests will be created in TypeScript format.
43
43
44
44
If a config file is set in TypeScript format (`codecept.conf.ts`) package `ts-node` will be used to run tests.
45
45
46
+
## TypeScript Tests in ESM (CodeceptJS 4.x) <Badgetext="Since 4.0.0"type="tip"/>
47
+
48
+
CodeceptJS 4.x uses ES Modules (ESM) which requires a different approach for TypeScript test files. While TypeScript **config files** (`codecept.conf.ts`) are automatically transpiled, TypeScript **test files** need a loader.
49
+
50
+
### Using tsx (Recommended)
51
+
52
+
[tsx](https://tsx.is) is a modern, fast TypeScript loader built on esbuild. It's the recommended way to run TypeScript tests in CodeceptJS 4.x.
53
+
54
+
**Installation:**
55
+
```bash
56
+
npm install --save-dev tsx
57
+
```
58
+
59
+
**Configuration:**
60
+
```typescript
61
+
// codecept.conf.ts
62
+
exportconst config = {
63
+
tests: './**/*_test.ts',
64
+
require: ['tsx/cjs'], // ← Enable TypeScript loader for test files
65
+
helpers: {
66
+
Playwright: {
67
+
url: 'http://localhost',
68
+
browser: 'chromium'
69
+
}
70
+
}
71
+
}
72
+
```
73
+
74
+
That's it! Now you can write tests in TypeScript with full language support:
0 commit comments