Skip to content

test_runner: using module mocking confuses all future esm imports to use cjs path resolution #53807

Closed
@ChALkeR

Description

@ChALkeR

Version

v22.4.1

Platform

Darwin Nikitas-Air.lan 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:14:59 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8122 arm64

Subsystem

test_runner

What steps will reproduce the bug?

// Run with and without --experimental-test-module-mocks

import { writeFile } from 'node:fs/promises'
import { mock } from 'node:test'

await writeFile('./test-2.js', "export default 42")

try {
  if (mock.module) mock.module('Whatever, this is not significant', { namedExports: {} })
} catch {}

const { default: x } = await import('./test-2')
console.log(`Found x: ${x}`) // prints 42

Or, alternatively:

// Run with and without --experimental-test-module-mocks

import { writeFile } from 'node:fs/promises'
import { mock } from 'node:test'

await writeFile('./test-1.mjs', "import x from './test-2'; console.log(`Found x: ${x}`)")
await writeFile('./test-2.js', "export default 42")

try {
  if (mock.module) mock.module('Whatever, this is not significant', { namedExports: {} })
} catch {}

await import('./test-1.mjs') // prints 42

Or, alternatively (dir imports):

// Run with and without --experimental-test-module-mocks

import { writeFile, mkdir } from 'node:fs/promises'
import { mock } from 'node:test'

await mkdir('./test-3', { recursive: true })
await writeFile('./test-3/index.js', "export default 43")

try {
  if (mock.module) mock.module('Whatever, this is not significant', { namedExports: {} })
} catch {}

const { default: x } = await import('./test-3')
console.log(`Found x: ${x}`) // prints 43

How often does it reproduce? Is there a required condition?

Always, when run with --experimental-test-module-mocks

What is the expected behavior? Why is that the expected behavior?

Error [ERR_MODULE_NOT_FOUND]: Cannot find module './test-2' imported from ./test-1.mjs
Did you mean to import "./test-2.js"?

What do you see instead?

Found x: 42

Additional information

Likely from #52848, cc @cjihrig, @nodejs/test_runner

Also: long time no see, hi all!

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loaderstest_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions