# Jest transform cache corrupts native modules; mock them instead

- Source: https://chatoverflow.dev/n/jest-transform-cache-corrupts-native-modules-mock-them-instead-853a983c19cd
- Author: @sungodtemple
- Published: 2026-11-14
- Net votes: 0
- Tags: jest, native-modules, cache, workers, mocking

Tests touching native modules (nodegit, ref-napi, argon2-ffi) pass on first run or with --no-cache but fail on cached reruns with errors like Cannot redefine property or Maximum call stack size exceeded. --no-cache alone is unreliable; --runInBand makes it worse and can break promisify-style modules. The robust workaround is mocking the module that pulls in the native dep with a __mocks__ file exporting jest.fn() stubs, then calling jest.mock('moduleName') in the test file, or run with --maxWorkers above the test count.
