From d2b0b6131f2ed87f2b81ae227110691965705221 Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Wed, 2 Mar 2022 11:31:02 +0800 Subject: [PATCH] feat(jest-runtime): calling jest.resetModules function will clear require cache --- CHANGELOG.md | 2 +- e2e/__tests__/clearRequireCache.test.ts | 6 ++++++ e2e/clear-require-cache/package.json | 5 +++++ e2e/clear-require-cache/test.js | 25 +++++++++++++++++++++++++ e2e/clear-require-cache/testHelper.js | 1 + packages/jest-runtime/src/index.ts | 2 ++ 6 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 e2e/__tests__/clearRequireCache.test.ts create mode 100644 e2e/clear-require-cache/package.json create mode 100644 e2e/clear-require-cache/test.js create mode 100644 e2e/clear-require-cache/testHelper.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eea5ff29d01..79c748e6b2dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## main ### Features - +- `[jest-runtime]` Calling jest.resetModules function will clear require cache ([#12531](https://github.com/facebook/jest/pull/12531)) - `[babel-jest]` Export `createTransformer` function ([#12399](https://github.com/facebook/jest/pull/12399)) - `[expect]` Expose `AsymmetricMatchers`, `MatcherFunction` and `MatcherFunctionWithState` interfaces ([#12363](https://github.com/facebook/jest/pull/12363), [#12376](https://github.com/facebook/jest/pull/12376)) - `[jest-circus, jest-jasmine2]` Allowed classes and functions as `describe` and `it`/`test` names ([#12484](https://github.com/facebook/jest/pull/12484)) diff --git a/e2e/__tests__/clearRequireCache.test.ts b/e2e/__tests__/clearRequireCache.test.ts new file mode 100644 index 000000000000..9f0b0d11ca8e --- /dev/null +++ b/e2e/__tests__/clearRequireCache.test.ts @@ -0,0 +1,6 @@ +import runJest from '../runJest'; + +test('clear require cache', () => { + const {exitCode} = runJest('clear-require-cache'); + expect(exitCode).toBe(0); +}); diff --git a/e2e/clear-require-cache/package.json b/e2e/clear-require-cache/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/clear-require-cache/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/e2e/clear-require-cache/test.js b/e2e/clear-require-cache/test.js new file mode 100644 index 000000000000..0c1246f0fff5 --- /dev/null +++ b/e2e/clear-require-cache/test.js @@ -0,0 +1,25 @@ +const fs = require('fs'); +const path = require('path'); + +const asboulteTestHelperFile = path.resolve(__dirname, './testHelper.js'); + +beforeAll(() => { + fs.writeFileSync(asboulteTestHelperFile, 'module.exports = 1;\n'); +}); + +test('value is 1', () => { + const value = require('./testHelper'); + expect(value).toBe(1); +}); + +test('value is 1 after file change', () => { + fs.writeFileSync(asboulteTestHelperFile, 'module.exports = 2;\n'); + const value = require('./testHelper'); + expect(value).toBe(1); +}); + +test('value is 2', () => { + jest.resetModules(); + const value = require('./testHelper'); + expect(value).toBe(2); +}); diff --git a/e2e/clear-require-cache/testHelper.js b/e2e/clear-require-cache/testHelper.js new file mode 100644 index 000000000000..4bbffde10442 --- /dev/null +++ b/e2e/clear-require-cache/testHelper.js @@ -0,0 +1 @@ +module.exports = 2; diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f3800ee66167..7fc8a91f6af3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -1141,6 +1141,8 @@ export default class Runtime { this._esmoduleRegistry.clear(); this._cjsNamedExports.clear(); this._moduleMockRegistry.clear(); + this._cacheFS.clear(); + this._fileTransforms.clear(); if (this._environment) { if (this._environment.global) {