Skip to content

Commit

Permalink
fall back to require where file doesnt have a native js extension (#1819
Browse files Browse the repository at this point in the history
)

Co-authored-by: Aslak Hellesøy <1000+aslakhellesoy@users.noreply.github.com>
  • Loading branch information
davidjgoss and aslakhellesoy authored Oct 18, 2021
1 parent 31570a8 commit fd540c0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/cli/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand Down
11 changes: 11 additions & 0 deletions src/cli/helpers_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from 'chai'
import {
emitMetaMessage,
emitSupportCodeMessages,
isJavaScript,
parseGherkinMessageStream,
} from './helpers'
import { EventEmitter } from 'events'
Expand Down Expand Up @@ -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[] = []
Expand Down
3 changes: 2 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
emitMetaMessage,
emitSupportCodeMessages,
getExpandedArgv,
isJavaScript,
parseGherkinMessageStream,
} from './helpers'
import { validateInstall } from './install_validator'
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/parallel/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit fd540c0

Please sign in to comment.