codeceptjs 4.0.2-beta.17 → 4.0.2-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config.js +20 -15
- package/lib/utils/typescript.js +65 -17
- package/package.json +1 -1
package/lib/config.js
CHANGED
|
@@ -156,27 +156,32 @@ async function loadConfigFile(configFile) {
|
|
|
156
156
|
try {
|
|
157
157
|
// For .ts files, try to compile and load as JavaScript
|
|
158
158
|
if (extensionName === '.ts') {
|
|
159
|
+
let transpileError = null
|
|
160
|
+
let tempFile = null
|
|
161
|
+
let allTempFiles = null
|
|
162
|
+
let fileMapping = null
|
|
163
|
+
|
|
159
164
|
try {
|
|
160
165
|
// Use the TypeScript transpilation utility
|
|
161
166
|
const typescript = require('typescript')
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
const result = await transpileTypeScript(configFile, typescript)
|
|
168
|
+
tempFile = result.tempFile
|
|
169
|
+
allTempFiles = result.allTempFiles
|
|
170
|
+
fileMapping = result.fileMapping
|
|
171
|
+
|
|
172
|
+
configModule = await import(tempFile)
|
|
173
|
+
cleanupTempFiles(allTempFiles)
|
|
174
|
+
} catch (err) {
|
|
175
|
+
transpileError = err
|
|
176
|
+
if (fileMapping) {
|
|
168
177
|
fixErrorStack(err, fileMapping)
|
|
169
|
-
cleanupTempFiles(allTempFiles)
|
|
170
|
-
throw err
|
|
171
178
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
try {
|
|
175
|
-
require('ts-node/register')
|
|
176
|
-
configModule = require(configFile)
|
|
177
|
-
} catch (tsNodeError) {
|
|
178
|
-
throw new Error(`Failed to load TypeScript config: ${tsError.message}`)
|
|
179
|
+
if (allTempFiles) {
|
|
180
|
+
cleanupTempFiles(allTempFiles)
|
|
179
181
|
}
|
|
182
|
+
// Throw immediately with the actual error - don't fall back to ts-node
|
|
183
|
+
// as it will mask the real error with "Unexpected token 'export'"
|
|
184
|
+
throw err
|
|
180
185
|
}
|
|
181
186
|
} else {
|
|
182
187
|
// Try ESM import first for JS files
|
package/lib/utils/typescript.js
CHANGED
|
@@ -118,20 +118,25 @@ const __dirname = __dirname_fn(__filename);
|
|
|
118
118
|
// Transpile this file
|
|
119
119
|
let jsContent = transpileTS(filePath)
|
|
120
120
|
|
|
121
|
-
// Find all relative TypeScript imports in this file
|
|
121
|
+
// Find all relative TypeScript imports in this file (both ESM imports and require() calls)
|
|
122
122
|
const importRegex = /from\s+['"](\.[^'"]+?)(?:\.ts)?['"]/g
|
|
123
|
+
const requireRegex = /require\s*\(\s*['"](\.[^'"]+?)(?:\.ts)?['"]\s*\)/g
|
|
123
124
|
let match
|
|
124
125
|
const imports = []
|
|
125
126
|
|
|
126
127
|
while ((match = importRegex.exec(jsContent)) !== null) {
|
|
127
|
-
imports.push(match[1])
|
|
128
|
+
imports.push({ path: match[1], type: 'import' })
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
while ((match = requireRegex.exec(jsContent)) !== null) {
|
|
132
|
+
imports.push({ path: match[1], type: 'require' })
|
|
128
133
|
}
|
|
129
134
|
|
|
130
135
|
// Get the base directory for this file
|
|
131
136
|
const fileBaseDir = path.dirname(filePath)
|
|
132
137
|
|
|
133
138
|
// Recursively transpile each imported TypeScript file
|
|
134
|
-
for (const relativeImport of imports) {
|
|
139
|
+
for (const { path: relativeImport } of imports) {
|
|
135
140
|
let importedPath = path.resolve(fileBaseDir, relativeImport)
|
|
136
141
|
|
|
137
142
|
// Handle .js extensions that might actually be .ts files
|
|
@@ -153,11 +158,17 @@ const __dirname = __dirname_fn(__filename);
|
|
|
153
158
|
if (fs.existsSync(tsPath)) {
|
|
154
159
|
importedPath = tsPath
|
|
155
160
|
} else {
|
|
156
|
-
// Try .
|
|
157
|
-
const
|
|
158
|
-
if (fs.existsSync(
|
|
159
|
-
|
|
160
|
-
|
|
161
|
+
// Try index.ts for directory imports
|
|
162
|
+
const indexTsPath = path.join(importedPath, 'index.ts')
|
|
163
|
+
if (fs.existsSync(indexTsPath)) {
|
|
164
|
+
importedPath = indexTsPath
|
|
165
|
+
} else {
|
|
166
|
+
// Try .js extension as well
|
|
167
|
+
const jsPath = importedPath + '.js'
|
|
168
|
+
if (fs.existsSync(jsPath)) {
|
|
169
|
+
// Skip .js files, they don't need transpilation
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
161
172
|
}
|
|
162
173
|
}
|
|
163
174
|
}
|
|
@@ -181,13 +192,11 @@ const __dirname = __dirname_fn(__filename);
|
|
|
181
192
|
if (transpiledFiles.has(tsVersion)) {
|
|
182
193
|
const tempFile = transpiledFiles.get(tsVersion)
|
|
183
194
|
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
184
|
-
// Ensure the path starts with ./
|
|
185
195
|
if (!relPath.startsWith('.')) {
|
|
186
196
|
return `from './${relPath}'`
|
|
187
197
|
}
|
|
188
198
|
return `from '${relPath}'`
|
|
189
199
|
}
|
|
190
|
-
// Keep .js extension as-is (might be a real .js file)
|
|
191
200
|
return match
|
|
192
201
|
}
|
|
193
202
|
|
|
@@ -198,18 +207,24 @@ const __dirname = __dirname_fn(__filename);
|
|
|
198
207
|
if (transpiledFiles.has(tsPath)) {
|
|
199
208
|
const tempFile = transpiledFiles.get(tsPath)
|
|
200
209
|
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
201
|
-
// Ensure the path starts with ./
|
|
202
210
|
if (!relPath.startsWith('.')) {
|
|
203
211
|
return `from './${relPath}'`
|
|
204
212
|
}
|
|
205
213
|
return `from '${relPath}'`
|
|
206
214
|
}
|
|
207
215
|
|
|
208
|
-
//
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
216
|
+
// Try index.ts for directory imports
|
|
217
|
+
const indexTsPath = path.join(resolvedPath, 'index.ts')
|
|
218
|
+
if (transpiledFiles.has(indexTsPath)) {
|
|
219
|
+
const tempFile = transpiledFiles.get(indexTsPath)
|
|
220
|
+
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
221
|
+
if (!relPath.startsWith('.')) {
|
|
222
|
+
return `from './${relPath}'`
|
|
223
|
+
}
|
|
224
|
+
return `from '${relPath}'`
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// If the import doesn't have a standard module extension, add .js for ESM compatibility
|
|
213
228
|
const standardExtensions = ['.js', '.mjs', '.cjs', '.json', '.node']
|
|
214
229
|
const hasStandardExtension = standardExtensions.includes(originalExt.toLowerCase())
|
|
215
230
|
|
|
@@ -217,7 +232,40 @@ const __dirname = __dirname_fn(__filename);
|
|
|
217
232
|
return match.replace(importPath, importPath + '.js')
|
|
218
233
|
}
|
|
219
234
|
|
|
220
|
-
|
|
235
|
+
return match
|
|
236
|
+
}
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
// Also rewrite require() calls to point to transpiled TypeScript files
|
|
240
|
+
jsContent = jsContent.replace(
|
|
241
|
+
/require\s*\(\s*['"](\.[^'"]+?)(?:\.ts)?['"]\s*\)/g,
|
|
242
|
+
(match, requirePath) => {
|
|
243
|
+
let resolvedPath = path.resolve(fileBaseDir, requirePath)
|
|
244
|
+
|
|
245
|
+
// Handle .js extension that might be .ts
|
|
246
|
+
if (resolvedPath.endsWith('.js')) {
|
|
247
|
+
const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
|
|
248
|
+
if (transpiledFiles.has(tsVersion)) {
|
|
249
|
+
const tempFile = transpiledFiles.get(tsVersion)
|
|
250
|
+
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
251
|
+
const finalPath = relPath.startsWith('.') ? relPath : './' + relPath
|
|
252
|
+
return `require('${finalPath}')`
|
|
253
|
+
}
|
|
254
|
+
return match
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Try with .ts extension
|
|
258
|
+
const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
|
|
259
|
+
|
|
260
|
+
// If we transpiled this file, use the temp file
|
|
261
|
+
if (transpiledFiles.has(tsPath)) {
|
|
262
|
+
const tempFile = transpiledFiles.get(tsPath)
|
|
263
|
+
const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
|
|
264
|
+
const finalPath = relPath.startsWith('.') ? relPath : './' + relPath
|
|
265
|
+
return `require('${finalPath}')`
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Otherwise, keep the require as-is
|
|
221
269
|
return match
|
|
222
270
|
}
|
|
223
271
|
)
|