codeceptjs 4.0.0-beta.15 → 4.0.0-beta.17

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.
@@ -41,7 +41,37 @@ export async function transpileTypeScript(mainFilePath, typescript) {
41
41
  // This ensures dynamic require() calls work with relative paths from the original file location
42
42
  const originalFileUrl = `file://${filePath.replace(/\\/g, '/')}`
43
43
  esmGlobals += `import { createRequire } from 'module';
44
- const require = createRequire('${originalFileUrl}');
44
+ import { extname as __extname } from 'path';
45
+ const __baseRequire = createRequire('${originalFileUrl}');
46
+
47
+ // Wrap require to auto-resolve extensions (mimics CommonJS behavior)
48
+ const require = (id) => {
49
+ try {
50
+ return __baseRequire(id);
51
+ } catch (err) {
52
+ // If module not found and it's a relative/absolute path without extension, try common extensions
53
+ if (err.code === 'MODULE_NOT_FOUND' && (id.startsWith('./') || id.startsWith('../') || id.startsWith('/'))) {
54
+ const ext = __extname(id);
55
+ // Only treat known file extensions as real extensions (so names like .TEST don't block probing)
56
+ const __knownExts = ['.js', '.cjs', '.mjs', '.json', '.node'];
57
+ const hasKnownExt = ext && __knownExts.includes(ext.toLowerCase());
58
+ if (!hasKnownExt) {
59
+ // Try common extensions in order: .js, .cjs, .json, .node
60
+ const extensions = ['.js', '.cjs', '.json', '.node'];
61
+ for (const testExt of extensions) {
62
+ try {
63
+ return __baseRequire(id + testExt);
64
+ } catch (e) {
65
+ // Continue to next extension
66
+ }
67
+ }
68
+ }
69
+ }
70
+ // Re-throw original error if all attempts failed
71
+ throw err;
72
+ }
73
+ };
74
+
45
75
  const module = { exports: {} };
46
76
  const exports = module.exports;
47
77
 
@@ -74,97 +104,114 @@ const __dirname = __dirname_fn(__filename);
74
104
  const transpiledFiles = new Map()
75
105
  const baseDir = path.dirname(mainFilePath)
76
106
 
77
- // Transpile main file
78
- let jsContent = transpileTS(mainFilePath)
79
-
80
- // Find and transpile all relative TypeScript imports
81
- // Match: import ... from './file' or '../file' or './file.ts'
82
- const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
83
- let match
84
- const imports = []
85
-
86
- while ((match = importRegex.exec(jsContent)) !== null) {
87
- imports.push(match[1])
88
- }
89
-
90
- // Transpile each imported TypeScript file
91
- for (const relativeImport of imports) {
92
- let importedPath = path.resolve(baseDir, relativeImport)
93
-
94
- // Handle .js extensions that might actually be .ts files
95
- if (importedPath.endsWith('.js')) {
96
- const tsVersion = importedPath.replace(/\.js$/, '.ts')
97
- if (fs.existsSync(tsVersion)) {
98
- importedPath = tsVersion
99
- }
107
+ // Recursive function to transpile a file and all its TypeScript dependencies
108
+ const transpileFileAndDeps = (filePath) => {
109
+ // Already transpiled, skip
110
+ if (transpiledFiles.has(filePath)) {
111
+ return
100
112
  }
101
113
 
102
- // Try adding .ts extension if file doesn't exist and no extension provided
103
- if (!path.extname(importedPath)) {
104
- const tsPath = importedPath + '.ts'
105
- if (fs.existsSync(tsPath)) {
106
- importedPath = tsPath
107
- } else {
108
- // Try .js extension as well
109
- const jsPath = importedPath + '.js'
110
- if (fs.existsSync(jsPath)) {
111
- // Skip .js files, they don't need transpilation
112
- continue
113
- }
114
- }
115
- }
114
+ // Transpile this file
115
+ let jsContent = transpileTS(filePath)
116
+
117
+ // Find all relative TypeScript imports in this file
118
+ const importRegex = /from\s+['"](\..+?)(?:\.ts)?['"]/g
119
+ let match
120
+ const imports = []
116
121
 
117
- // If it's a TypeScript file, transpile it
118
- if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
119
- const transpiledImportContent = transpileTS(importedPath)
120
- const tempImportFile = importedPath.replace(/\.ts$/, '.temp.mjs')
121
- fs.writeFileSync(tempImportFile, transpiledImportContent)
122
- transpiledFiles.set(importedPath, tempImportFile)
122
+ while ((match = importRegex.exec(jsContent)) !== null) {
123
+ imports.push(match[1])
123
124
  }
124
- }
125
-
126
- // Replace imports in the main file to point to temp .mjs files
127
- jsContent = jsContent.replace(
128
- /from\s+['"](\..+?)(?:\.ts)?['"]/g,
129
- (match, importPath) => {
130
- let resolvedPath = path.resolve(baseDir, importPath)
125
+
126
+ // Get the base directory for this file
127
+ const fileBaseDir = path.dirname(filePath)
128
+
129
+ // Recursively transpile each imported TypeScript file
130
+ for (const relativeImport of imports) {
131
+ let importedPath = path.resolve(fileBaseDir, relativeImport)
131
132
 
132
- // Handle .js extension that might be .ts
133
- if (resolvedPath.endsWith('.js')) {
134
- const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
135
- if (transpiledFiles.has(tsVersion)) {
136
- const tempFile = transpiledFiles.get(tsVersion)
137
- const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
138
- return `from './${relPath}'`
133
+ // Handle .js extensions that might actually be .ts files
134
+ if (importedPath.endsWith('.js')) {
135
+ const tsVersion = importedPath.replace(/\.js$/, '.ts')
136
+ if (fs.existsSync(tsVersion)) {
137
+ importedPath = tsVersion
139
138
  }
140
139
  }
141
140
 
142
- // Try with .ts extension
143
- const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
144
-
145
- // If we transpiled this file, use the temp file
146
- if (transpiledFiles.has(tsPath)) {
147
- const tempFile = transpiledFiles.get(tsPath)
148
- // Get relative path from main temp file to this temp file
149
- const relPath = path.relative(baseDir, tempFile).replace(/\\/g, '/')
150
- // Ensure the path starts with ./
151
- if (!relPath.startsWith('.')) {
152
- return `from './${relPath}'`
141
+ // Try adding .ts extension if file doesn't exist and no extension provided
142
+ if (!path.extname(importedPath)) {
143
+ const tsPath = importedPath + '.ts'
144
+ if (fs.existsSync(tsPath)) {
145
+ importedPath = tsPath
146
+ } else {
147
+ // Try .js extension as well
148
+ const jsPath = importedPath + '.js'
149
+ if (fs.existsSync(jsPath)) {
150
+ // Skip .js files, they don't need transpilation
151
+ continue
152
+ }
153
153
  }
154
- return `from '${relPath}'`
155
154
  }
156
155
 
157
- // Otherwise, keep the import as-is
158
- return match
156
+ // If it's a TypeScript file, recursively transpile it and its dependencies
157
+ if (importedPath.endsWith('.ts') && fs.existsSync(importedPath)) {
158
+ transpileFileAndDeps(importedPath)
159
+ }
159
160
  }
160
- )
161
-
162
- // Create a temporary JS file with .mjs extension for the main file
163
- const tempJsFile = mainFilePath.replace(/\.ts$/, '.temp.mjs')
164
- fs.writeFileSync(tempJsFile, jsContent)
161
+
162
+ // After all dependencies are transpiled, rewrite imports in this file
163
+ jsContent = jsContent.replace(
164
+ /from\s+['"](\..+?)(?:\.ts)?['"]/g,
165
+ (match, importPath) => {
166
+ let resolvedPath = path.resolve(fileBaseDir, importPath)
167
+
168
+ // Handle .js extension that might be .ts
169
+ if (resolvedPath.endsWith('.js')) {
170
+ const tsVersion = resolvedPath.replace(/\.js$/, '.ts')
171
+ if (transpiledFiles.has(tsVersion)) {
172
+ const tempFile = transpiledFiles.get(tsVersion)
173
+ const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
174
+ // Ensure the path starts with ./
175
+ if (!relPath.startsWith('.')) {
176
+ return `from './${relPath}'`
177
+ }
178
+ return `from '${relPath}'`
179
+ }
180
+ }
181
+
182
+ // Try with .ts extension
183
+ const tsPath = resolvedPath.endsWith('.ts') ? resolvedPath : resolvedPath + '.ts'
184
+
185
+ // If we transpiled this file, use the temp file
186
+ if (transpiledFiles.has(tsPath)) {
187
+ const tempFile = transpiledFiles.get(tsPath)
188
+ const relPath = path.relative(fileBaseDir, tempFile).replace(/\\/g, '/')
189
+ // Ensure the path starts with ./
190
+ if (!relPath.startsWith('.')) {
191
+ return `from './${relPath}'`
192
+ }
193
+ return `from '${relPath}'`
194
+ }
195
+
196
+ // Otherwise, keep the import as-is
197
+ return match
198
+ }
199
+ )
200
+
201
+ // Write the transpiled file with updated imports
202
+ const tempFile = filePath.replace(/\.ts$/, '.temp.mjs')
203
+ fs.writeFileSync(tempFile, jsContent)
204
+ transpiledFiles.set(filePath, tempFile)
205
+ }
206
+
207
+ // Start recursive transpilation from the main file
208
+ transpileFileAndDeps(mainFilePath)
209
+
210
+ // Get the main transpiled file
211
+ const tempJsFile = transpiledFiles.get(mainFilePath)
165
212
 
166
213
  // Store all temp files for cleanup
167
- const allTempFiles = [tempJsFile, ...Array.from(transpiledFiles.values())]
214
+ const allTempFiles = Array.from(transpiledFiles.values())
168
215
 
169
216
  return { tempFile: tempJsFile, allTempFiles }
170
217
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codeceptjs",
3
- "version": "4.0.0-beta.15",
3
+ "version": "4.0.0-beta.17",
4
4
  "type": "module",
5
5
  "description": "Supercharged End 2 End Testing Framework for NodeJS",
6
6
  "keywords": [