Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-runtime): calling jest.resetModules function will clear FS and transform cache #12531

Merged
merged 4 commits into from Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions e2e/__tests__/clearFSAndTransformCache.test.ts
@@ -0,0 +1,51 @@
/**
* 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';
import runJest from '../runJest';

const dir = path.resolve(tmpdir(), 'clear_FS_and_transform_cache');
const testFileContent = `
'use strict';

const fs = require('fs');
const path = require('path');

const absoluteTestHelperFile = 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(absoluteTestHelperFile, '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);
});
`;

beforeEach(() => cleanup(dir));
afterAll(() => 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(dir);
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();
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved

if (this._environment) {
if (this._environment.global) {
Expand Down