diff --git a/src/cli/helpers.ts b/src/cli/helpers.ts index 92aa407c9..a0a326399 100644 --- a/src/cli/helpers.ts +++ b/src/cli/helpers.ts @@ -103,6 +103,14 @@ export function orderPickleIds(pickleIds: string[], order: string): void { } } +export function isJavaScript(filePath: string): boolean { + return ( + filePath.endsWith('.js') || + filePath.endsWith('.mjs') || + filePath.endsWith('.cjs') + ) +} + export async function emitMetaMessage( eventBroadcaster: EventEmitter ): Promise { diff --git a/src/cli/helpers_spec.ts b/src/cli/helpers_spec.ts index 4f4e0e640..f4200b536 100644 --- a/src/cli/helpers_spec.ts +++ b/src/cli/helpers_spec.ts @@ -3,6 +3,7 @@ import { expect } from 'chai' import { emitMetaMessage, emitSupportCodeMessages, + isJavaScript, parseGherkinMessageStream, } from './helpers' import { EventEmitter } from 'events' @@ -87,6 +88,16 @@ function testEmitSupportCodeMessages( } describe('helpers', () => { + describe('isJavaScript', () => { + it('should identify a native javascript file path that can be `import()`ed', () => { + expect(isJavaScript('foo/bar.js')).to.be.true() + expect(isJavaScript('foo/bar.mjs')).to.be.true() + expect(isJavaScript('foo/bar.cjs')).to.be.true() + expect(isJavaScript('foo/bar.ts')).to.be.false() + expect(isJavaScript('foo/bar.coffee')).to.be.false() + }) + }) + describe('emitMetaMessage', () => { it('emits a meta message', async () => { const envelopes: messages.Envelope[] = [] diff --git a/src/cli/index.ts b/src/cli/index.ts index bb9a08f30..bd5488e60 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -3,6 +3,7 @@ import { emitMetaMessage, emitSupportCodeMessages, getExpandedArgv, + isJavaScript, parseGherkinMessageStream, } from './helpers' import { validateInstall } from './install_validator' @@ -162,7 +163,7 @@ export default class Cli { supportCodeRequiredModules.map((module) => require(module)) supportCodeLibraryBuilder.reset(this.cwd, newId) for (const codePath of supportCodePaths) { - if (supportCodeRequiredModules.length) { + if (supportCodeRequiredModules.length || !isJavaScript(codePath)) { require(codePath) } else { await importer(pathToFileURL(codePath)) diff --git a/src/runtime/parallel/worker.ts b/src/runtime/parallel/worker.ts index 71065e094..6fd1db60c 100644 --- a/src/runtime/parallel/worker.ts +++ b/src/runtime/parallel/worker.ts @@ -19,6 +19,7 @@ import { IRuntimeOptions } from '../index' import { RealTestRunStopwatch } from '../stopwatch' import { duration } from 'durations' import { pathToFileURL } from 'url' +import { isJavaScript } from '../../cli/helpers' // eslint-disable-next-line @typescript-eslint/no-var-requires const { importer } = require('../../importer') @@ -76,7 +77,7 @@ export default class Worker { supportCodeRequiredModules.map((module) => require(module)) supportCodeLibraryBuilder.reset(this.cwd, this.newId) for (const codePath of supportCodePaths) { - if (supportCodeRequiredModules.length) { + if (supportCodeRequiredModules.length || !isJavaScript(codePath)) { require(codePath) } else { await importer(pathToFileURL(codePath))