Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/command/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,12 @@ function getImportString(testsPath, targetFolderPath, pathsToType, pathsToValue)
const importStrings = []

for (const name in pathsToType) {
const relativePath = getPath(pathsToType[name], targetFolderPath, testsPath)
// For ESM modules with default exports, we need to access the default export type
if (relativePath.endsWith('.js')) {
importStrings.push(`type ${name} = typeof import('${relativePath}')['default'];`)
const originalPath = pathsToType[name]
const relativePath = getPath(originalPath, targetFolderPath, testsPath)
// For .js files with plain object exports, access .default to allow TypeScript to extract properties
// For .ts files, the default export is handled differently by TypeScript
if (originalPath.endsWith('.js')) {
importStrings.push(`type ${name} = typeof import('${relativePath}').default;`)
} else {
importStrings.push(`type ${name} = typeof import('${relativePath}');`)
}
Expand Down
4 changes: 3 additions & 1 deletion test/runner/definitions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ describe('Definitions', function () {
const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`)
const extend = definitionFile.getFullText()

extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js')['default'];")
// Page objects are exported as plain objects in .js files
// Access .default to allow TypeScript to extract properties for autocompletion
extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js').default;")
assert(!err)
done()
})
Expand Down