Files
bun.sh/test/js
Dylan Conway a33f58818a fix(test): make mock.restore() restore modules mocked with mock.module()
Previously, mock.restore() only reset function spies (spyOn) but had no
effect on modules mocked with mock.module(). This was because
mock.restore() only called JSMock__resetSpies(), which never touched
the virtualModules map or reversed overrideExportValue patches on ESM
namespace objects.

The fix adds snapshot/restore logic to JSModuleMock:
- Before patching ESM exports, snapshot original values into a plain
  JSObject using WriteBarrier on the existing GC-managed JSModuleMock
- On mock.restore(), iterate virtualModules, restore original export
  values via overrideExportValue, and clear the mock entries
- Preserve true originals across re-mocks of the same specifier

Key discovery: JSModuleNamespaceObject::getOwnPropertyNames() returns 0
exports for Bun's synthetic namespace objects (empty m_names). The fix
uses the mock result object's property names to determine which exports
to snapshot from the namespace.

Closes #7823
2026-01-28 23:47:17 -08:00
..