Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update config to support default definition function #2384

Merged
17 changes: 15 additions & 2 deletions src/configuration/from_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ export async function fromFile(
file: string,
profiles: string[] = []
): Promise<Partial<IConfiguration>> {
const definitions = await loadFile(logger, cwd, file)
if (!definitions.default) {
let definitions = await loadFile(logger, cwd, file)

const defaultDefinition: unknown = definitions.default

if (defaultDefinition) {
if (typeof defaultDefinition === 'function') {
const definitionsFromDefault = await defaultDefinition()

definitions = {
...definitions,
default: {},
...definitionsFromDefault,
}
}
} else {
logger.debug('No default profile defined in configuration file')
definitions.default = {}
}
Expand Down
23 changes: 23 additions & 0 deletions src/configuration/from_file_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ describe('fromFile', () => {
expect(result).to.deep.eq({ paths: ['other/path/*.feature'] })
})

it('should work with .mjs with default function', async () => {
const { logger, cwd } = await setup(
'cucumber.mjs',
`export default async function() {
return {
default: { paths: ['default/path/*.feature'] },
p1: { paths: ['p1/path/*.feature'] }
};
};
export const p1 = { paths: ['other/p1/path/*.feature'] };
export const p2 = { paths: ['p2/path/*.feature'] };`
)

const p1Result = await fromFile(logger, cwd, 'cucumber.mjs', ['p1'])
const p2Result = await fromFile(logger, cwd, 'cucumber.mjs', ['p2'])
const defaultResult = await fromFile(logger, cwd, 'cucumber.mjs', [
'default',
])
expect(p1Result).to.deep.eq({ paths: ['p1/path/*.feature'] })
expect(p2Result).to.deep.eq({ paths: ['p2/path/*.feature'] })
expect(defaultResult).to.deep.eq({ paths: ['default/path/*.feature'] })
})

it('should work with .cjs', async () => {
const { logger, cwd } = await setup(
'cucumber.cjs',
Expand Down