Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vitest/src/runtime/vm/commonjs-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ export class CommonjsExecutor {

private requireJs = (m: NodeJS.Module, filename: string) => {
const content = this.fs.readFile(filename)
;(m as PrivateNodeModule)._compile(content, filename)
const code = content.startsWith('#!') ? `//${content.slice(2)}` : content
;(m as PrivateNodeModule)._compile(code, filename)
}

private requireJson = (m: NodeJS.Module, filename: string) => {
Expand Down
36 changes: 36 additions & 0 deletions test/e2e/test/vm-threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,42 @@ test.for(['vmThreads', 'vmForks'] as const)(
},
)

test.for(['vmThreads', 'vmForks'] as const)(
'%s loads CommonJS dependencies that start with a hashbang',
async (pool) => {
const { stderr, exitCode } = await runInlineTests(
{
'node_modules/hashbang-cjs/package.json': JSON.stringify({
name: 'hashbang-cjs',
main: './index.cjs',
}),
'node_modules/hashbang-cjs/index.cjs':
'#!/usr/bin/env node\nmodule.exports = { value: "cjs" }\n',
'node_modules/hashbang-js/package.json': JSON.stringify({
name: 'hashbang-js',
main: './index.js',
}),
'node_modules/hashbang-js/index.js':
'#!/usr/bin/env node\nmodule.exports = { value: "js" }\n',
'basic.test.js': `
import { expect, test } from 'vitest'
import cjs from 'hashbang-cjs'
import js from 'hashbang-js'
test('hashbang is ignored', () => {
expect(cjs).toEqual({ value: 'cjs' })
expect(js).toEqual({ value: 'js' })
})
`,
},
{ pool },
)

expect(stderr).toBe('')
expect(exitCode).toBe(0)
},
)

// The module-sync condition was added in Node 22.12/20.19 when require(esm)
// was unflagged. The fix uses the _resolveFilename conditions option which
// is only available on Node 22.12+. Node 20 is unfixable and reaches EOL
Expand Down
Loading