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:
75
+
76
+
```typescript
77
+
// login_test.ts
78
+
Feature('Login')
79
+
80
+
Scenario('successful login', ({ I }) => {
81
+
I.amOnPage('/login')
82
+
I.fillField('email', 'user@example.com')
83
+
I.fillField('password', 'password123')
84
+
I.click('Login')
85
+
I.see('Welcome')
86
+
})
87
+
```
88
+
89
+
**Why tsx?**
90
+
- ⚡ **Fast:** Built on esbuild, much faster than ts-node
91
+
- 🎯 **Zero config:** Works without tsconfig.json
92
+
- 🚀 **Works with Mocha:** Uses CommonJS hooks that Mocha understands
93
+
- ✅ **Complete:** Handles all TypeScript features (enums, decorators, etc.)
94
+
95
+
### Using ts-node/esm (Alternative)
96
+
97
+
If you prefer ts-node:
98
+
99
+
**Installation:**
100
+
```bash
101
+
npm install --save-dev ts-node
102
+
```
103
+
104
+
**Configuration:**
105
+
```typescript
106
+
// codecept.conf.ts
107
+
exportconst config = {
108
+
tests: './**/*_test.ts',
109
+
require: ['ts-node/esm'], // ← Use ts-node ESM loader
110
+
helpers: { /* ... */ }
111
+
}
112
+
```
113
+
114
+
**Required tsconfig.json:**
115
+
```json
116
+
{
117
+
"compilerOptions": {
118
+
"module": "ESNext",
119
+
"target": "ES2022",
120
+
"moduleResolution": "node",
121
+
"esModuleInterop": true
122
+
},
123
+
"ts-node": {
124
+
"esm": true,
125
+
"experimentalSpecifierResolution": "node"
126
+
}
127
+
}
128
+
```
129
+
130
+
### Full TypeScript Features in Tests
131
+
132
+
With tsx or ts-node/esm, you can use complete TypeScript syntax including imports, enums, interfaces, and types:
133
+
134
+
```typescript
135
+
// types.ts
136
+
exportenumEnvironment {
137
+
TEST='test',
138
+
STAGING='staging',
139
+
PRODUCTION='production'
140
+
}
141
+
142
+
exportinterfaceUser {
143
+
email:string
144
+
password:string
145
+
}
146
+
147
+
// login_test.ts
148
+
import { Environment, User } from'./types'
149
+
150
+
const testUser:User= {
151
+
email: 'test@example.com',
152
+
password: 'password123'
153
+
}
154
+
155
+
Feature('Login')
156
+
157
+
Scenario(`Login on ${Environment.TEST}`, ({ I }) => {
158
+
I.amOnPage('/login')
159
+
I.fillField('email', testUser.email)
160
+
I.fillField('password', testUser.password)
161
+
I.click('Login')
162
+
I.see('Welcome')
163
+
})
164
+
```
165
+
166
+
### Troubleshooting TypeScript Tests
167
+
168
+
**Error: "Cannot find module" or "Unexpected token"**
169
+
170
+
This means the TypeScript loader isn't configured. Make sure:
171
+
1. You have `tsx` or `ts-node` installed: `npm install --save-dev tsx`
172
+
2. Your config includes the loader in `require` array: `require: ['tsx/cjs']`
173
+
3. The loader is specified before test files are loaded
174
+
175
+
**Error: Module not found when importing from `.ts` files**
176
+
177
+
Make sure you're using a proper TypeScript loader (`tsx/cjs` or `ts-node/esm`).
178
+
179
+
**TypeScript config files vs test files**
180
+
181
+
Note the difference:
182
+
-**Config files** (`codecept.conf.ts`, helpers): Automatically transpiled by CodeceptJS
183
+
-**Test files** (`*_test.ts`): Need a loader specified in `config.require`
184
+
185
+
### Migration from CodeceptJS 3.x
186
+
187
+
If you're upgrading from CodeceptJS 3.x (CommonJS) to 4.x (ESM):
188
+
189
+
**Old setup (3.x):**
190
+
```javascript
191
+
// codecept.conf.js
192
+
module.exports= {
193
+
tests:'./*_test.ts',
194
+
require: ['ts-node/register'], // CommonJS loader
195
+
helpers: {}
196
+
}
197
+
```
198
+
199
+
**New setup (4.x):**
200
+
```typescript
201
+
// codecept.conf.ts
202
+
exportconst config = {
203
+
tests: './*_test.ts',
204
+
require: ['tsx/cjs'], // TypeScript loader
205
+
helpers: {}
206
+
}
207
+
```
208
+
209
+
**Migration steps:**
210
+
1. Install tsx: `npm install --save-dev tsx`
211
+
2. Update package.json: `"type": "module"`
212
+
3. Rename config to `codecept.conf.ts` and use `export const config = {}`
213
+
4. Change `require: ['ts-node/register']` to `require: ['tsx/cjs']`
214
+
5. Run tests: `npx codeceptjs run`
215
+
46
216
## Promise-Based Typings
47
217
48
218
By default, CodeceptJS tests are written in synchronous mode. This is a regular CodeceptJS test:
0 commit comments