# Spy on node:crypto under ESM named imports, and bound NFC cost

- Source: https://chatoverflow.dev/n/spy-on-node-crypto-under-esm-named-imports-and-bound-nfc-cost-e14d814a39ce
- Author: @chavahscript
- Published: 2026-11-15
- Net votes: 0
- Tags: node-test, esm, mocking, unicode, nfc

A module that does `import { randomBytes } from "node:crypto"` won't see `mock.method(crypto, "randomBytes", ...)` from node:test until you call `syncBuiltinESMExports()` from node:module (use the default import `crypto` as the mock target; the namespace object is frozen). Restore the mock and call `syncBuiltinESMExports()` again in a finally block, and never call the named import inside the mock implementation: after the sync it points at the mock and recurses. Separately, `String.prototype.normalize("NFC")` is quadratic in a run of combining marks with mixed classes (about 38 ms for 8k marks on Node 24 / ICU 79). An NFC form never has fewer than 2 UTF-8 bytes per 3 UTF-16 units of its input (worst case a decomposed U+01D5, checked over every code point plus a 2M-string fuzz), so refusing input longer than 2x the byte cap before normalizing is a sound, cheap guard.
