Skip to content

Commit

Permalink
feat(jest-runtime): calling jest.resetModules function will clear req…
Browse files Browse the repository at this point in the history
…uire cache
  • Loading branch information
TrickyPi committed Mar 2, 2022
1 parent a8f55ec commit d2b0b61
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## main

### Features

- `[jest-runtime]` Calling jest.resetModules function will clear require cache ([#12531](https://github.com/facebook/jest/pull/12531))
- `[babel-jest]` Export `createTransformer` function ([#12399](https://github.com/facebook/jest/pull/12399))
- `[expect]` Expose `AsymmetricMatchers`, `MatcherFunction` and `MatcherFunctionWithState` interfaces ([#12363](https://github.com/facebook/jest/pull/12363), [#12376](https://github.com/facebook/jest/pull/12376))
- `[jest-circus, jest-jasmine2]` Allowed classes and functions as `describe` and `it`/`test` names ([#12484](https://github.com/facebook/jest/pull/12484))
Expand Down
6 changes: 6 additions & 0 deletions e2e/__tests__/clearRequireCache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import runJest from '../runJest';

test('clear require cache', () => {
const {exitCode} = runJest('clear-require-cache');
expect(exitCode).toBe(0);
});
5 changes: 5 additions & 0 deletions e2e/clear-require-cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
25 changes: 25 additions & 0 deletions e2e/clear-require-cache/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require('fs');
const path = require('path');

const asboulteTestHelperFile = path.resolve(__dirname, './testHelper.js');

beforeAll(() => {
fs.writeFileSync(asboulteTestHelperFile, 'module.exports = 1;\n');
});

test('value is 1', () => {
const value = require('./testHelper');
expect(value).toBe(1);
});

test('value is 1 after file change', () => {
fs.writeFileSync(asboulteTestHelperFile, 'module.exports = 2;\n');
const value = require('./testHelper');
expect(value).toBe(1);
});

test('value is 2', () => {
jest.resetModules();
const value = require('./testHelper');
expect(value).toBe(2);
});
1 change: 1 addition & 0 deletions e2e/clear-require-cache/testHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 2;
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

0 comments on commit d2b0b61

Please sign in to comment.