diff --git a/lib/command/definitions.js b/lib/command/definitions.js index 81bc83e02..db76bcde2 100644 --- a/lib/command/definitions.js +++ b/lib/command/definitions.js @@ -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;`) + } else { + importStrings.push(`type ${name} = import('${relativePath}');`) + } } return importStrings diff --git a/test/data/sandbox/configs/custom-helper-esm/My_test.ts b/test/data/sandbox/configs/custom-helper-esm/My_test.ts new file mode 100644 index 000000000..878b66728 --- /dev/null +++ b/test/data/sandbox/configs/custom-helper-esm/My_test.ts @@ -0,0 +1,7 @@ +Feature("My"); + +const {I} = inject(); + +Scenario("test something", () => { + I.openPage("http://codecept.io"); +}); diff --git a/test/data/sandbox/configs/custom-helper-esm/codecept-ts.conf.js b/test/data/sandbox/configs/custom-helper-esm/codecept-ts.conf.js new file mode 100644 index 000000000..97a055620 --- /dev/null +++ b/test/data/sandbox/configs/custom-helper-esm/codecept-ts.conf.js @@ -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', +}; diff --git a/test/data/sandbox/configs/custom-helper-esm/codecept.conf.js b/test/data/sandbox/configs/custom-helper-esm/codecept.conf.js new file mode 100644 index 000000000..99127f023 --- /dev/null +++ b/test/data/sandbox/configs/custom-helper-esm/codecept.conf.js @@ -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', +}; diff --git a/test/data/sandbox/configs/custom-helper-esm/myhelper_helper.js b/test/data/sandbox/configs/custom-helper-esm/myhelper_helper.js new file mode 100644 index 000000000..95c19e1fb --- /dev/null +++ b/test/data/sandbox/configs/custom-helper-esm/myhelper_helper.js @@ -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; diff --git a/test/data/sandbox/configs/custom-helper-esm/myhelper_ts.ts b/test/data/sandbox/configs/custom-helper-esm/myhelper_ts.ts new file mode 100644 index 000000000..d5f13dac7 --- /dev/null +++ b/test/data/sandbox/configs/custom-helper-esm/myhelper_ts.ts @@ -0,0 +1,9 @@ +import Helper from '../../../../../lib/helper.js'; + +class MyHelperTs extends Helper { + openPageTs(url: string): Promise { + return this.helpers.FileSystem.amInPath(url); + } +} + +export default MyHelperTs; diff --git a/test/runner/definitions_test.js b/test/runner/definitions_test.js index 4e06c2cc1..0fd6f25d8 100644 --- a/test/runner/definitions_test.js +++ b/test/runner/definitions_test.js @@ -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;") + + 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() + }) + }) }) /**