From 88dbdc39f9bd8dfc8f3e57b891d1d136e9eb6d5c Mon Sep 17 00:00:00 2001 From: TrickyPi <530257315@qq.com> Date: Wed, 2 Mar 2022 16:45:46 +0800 Subject: [PATCH] feat(jest-runtime): calling jest.resetModules function will clear FS and transform cache --- CHANGELOG.md | 1 + .../clearFSAndTransformCache.test.ts | 42 +++++++++++++++++++ packages/jest-runtime/src/index.ts | 2 + 3 files changed, 45 insertions(+) create mode 100644 e2e/__tests__/clearFSAndTransformCache.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eea5ff29d01..37f90b8d5235 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ - `[jest-runner]` Allow `setupFiles` module to export an async function ([#12042](https://github.com/facebook/jest/pull/12042)) - `[jest-runner]` Allow passing `testEnvironmentOptions` via docblocks ([#12470](https://github.com/facebook/jest/pull/12470)) - `[jest-runtime]` [**BREAKING**] `Runtime.createHasteMap` now returns a promise ([#12008](https://github.com/facebook/jest/pull/12008)) +- `[jest-runtime]` Calling `jest.resetModules` function will clear FS and transform cache ([#12531](https://github.com/facebook/jest/pull/12531)) - `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384)) - `[jest-test-result]` Add duration property to JSON test output ([#12518](https://github.com/facebook/jest/pull/12518)) - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) diff --git a/e2e/__tests__/clearFSAndTransformCache.test.ts b/e2e/__tests__/clearFSAndTransformCache.test.ts new file mode 100644 index 000000000000..b8d4cdba6ccb --- /dev/null +++ b/e2e/__tests__/clearFSAndTransformCache.test.ts @@ -0,0 +1,42 @@ +import * as path from 'path'; +import {cleanup, writeFiles} from '../Utils'; +import runJest from '../runJest'; + +const dirName = 'clear_FS_and_transform_cache'; +const dir = path.resolve(__dirname, `../${dirName}`); +const testFileContent = ` +const fs = require('fs'); +const path = require('path'); + +const asboulteTestHelperFile = path.resolve(__dirname, './testHelper.js'); + +test('value is 1', () => { + const value = require('./testHelper'); + expect(value).toBe(1); +}); + +test('value is 1 after file is changed', () => { + fs.writeFileSync(asboulteTestHelperFile, 'module.exports = 2;'); + const value = require('./testHelper'); + expect(value).toBe(1); +}); + +test('value is 2 after calling "jest.resetModules"', () => { + jest.resetModules(); + const value = require('./testHelper'); + expect(value).toBe(2); +}); + +`; + +afterEach(() => cleanup(dir)); + +test('clear FS and transform cache', () => { + writeFiles(dir, { + 'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}), + 'test.js': testFileContent, + 'testHelper.js': 'module.exports = 1;', + }); + const {exitCode} = runJest(dirName); + expect(exitCode).toBe(0); +}); 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) {