Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(jest-runtime): calling jest.resetModules function will clear FS …
…and transform cache
  • Loading branch information
TrickyPi committed Mar 2, 2022
commit 88dbdc39f9bd8dfc8f3e57b891d1d136e9eb6d5c
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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