node/test/parallel/test-source-map-cjs-require-cache.js
Chengzhong Wu c0c598d753
lib: allow CJS source map cache to be reclaimed
Unifies the CJS and ESM source map cache map with SourceMapCacheMap
and allows the CJS cache entries to be queried more efficiently with
a source url without iteration on an IterableWeakMap.

Add a test to verify that the CJS source map cache entry can be
reclaimed.

PR-URL: https://github.com/nodejs/node/pull/51711
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-05-28 22:56:56 +00:00

36 lines
1.3 KiB
JavaScript

// Flags: --enable-source-maps --max-old-space-size=10 --expose-gc
/**
* This test verifies that the source map of a CJS module is cleared after the
* CJS module is reclaimed by GC.
*/
'use strict';
const common = require('../common');
const assert = require('node:assert');
const { findSourceMap } = require('node:module');
const moduleId = require.resolve('../fixtures/source-map/no-throw.js');
const moduleIdRepeat = require.resolve('../fixtures/source-map/no-throw2.js');
function run(moduleId) {
require(moduleId);
delete require.cache[moduleId];
const idx = module.children.findIndex((child) => child.id === moduleId);
assert.ok(idx >= 0);
module.children.splice(idx, 1);
// Verify that the source map is still available
assert.notStrictEqual(findSourceMap(moduleId), undefined);
}
// Run the test in a function scope so that every variable can be reclaimed by GC.
run(moduleId);
// Run until the source map is cleared by GC, or fail the test after determined iterations.
common.gcUntil('SourceMap of deleted CJS module is cleared', () => {
// Repetitively load a second module with --max-old-space-size=10 to make GC more aggressive.
run(moduleIdRepeat);
// Verify that the source map is cleared.
return findSourceMap(moduleId) == null;
});