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
- 🚀 **Works with Mocha:** Uses CommonJS hooks that Mocha understands
93
93
- ✅ **Complete:** Handles all TypeScript features (enums, decorators, etc.)
94
94
95
-
### Using ts-node/esm (Alternative)
95
+
### Using ts-node/esm (Not Recommended)
96
96
97
-
If you prefer ts-node:
97
+
> ⚠️ **Note:**`ts-node/esm` has significant limitations with module resolution and doesn't work well with modern ESM TypeScript projects. **We strongly recommend using `tsx` instead.** The information below is provided for reference only.
98
+
99
+
`ts-node/esm` has several issues:
100
+
- Doesn't support `"type": "module"` in package.json
101
+
- Doesn't resolve extensionless imports or `.js` imports to `.ts` files
102
+
- Requires explicit `.ts` extensions in imports, which isn't standard TypeScript practice
103
+
- Less reliable than `tsx` for ESM scenarios
104
+
105
+
**If you still want to use ts-node/esm:**
98
106
99
-
**Installation:**
100
107
```bash
101
108
npm install --save-dev ts-node
102
109
```
103
110
104
-
**Configuration:**
105
111
```typescript
106
112
// codecept.conf.ts
107
113
exportconst config = {
108
114
tests: './**/*_test.ts',
109
-
require: ['ts-node/esm'],// ← Use ts-node ESM loader
115
+
require: ['ts-node/esm'],
110
116
helpers: { /* ... */ }
111
117
}
112
118
```
113
119
114
-
**Required tsconfig.json:**
115
120
```json
121
+
// tsconfig.json
116
122
{
117
123
"compilerOptions": {
118
124
"module": "ESNext",
@@ -121,12 +127,18 @@ export const config = {
121
127
"esModuleInterop": true
122
128
},
123
129
"ts-node": {
124
-
"esm": true,
125
-
"experimentalSpecifierResolution": "node"
130
+
"esm": true
126
131
}
127
132
}
128
133
```
129
134
135
+
**Critical Limitations:**
136
+
- ❌ Cannot use `"type": "module"` in package.json
137
+
- ❌ Import statements must match the actual file (no automatic resolution)
138
+
- ❌ Module resolution doesn't work like standard TypeScript/Node.js ESM
139
+
140
+
**Recommendation:** Use `tsx/cjs` instead for a better experience.
141
+
130
142
### Full TypeScript Features in Tests
131
143
132
144
With tsx or ts-node/esm, you can use complete TypeScript syntax including imports, enums, interfaces, and types:
@@ -174,7 +186,19 @@ This means the TypeScript loader isn't configured. Make sure:
174
186
175
187
**Error: Module not found when importing from `.ts` files**
176
188
177
-
Make sure you're using a proper TypeScript loader (`tsx/cjs` or `ts-node/esm`).
189
+
When using `ts-node/esm` with ESM, you need to use `.js` extensions in imports:
190
+
191
+
```typescript
192
+
// This will cause an error in ESM mode:
193
+
importloginPagefrom"./pages/Login"
194
+
195
+
// Use .js extension instead:
196
+
importloginPagefrom"./pages/Login.js"
197
+
```
198
+
199
+
TypeScript will resolve the `.js` import to your `.ts` file during compilation. This is the standard behavior for ESM + TypeScript.
200
+
201
+
Alternatively, use `tsx/cjs` which doesn't require explicit extensions.
0 commit comments