Skip to content

Commit fd540c0

Browse files
fall back to require where file doesnt have a native js extension (#1819)
Co-authored-by: Aslak Hellesøy <[email protected]>
1 parent 31570a8 commit fd540c0

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/cli/helpers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ export function orderPickleIds(pickleIds: string[], order: string): void {
103103
}
104104
}
105105

106+
export function isJavaScript(filePath: string): boolean {
107+
return (
108+
filePath.endsWith('.js') ||
109+
filePath.endsWith('.mjs') ||
110+
filePath.endsWith('.cjs')
111+
)
112+
}
113+
106114
export async function emitMetaMessage(
107115
eventBroadcaster: EventEmitter
108116
): Promise<void> {

src/cli/helpers_spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { expect } from 'chai'
33
import {
44
emitMetaMessage,
55
emitSupportCodeMessages,
6+
isJavaScript,
67
parseGherkinMessageStream,
78
} from './helpers'
89
import { EventEmitter } from 'events'
@@ -87,6 +88,16 @@ function testEmitSupportCodeMessages(
8788
}
8889

8990
describe('helpers', () => {
91+
describe('isJavaScript', () => {
92+
it('should identify a native javascript file path that can be `import()`ed', () => {
93+
expect(isJavaScript('foo/bar.js')).to.be.true()
94+
expect(isJavaScript('foo/bar.mjs')).to.be.true()
95+
expect(isJavaScript('foo/bar.cjs')).to.be.true()
96+
expect(isJavaScript('foo/bar.ts')).to.be.false()
97+
expect(isJavaScript('foo/bar.coffee')).to.be.false()
98+
})
99+
})
100+
90101
describe('emitMetaMessage', () => {
91102
it('emits a meta message', async () => {
92103
const envelopes: messages.Envelope[] = []

src/cli/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
emitMetaMessage,
44
emitSupportCodeMessages,
55
getExpandedArgv,
6+
isJavaScript,
67
parseGherkinMessageStream,
78
} from './helpers'
89
import { validateInstall } from './install_validator'
@@ -162,7 +163,7 @@ export default class Cli {
162163
supportCodeRequiredModules.map((module) => require(module))
163164
supportCodeLibraryBuilder.reset(this.cwd, newId)
164165
for (const codePath of supportCodePaths) {
165-
if (supportCodeRequiredModules.length) {
166+
if (supportCodeRequiredModules.length || !isJavaScript(codePath)) {
166167
require(codePath)
167168
} else {
168169
await importer(pathToFileURL(codePath))

src/runtime/parallel/worker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { IRuntimeOptions } from '../index'
1919
import { RealTestRunStopwatch } from '../stopwatch'
2020
import { duration } from 'durations'
2121
import { pathToFileURL } from 'url'
22+
import { isJavaScript } from '../../cli/helpers'
2223

2324
// eslint-disable-next-line @typescript-eslint/no-var-requires
2425
const { importer } = require('../../importer')
@@ -76,7 +77,7 @@ export default class Worker {
7677
supportCodeRequiredModules.map((module) => require(module))
7778
supportCodeLibraryBuilder.reset(this.cwd, this.newId)
7879
for (const codePath of supportCodePaths) {
79-
if (supportCodeRequiredModules.length) {
80+
if (supportCodeRequiredModules.length || !isJavaScript(codePath)) {
8081
require(codePath)
8182
} else {
8283
await importer(pathToFileURL(codePath))

0 commit comments

Comments
 (0)