Skip to content

Commit

Permalink
feat(jest-runtime): calling jest.resetModules function will clear FS …
Browse files Browse the repository at this point in the history
…and transform cache
  • Loading branch information
TrickyPi committed Mar 2, 2022
1 parent a8f55ec commit 88dbdc3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
42 changes: 42 additions & 0 deletions 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);
});
2 changes: 2 additions & 0 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -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) {
Expand Down

0 comments on commit 88dbdc3

Please sign in to comment.