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
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);
}));
});
32 changes: 31 additions & 1 deletion packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,19 +1238,49 @@ class Runtime {
resolve.paths = (moduleName: string) =>
this._requireResolvePaths(from.filename, moduleName);

const moduleRegistry = this._moduleRegistry;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
const esmoduleRegistry = this._esmoduleRegistry;
const moduleRequire = (options && options.isInternalModule
? (moduleName: string) =>
this.requireInternalModule(from.filename, moduleName)
: this.requireModuleOrMock.bind(
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;

Object.defineProperty(moduleRequire, 'cache', {
enumerable: true,
get() {
const cache: {[key: string]: any} = {};
SimenB marked this conversation as resolved.
Show resolved Hide resolved
const notPermittedMethod = () => {
console.warn('`require.cache` modification is not permitted');
return true;
};
moduleRegistry.forEach((value, key) => {
cache[key] = value;
});
esmoduleRegistry.forEach((value, key) => {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
cache[key] = value;
});

return new Proxy(cache, {
defineProperty: notPermittedMethod,
deleteProperty: notPermittedMethod,
get(target, key) {
return typeof key === 'string' ? target[key] : undefined;
},
has(target, key) {
return typeof key === 'string' && !!target[key];
},
set: notPermittedMethod,
});
},
});

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