node/test/es-module/test-esm-in-require-cache-2.mjs
Joyee Cheung c46260284c
module: only put directly require-d ESM into require.cache
This reduces the impact of
https://redirect.github.com/nodejs/node/pull/59679
by delaying the require.cache population of ESM until they
are directly required. After that, it's necessary for them
to be in the cache to maintain correctness.

PR-URL: https://github.com/nodejs/node/pull/59874
Refs: https://github.com/nodejs/node/issues/59868
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2025-09-17 13:13:59 +00:00

27 lines
1.1 KiB
JavaScript

// This tests the behavior of ESM in require.cache when it's loaded from import.
import '../common/index.mjs';
import assert from 'node:assert';
import * as fixtures from '../common/fixtures.mjs';
const filename = fixtures.path('es-modules', 'esm-in-require-cache', 'esm.mjs');
import { Module } from 'node:module';
// Imported ESM should not be in the require cache.
let { name } = await import('../fixtures/es-modules/esm-in-require-cache/import-esm.mjs');
assert.strictEqual(name, 'esm');
assert(!Module._cache[filename]);
({ name } = await import('../fixtures/es-modules/esm-in-require-cache/esm.mjs'));
assert.strictEqual(name, 'esm');
assert(!Module._cache[filename]);
// Requiring ESM indirectly should not put it in the cache.
({ name } = await import('../fixtures/es-modules/esm-in-require-cache/require-import-esm.cjs'));
assert.strictEqual(name, 'esm');
assert(!Module._cache[filename]);
// After being required directly, it should be in the cache.
({ name } = await import('../fixtures/es-modules/esm-in-require-cache/import-require-esm.mjs'));
assert.strictEqual(name, 'esm');
assert(Module._cache[filename]);