Skip to content

Commit 2cf086c

Browse files
committed
refactor(core): move ESM loader hooks to CLI-only path
The jest-runner instruments code through Jest's own transformer pipeline and monkey-patches built-ins at runtime — it never uses the ESM loader hooks. Extracted registerEsmLoaderHooks from registerInstrumentor, called only from startFuzzing (the CLI entry point). hookRequire stays in registerInstrumentor because custom hooks loaded via import() in initFuzzing still need coverage instrumentation through require.extensions.
1 parent a8db2b2 commit 2cf086c

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/core/core.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
FileSyncIdStrategy,
3030
Instrumentor,
3131
MemorySyncIdStrategy,
32+
registerEsmLoaderHooks,
3233
registerInstrumentor,
3334
} from "@jazzer.js/instrumentor";
3435

@@ -189,7 +190,9 @@ export async function startFuzzing(
189190
options: OptionsManager,
190191
): Promise<FuzzingResult> {
191192
registerGlobals(options);
192-
await initFuzzing(options);
193+
const instrumentor = await initFuzzing(options);
194+
registerEsmLoaderHooks(instrumentor);
195+
instrumentor.sendHooksToLoader();
193196
const fuzzFn = await loadFuzzFunction(options);
194197
const findingAwareFuzzFn = asFindingAwareFuzzFn(fuzzFn);
195198
return startFuzzingNoInit(findingAwareFuzzFn, options).finally(() => {

packages/instrumentor/instrument.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,17 @@ export function registerInstrumentor(instrumentor: Instrumentor) {
237237
(code: string, opts: TransformerOptions): string => {
238238
return instrumentor.instrument(code, opts.filename)?.code || code;
239239
},
240-
// required to allow jest to run typescript files
241-
// jest's typescript integration will transform the typescript into javascript before giving it to the
242-
// instrumentor but the filename will still have a .ts extension
243240
{ extensions: [".js", ".mjs", ".cjs", ".ts", ".mts", ".cts"] },
244241
);
242+
}
245243

244+
/**
245+
* Register an ESM loader hook so that import() and static imports are
246+
* instrumented too. Only needed when modules are loaded via Node
247+
* directly (CLI path). The jest-runner uses Jest's own transformer
248+
* pipeline instead and does not need the ESM loader thread.
249+
*/
250+
export function registerEsmLoaderHooks(instrumentor: Instrumentor) {
246251
registerEsmHooks(instrumentor);
247252
}
248253

0 commit comments

Comments
 (0)