Skip to content

Commit 90d53c2

Browse files
Copilotkobenguyent
andauthored
Fix type definitions for custom helpers with ESM default exports (#5321)
* Initial plan * Initial investigation of custom helper type definition issue Co-authored-by: kobenguyent <[email protected]> * Fix custom helper type definitions for ESM default exports Co-authored-by: kobenguyent <[email protected]> * Remove generated steps.d.ts files from test fixtures * Final verification complete - all tests pass Co-authored-by: kobenguyent <[email protected]> * Remove generated steps.d.ts file from test fixtures Co-authored-by: kobenguyent <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: kobenguyent <[email protected]>
1 parent b53bec7 commit 90d53c2

File tree

7 files changed

+82
-2
lines changed

7 files changed

+82
-2
lines changed

lib/command/definitions.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,13 @@ function getImportString(testsPath, targetFolderPath, pathsToType, pathsToValue)
239239
}
240240

241241
for (const name in pathsToValue) {
242-
const relativePath = getPath(pathsToValue[name], targetFolderPath, testsPath)
243-
importStrings.push(`type ${name} = import('${relativePath}');`)
242+
const originalPath = pathsToValue[name]
243+
const relativePath = getPath(originalPath, targetFolderPath, testsPath)
244+
if (originalPath.endsWith('.js') || originalPath.endsWith('.ts')) {
245+
importStrings.push(`type ${name} = InstanceType<typeof import('${relativePath}').default>;`)
246+
} else {
247+
importStrings.push(`type ${name} = import('${relativePath}');`)
248+
}
244249
}
245250

246251
return importStrings
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Feature("My");
2+
3+
const {I} = inject();
4+
5+
Scenario("test something", () => {
6+
I.openPage("http://codecept.io");
7+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const config = {
2+
tests: './*_test.js',
3+
output: './output',
4+
helpers: {
5+
FileSystem: {},
6+
MyHelperTs: {
7+
require: './myhelper_ts.ts',
8+
},
9+
},
10+
include: {},
11+
bootstrap: false,
12+
mocha: {},
13+
name: 'custom-helper-esm-ts',
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const config = {
2+
tests: './*_test.js',
3+
output: './output',
4+
helpers: {
5+
FileSystem: {},
6+
MyHelper: {
7+
require: './myhelper_helper.js',
8+
},
9+
},
10+
include: {},
11+
bootstrap: false,
12+
mocha: {},
13+
name: 'custom-helper-esm',
14+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Helper from '../../../../../lib/helper.js';
2+
3+
class MyHelper extends Helper {
4+
openPage(url) {
5+
return this.helpers.FileSystem.amInPath(url);
6+
}
7+
}
8+
9+
export default MyHelper;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Helper from '../../../../../lib/helper.js';
2+
3+
class MyHelperTs extends Helper {
4+
openPageTs(url: string): Promise<void> {
5+
return this.helpers.FileSystem.amInPath(url);
6+
}
7+
}
8+
9+
export default MyHelperTs;

test/runner/definitions_test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@ describe('Definitions', function () {
261261
done()
262262
})
263263
})
264+
265+
it('def should create definition file with custom helper using ESM default export', done => {
266+
const customHelperDir = `${codecept_dir}/../custom-helper-esm`
267+
exec(`${runner} def --config ${customHelperDir}/codecept.conf.js`, (err, stdout) => {
268+
stdout.should.include('Definitions were generated in steps.d.ts')
269+
const types = typesFrom(`${customHelperDir}/steps.d.ts`)
270+
types.should.be.valid
271+
272+
const definitionFile = types.getSourceFileOrThrow(`${customHelperDir}/steps.d.ts`)
273+
const fileContent = definitionFile.getFullText()
274+
fileContent.should.include("type MyHelper = InstanceType<typeof import('./myhelper_helper.js').default>;")
275+
276+
const extend = getExtends(definitionFile.getModule('CodeceptJS').getInterfaceOrThrow('I'))
277+
const hasOpenPageMethod = extend.some(ext =>
278+
ext.methods && ext.methods.some(m => m.name === 'openPage')
279+
)
280+
hasOpenPageMethod.should.be.true
281+
282+
assert(!err)
283+
done()
284+
})
285+
})
264286
})
265287

266288
/**

0 commit comments

Comments
 (0)