Skip to content

Commit 4ff9a23

Browse files
committed
feat: TypeScript configs now support top-level await with dynamic imports
- Updated transpiler to enable lib.esnext for latest features including top-level await - Users can now use 'await import()' in TypeScript config files - Added test case for top-level await with dynamic environment loading - This allows dynamic loading of .ts or .js files at runtime using import() - Bumped version to 4.0.0-beta.18 Example usage: const environment = await import('./environments/environment.${env}.js'); Note: IDE may show TypeScript warning but code runs correctly.
1 parent 36c9d06 commit 4ff9a23

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed

lib/utils/typescript.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export async function transpileTypeScript(mainFilePath, typescript) {
2525
target: 99, // ScriptTarget.ESNext
2626
esModuleInterop: true,
2727
allowSyntheticDefaultImports: true,
28+
lib: ['lib.esnext.d.ts'], // Enable latest features including top-level await
29+
suppressOutputPathCheck: true,
30+
skipLibCheck: true,
2831
})
2932

3033
// Check if the code uses CommonJS globals
@@ -57,6 +60,7 @@ const require = (id) => {
5760
const hasKnownExt = ext && __knownExts.includes(ext.toLowerCase());
5861
if (!hasKnownExt) {
5962
// Try common extensions in order: .js, .cjs, .json, .node
63+
// Note: .ts files cannot be required - they need transpilation first
6064
const extensions = ['.js', '.cjs', '.json', '.node'];
6165
for (const testExt of extensions) {
6266
try {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codeceptjs",
3-
"version": "4.0.0-beta.17",
3+
"version": "4.0.0-beta.18",
44
"type": "module",
55
"description": "Supercharged End 2 End Testing Framework for NodeJS",
66
"keywords": [
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { E2EEnvironment } from './environments';
2+
3+
// Use top-level await in codecept.conf.ts
4+
const environment = await E2EEnvironment;
5+
6+
export const config = {
7+
tests: './*_test.js',
8+
output: './output',
9+
helpers: {
10+
REST: {
11+
endpoint: environment.url,
12+
timeout: environment.timeout
13+
}
14+
},
15+
name: 'typescript-top-level-await-test'
16+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Test top-level await in TypeScript
2+
let env = process.env.E2E_ENV || "TEST";
3+
4+
if (process.env.PROD === 'true') {
5+
env = 'PROD';
6+
}
7+
8+
async function loadEnvironment() {
9+
const environmentPath = `./environments/environment.${env}.js`;
10+
const environmentModule = await import(environmentPath);
11+
const environment = environmentModule.default || environmentModule;
12+
13+
environment.url = process.env.E2E_URL || environment.url;
14+
15+
if (environment.url.endsWith("/")) {
16+
environment.url = environment.url.slice(0, -1);
17+
}
18+
19+
if (!environment.urlApi) {
20+
environment.urlApi = `${environment.url}/api`;
21+
}
22+
23+
environment.outputDir = `./../reports/e2e/results/`;
24+
environment.env = env;
25+
26+
console.log(`Parsed E2E environment:`, environment);
27+
28+
return environment;
29+
}
30+
31+
export const E2EEnvironment = loadEnvironment();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
url: 'https://test.example.com',
3+
timeout: 10000
4+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "typescript-top-level-await",
3+
"version": "1.0.0",
4+
"type": "module"
5+
}

test/unit/config_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,17 @@ describe('Config', () => {
119119
expect(cfg.name).to.equal('typescript-require-relative-test')
120120
delete process.env.E2E_ENV
121121
})
122+
123+
it('should load TypeScript config with top-level await and dynamic imports', async () => {
124+
const configPath = './test/data/typescript-top-level-await/codecept.conf.ts'
125+
process.env.E2E_ENV = 'TEST'
126+
const cfg = await config.load(configPath)
127+
128+
expect(cfg).to.be.ok
129+
expect(cfg.helpers).to.have.property('REST')
130+
expect(cfg.helpers.REST.endpoint).to.equal('https://test.example.com')
131+
expect(cfg.helpers.REST.timeout).to.equal(10000)
132+
expect(cfg.name).to.equal('typescript-top-level-await-test')
133+
delete process.env.E2E_ENV
134+
})
122135
})

0 commit comments

Comments
 (0)