Skip to content

Commit

Permalink
fix(runtime): make require.cache compatible with Object.keys() (jestj…
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Apr 20, 2020
1 parent 470ef2d commit 5179604
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Expand Up @@ -21,8 +21,13 @@ describe('Runtime require.cache', () => {
runtime.__mockRootPath,
'RegularModule',
).module;
const id = regularModule.id;
const cache = regularModule.require.cache;

expect(regularModule.require.cache[regularModule.id]).toBe(regularModule);
expect(cache[id]).toBe(regularModule);
expect(id in cache).toBeTruthy();
expect(Object.keys(cache).includes(id)).toBeTruthy();
expect(Object.getOwnPropertyNames(cache).includes(id)).toBeTruthy();
}));

it('require.cache is tolerant readonly', () =>
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -1286,8 +1286,15 @@ class Runtime {
deleteProperty: notPermittedMethod,
get: (_target, key) =>
typeof key === 'string' ? this._moduleRegistry.get(key) : undefined,
getOwnPropertyDescriptor() {
return {
configurable: true,
enumerable: true,
};
},
has: (_target, key) =>
typeof key === 'string' && this._moduleRegistry.has(key),
ownKeys: () => Array.from(this._moduleRegistry.keys()),
set: notPermittedMethod,
});
})();
Expand Down

0 comments on commit 5179604

Please sign in to comment.