Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime): populate require.cache #9841

Merged
merged 8 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-runtime]` Populate `require.cache` ([#9841](https://github.com/facebook/jest/pull/9841))

### Fixes

### Chore & Maintenance
Expand Down
41 changes: 41 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_require_cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

let createRuntime;

describe('Runtime require.cache', () => {
beforeEach(() => {
createRuntime = require('createRuntime');
});

it('require.cache returns loaded module list as native Nodejs require does', () =>
createRuntime(__filename).then(runtime => {
const regularModule = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
).module;

expect(regularModule.require.cache[regularModule.id]).toBe(regularModule);
}));

it('require.cache is tolerant readonly', () =>
createRuntime(__filename).then(runtime => {
const regularModule = runtime.requireModule(
runtime.__mockRootPath,
'RegularModule',
).module;

delete regularModule.require.cache[regularModule.id];
expect(regularModule.require.cache[regularModule.id]).toBe(regularModule);

regularModule.require.cache[regularModule.id] = 'something';
expect(regularModule.require.cache[regularModule.id]).toBe(regularModule);
}));
});
18 changes: 17 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type ResolveOptions = Parameters<typeof require.resolve>[1];
type BooleanObject = Record<string, boolean>;
type CacheFS = {[path: string]: string};

type RequireCache = {[key: string]: Module};

namespace Runtime {
export type Context = JestContext;
// ditch this export when moving to esm - for now we need it for to avoid faulty type elision
Expand Down Expand Up @@ -1270,11 +1272,25 @@ class Runtime {
this,
from.filename,
)) as LocalModuleRequire;
moduleRequire.cache = Object.create(null);
moduleRequire.extensions = Object.create(null);
moduleRequire.requireActual = this.requireActual.bind(this, from.filename);
moduleRequire.requireMock = this.requireMock.bind(this, from.filename);
moduleRequire.resolve = resolve;
moduleRequire.cache = (() => {
const notPermittedMethod = () => {
console.warn('`require.cache` modification is not permitted');
return true;
};
return new Proxy<RequireCache>(Object.create(null), {
defineProperty: notPermittedMethod,
deleteProperty: notPermittedMethod,
get: (_target, key) =>
typeof key === 'string' ? this._moduleRegistry.get(key) : undefined,
has: (_target, key) =>
typeof key === 'string' && this._moduleRegistry.has(key),
set: notPermittedMethod,
});
})();

Object.defineProperty(moduleRequire, 'main', {
enumerable: true,
Expand Down