Skip to content

Commit 9cca480

Browse files
authored
Add warning for ts-node/esm usage (#5381)
* add warning for ts-node/esm usage * add examples
1 parent 6f949c9 commit 9cca480

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/codecept.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import ActorFactory from './actor.js'
1919
import output from './output.js'
2020
import { emptyFolder } from './utils.js'
2121
import { initCodeceptGlobals } from './globals.js'
22-
import { validateTypeScriptSetup } from './utils/loaderCheck.js'
22+
import { validateTypeScriptSetup, getTSNodeESMWarning } from './utils/loaderCheck.js'
2323
import recorder from './recorder.js'
2424

2525
import storeListener from './listener/store.js'
@@ -270,6 +270,12 @@ class Codecept {
270270
process.exit(1)
271271
}
272272

273+
// Show warning if ts-node/esm is being used
274+
const tsWarning = getTSNodeESMWarning(this.requiringModules || [])
275+
if (tsWarning) {
276+
output.print(output.colors.yellow(tsWarning))
277+
}
278+
273279
// Ensure translations are loaded for Gherkin features
274280
try {
275281
const { loadTranslations } = await import('./mocha/gherkin.js')

lib/utils/loaderCheck.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,34 @@ Note: TypeScript config files (codecept.conf.ts) and helpers are automatically
106106
`
107107
}
108108

109+
/**
110+
* Get warning message if ts-node/esm is being used
111+
* @param {string[]} requiredModules - Array of required modules from config
112+
* @returns {string|null} Warning message or null
113+
*/
114+
export function getTSNodeESMWarning(requiredModules = []) {
115+
if (!requiredModules.includes('ts-node/esm')) {
116+
return null
117+
}
118+
119+
return `
120+
⚠️ Warning: ts-node/esm with "module": "esnext" requires explicit file extensions in all imports.
121+
122+
This is a known limitation. Use tsx/cjs instead to write imports without extensions.
123+
124+
Examples:
125+
126+
❌ Incorrect (will fail):
127+
import loginPage from "./pages/Login";
128+
129+
✅ Correct (must include .ts extension):
130+
import loginPage from "./pages/Login.ts";
131+
132+
📚 Documentation: https://codecept.io/typescript
133+
134+
`
135+
}
136+
109137
/**
110138
* Check if user is trying to run TypeScript tests without proper loader
111139
* @param {string[]} testFiles - Array of test file paths

0 commit comments

Comments
 (0)