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
9 changes: 7 additions & 2 deletions lib/command/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,13 @@ function getImportString(testsPath, targetFolderPath, pathsToType, pathsToValue)
}

for (const name in pathsToValue) {
const relativePath = getPath(pathsToValue[name], targetFolderPath, testsPath)
importStrings.push(`type ${name} = import('${relativePath}');`)
const originalPath = pathsToValue[name]
const relativePath = getPath(originalPath, targetFolderPath, testsPath)
if (originalPath.endsWith('.js') || originalPath.endsWith('.ts')) {
importStrings.push(`type ${name} = InstanceType<typeof import('${relativePath}').default>;`)
} else {
importStrings.push(`type ${name} = import('${relativePath}');`)
}
}

return importStrings
Expand Down
7 changes: 7 additions & 0 deletions test/data/sandbox/configs/custom-helper-esm/My_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature("My");

const {I} = inject();

Scenario("test something", () => {
I.openPage("http://codecept.io");
});
14 changes: 14 additions & 0 deletions test/data/sandbox/configs/custom-helper-esm/codecept-ts.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const config = {
tests: './*_test.js',
output: './output',
helpers: {
FileSystem: {},
MyHelperTs: {
require: './myhelper_ts.ts',
},
},
include: {},
bootstrap: false,
mocha: {},
name: 'custom-helper-esm-ts',
};
14 changes: 14 additions & 0 deletions test/data/sandbox/configs/custom-helper-esm/codecept.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const config = {
tests: './*_test.js',
output: './output',
helpers: {
FileSystem: {},
MyHelper: {
require: './myhelper_helper.js',
},
},
include: {},
bootstrap: false,
mocha: {},
name: 'custom-helper-esm',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Helper from '../../../../../lib/helper.js';

class MyHelper extends Helper {
openPage(url) {
return this.helpers.FileSystem.amInPath(url);
}
}

export default MyHelper;
9 changes: 9 additions & 0 deletions test/data/sandbox/configs/custom-helper-esm/myhelper_ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Helper from '../../../../../lib/helper.js';

class MyHelperTs extends Helper {
openPageTs(url: string): Promise<void> {
return this.helpers.FileSystem.amInPath(url);
}
}

export default MyHelperTs;
22 changes: 22 additions & 0 deletions test/runner/definitions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,28 @@ describe('Definitions', function () {
done()
})
})

it('def should create definition file with custom helper using ESM default export', done => {
const customHelperDir = `${codecept_dir}/../custom-helper-esm`
exec(`${runner} def --config ${customHelperDir}/codecept.conf.js`, (err, stdout) => {
stdout.should.include('Definitions were generated in steps.d.ts')
const types = typesFrom(`${customHelperDir}/steps.d.ts`)
types.should.be.valid

const definitionFile = types.getSourceFileOrThrow(`${customHelperDir}/steps.d.ts`)
const fileContent = definitionFile.getFullText()
fileContent.should.include("type MyHelper = InstanceType<typeof import('./myhelper_helper.js').default>;")

const extend = getExtends(definitionFile.getModule('CodeceptJS').getInterfaceOrThrow('I'))
const hasOpenPageMethod = extend.some(ext =>
ext.methods && ext.methods.some(m => m.name === 'openPage')
)
hasOpenPageMethod.should.be.true

assert(!err)
done()
})
})
})

/**
Expand Down