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 1/4] 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) { From e203476cd25b5672456e0c6f723f53b4335803e7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 2 Mar 2022 09:55:01 +0100 Subject: [PATCH 2/4] tweaks --- e2e/__tests__/clearFSAndTransformCache.test.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/e2e/__tests__/clearFSAndTransformCache.test.ts b/e2e/__tests__/clearFSAndTransformCache.test.ts index b8d4cdba6ccb..7576272191bb 100644 --- a/e2e/__tests__/clearFSAndTransformCache.test.ts +++ b/e2e/__tests__/clearFSAndTransformCache.test.ts @@ -1,14 +1,16 @@ +import {tmpdir} from 'os'; 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 dir = path.resolve(tmpdir(), 'clear_FS_and_transform_cache'); const testFileContent = ` +'use strict'; + const fs = require('fs'); const path = require('path'); -const asboulteTestHelperFile = path.resolve(__dirname, './testHelper.js'); +const absoluteTestHelperFile = path.resolve(__dirname, './testHelper.js'); test('value is 1', () => { const value = require('./testHelper'); @@ -16,7 +18,7 @@ test('value is 1', () => { }); test('value is 1 after file is changed', () => { - fs.writeFileSync(asboulteTestHelperFile, 'module.exports = 2;'); + fs.writeFileSync(absoluteTestHelperFile, 'module.exports = 2;'); const value = require('./testHelper'); expect(value).toBe(1); }); @@ -26,9 +28,9 @@ test('value is 2 after calling "jest.resetModules"', () => { const value = require('./testHelper'); expect(value).toBe(2); }); - `; +beforeAll(() => cleanup(dir)); afterEach(() => cleanup(dir)); test('clear FS and transform cache', () => { @@ -37,6 +39,6 @@ test('clear FS and transform cache', () => { 'test.js': testFileContent, 'testHelper.js': 'module.exports = 1;', }); - const {exitCode} = runJest(dirName); + const {exitCode} = runJest(dir); expect(exitCode).toBe(0); }); From 09cb8ab6218446d56e098b2e0d6cc7b17061b3f5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 2 Mar 2022 09:55:17 +0100 Subject: [PATCH 3/4] add copyright notice --- e2e/__tests__/clearFSAndTransformCache.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/e2e/__tests__/clearFSAndTransformCache.test.ts b/e2e/__tests__/clearFSAndTransformCache.test.ts index 7576272191bb..09ac64998162 100644 --- a/e2e/__tests__/clearFSAndTransformCache.test.ts +++ b/e2e/__tests__/clearFSAndTransformCache.test.ts @@ -1,3 +1,10 @@ +/** + * 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. + */ + import {tmpdir} from 'os'; import * as path from 'path'; import {cleanup, writeFiles} from '../Utils'; From d7118a186e694172980cb51822a56715e90d588e Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 2 Mar 2022 09:56:11 +0100 Subject: [PATCH 4/4] flip lifecycles --- e2e/__tests__/clearFSAndTransformCache.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/__tests__/clearFSAndTransformCache.test.ts b/e2e/__tests__/clearFSAndTransformCache.test.ts index 09ac64998162..4d35bcf75804 100644 --- a/e2e/__tests__/clearFSAndTransformCache.test.ts +++ b/e2e/__tests__/clearFSAndTransformCache.test.ts @@ -37,8 +37,8 @@ test('value is 2 after calling "jest.resetModules"', () => { }); `; -beforeAll(() => cleanup(dir)); -afterEach(() => cleanup(dir)); +beforeEach(() => cleanup(dir)); +afterAll(() => cleanup(dir)); test('clear FS and transform cache', () => { writeFiles(dir, {