Skip to content

Commit

Permalink
fix: remove deprecated functions from the jest object (#9853)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Nov 4, 2020
1 parent 3079f29 commit 2fa34c4
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 51 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

- `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781))
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
- `[jest-runtime]` [**BREAKING**] Convert to ESM ([#10325](https://github.com/facebook/jest/pull/10325))
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
- `[jest-transform]` Show enhanced `SyntaxError` message for all `SyntaxError`s ([#10749](https://github.com/facebook/jest/pull/10749))
- `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753))

Expand Down
4 changes: 0 additions & 4 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,6 @@ Exhausts all tasks queued by `setImmediate()`.
### `jest.advanceTimersByTime(msToRun)`

##### renamed in Jest **22.0.0+**

Also under the alias: `.runTimersToTime()`

Executes only the macro task queue (i.e. all tasks queued by `setTimeout()` or `setInterval()` and `setImmediate()`).

When this API is called, all timers are advanced by `msToRun` milliseconds. All pending "macro-tasks" that have been queued via `setTimeout()` or `setInterval()`, and would be executed within this time frame will be executed. Additionally, if those macro-tasks schedule new macro-tasks that would be executed within the same time frame, those will be executed until there are no more macro-tasks remaining in the queue, that should be run within `msToRun` milliseconds.
Expand Down
2 changes: 0 additions & 2 deletions docs/TimerMocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ describe('infiniteTimerGame', () => {

## Advance Timers by Time

##### renamed from `runTimersToTime` to `advanceTimersByTime` in Jest **22.0.0**

Another possibility is use `jest.advanceTimersByTime(msToRun)`. When this API is called, all timers are advanced by `msToRun` milliseconds. All pending "macro-tasks" that have been queued via setTimeout() or setInterval(), and would be executed during this time frame, will be executed. Additionally, if those macro-tasks schedule new macro-tasks that would be executed within the same time frame, those will be executed until there are no more macro-tasks remaining in the queue that should be run within msToRun milliseconds.

```javascript
Expand Down
20 changes: 0 additions & 20 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ export type Module = NodeModule;

// TODO: Move to some separate package
export interface Jest {
/**
* Provides a way to add Jasmine-compatible matchers into your Jest context.
*
* @deprecated Use `expect.extend` instead
*/
addMatchers(matchers: Record<string, unknown>): void;
/**
* Advances all timers by the needed milliseconds so that only the next timeouts/intervals will run.
* Optionally, you can provide steps, so it will run steps amount of next timeouts/intervals.
Expand Down Expand Up @@ -183,13 +177,6 @@ export interface Jest {
* Equivalent to calling .mockReset() on every mocked function.
*/
resetAllMocks(): Jest;
/**
* Resets the module registry - the cache of all required modules. This is
* useful to isolate modules where local state might conflict between tests.
*
* @deprecated Use `jest.resetModules()`
*/
resetModuleRegistry(): Jest;
/**
* Resets the module registry - the cache of all required modules. This is
* useful to isolate modules where local state might conflict between tests.
Expand Down Expand Up @@ -237,13 +224,6 @@ export interface Jest {
* executed within this timeframe will be executed.
*/
advanceTimersByTime(msToRun: number): void;
/**
* Executes only the macro task queue (i.e. all tasks queued by setTimeout()
* or setInterval() and setImmediate()).
*
* @deprecated Use `jest.advanceTimersByTime()`
*/
runTimersToTime(msToRun: number): void;
/**
* Returns the number of fake timers still left to run.
*/
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-haste-map/src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('HasteMap', () => {
});

it('creates valid cache file paths', () => {
jest.resetModuleRegistry();
jest.resetModules();
HasteMap = require('../');

expect(
Expand All @@ -237,15 +237,15 @@ describe('HasteMap', () => {
});

it('creates different cache file paths for different roots', () => {
jest.resetModuleRegistry();
jest.resetModules();
const HasteMap = require('../');
const hasteMap1 = new HasteMap({...defaultConfig, rootDir: '/root1'});
const hasteMap2 = new HasteMap({...defaultConfig, rootDir: '/root2'});
expect(hasteMap1.getCacheFilePath()).not.toBe(hasteMap2.getCacheFilePath());
});

it('creates different cache file paths for different dependency extractor cache keys', () => {
jest.resetModuleRegistry();
jest.resetModules();
const HasteMap = require('../');
const dependencyExtractor = require('./dependencyExtractor');
const config = {
Expand All @@ -260,7 +260,7 @@ describe('HasteMap', () => {
});

it('creates different cache file paths for different hasteImplModulePath cache keys', () => {
jest.resetModuleRegistry();
jest.resetModules();
const HasteMap = require('../');
const hasteImpl = require('./haste_impl');
hasteImpl.setCacheKey('foo');
Expand All @@ -271,7 +271,7 @@ describe('HasteMap', () => {
});

it('creates different cache file paths for different projects', () => {
jest.resetModuleRegistry();
jest.resetModules();
const HasteMap = require('../');
const hasteMap1 = new HasteMap({...defaultConfig, name: '@scoped/package'});
const hasteMap2 = new HasteMap({...defaultConfig, name: '-scoped-package'});
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-runtime/src/__tests__/runtime_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Runtime', () => {
const mockReference = {isMock: true};
const root = runtime.requireModule(runtime.__mockRootPath, rootJsPath);
// Erase module registry because root.js requires most other modules.
root.jest.resetModuleRegistry();
root.jest.resetModules();

root.jest.mock('RegularModule', () => mockReference);
root.jest.mock('ManuallyMocked', () => mockReference);
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('Runtime', () => {
const virtual = true;
const root = runtime.requireModule(runtime.__mockRootPath, rootJsPath);
// Erase module registry because root.js requires most other modules.
root.jest.resetModuleRegistry();
root.jest.resetModules();

root.jest.mock('NotInstalledModule', () => mockReference, {virtual});
root.jest.mock('../ManuallyMocked', () => mockReference, {virtual});
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('Runtime', () => {
const virtual = true;
const root = runtime.requireModule(runtime.__mockRootPath, rootJsPath);
// Erase module registry because root.js requires most other modules.
root.jest.resetModuleRegistry();
root.jest.resetModules();

root.jest.mock('NotInstalledModule', () => mockReference, {virtual});
root.jest.mock('../ManuallyMocked', () => mockReference, {virtual});
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Runtime', () => {
const mockReference = {isMock: true};
const root = runtime.requireModule(runtime.__mockRootPath, rootJsPath);
// Erase module registry because root.js requires most other modules.
root.jest.resetModuleRegistry();
root.jest.resetModules();

root.jest.setMock('RegularModule', mockReference);
root.jest.setMock('ManuallyMocked', mockReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('Runtime requireModule', () => {
automock: true,
}).then(runtime => {
const root = runtime.requireModule(runtime.__mockRootPath, './root.js');
root.jest.resetModuleRegistry();
root.jest.resetModules();
root.jest.unmock('ManuallyMocked');
const exports = runtime.requireModule(
runtime.__mockRootPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('transitive dependencies', () => {
);

// Test twice to make sure Runtime caching works properly
root.jest.resetModuleRegistry();
root.jest.resetModules();
expectUnmocked(
runtime.requireModuleOrMock(runtime.__mockRootPath, 'npm3-main-dep'),
);
Expand All @@ -88,7 +88,7 @@ describe('transitive dependencies', () => {
);

// Test twice to make sure Runtime caching works properly
root.jest.resetModuleRegistry();
root.jest.resetModules();
expectUnmocked(
runtime.requireModuleOrMock(runtime.__mockRootPath, 'npm3-main-dep'),
);
Expand All @@ -114,7 +114,7 @@ describe('transitive dependencies', () => {
);

// Test twice to make sure Runtime caching works properly
root.jest.resetModuleRegistry();
root.jest.resetModules();
expectUnmocked(
runtime.requireModuleOrMock(runtime.__mockRootPath, 'npm3-main-dep'),
);
Expand Down
5 changes: 0 additions & 5 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1561,8 +1561,6 @@ export default class Runtime {
};

const jestObject: Jest = {
addMatchers: (matchers: Record<string, unknown>) =>
this._environment.global.jasmine.addMatchers(matchers),
advanceTimersByTime: (msToRun: number) =>
_getFakeTimers().advanceTimersByTime(msToRun),
advanceTimersToNextTimer: (steps?: number) =>
Expand Down Expand Up @@ -1599,7 +1597,6 @@ export default class Runtime {
requireActual: this.requireActual.bind(this, from),
requireMock: this.requireMock.bind(this, from),
resetAllMocks,
resetModuleRegistry: resetModules,
resetModules,
restoreAllMocks,
retryTimes,
Expand All @@ -1617,8 +1614,6 @@ export default class Runtime {
runAllTicks: () => _getFakeTimers().runAllTicks(),
runAllTimers: () => _getFakeTimers().runAllTimers(),
runOnlyPendingTimers: () => _getFakeTimers().runOnlyPendingTimers(),
runTimersToTime: (msToRun: number) =>
_getFakeTimers().advanceTimersByTime(msToRun),
setMock: (moduleName: string, mock: unknown) =>
setMockFactory(moduleName, () => mock),
setSystemTime: (now?: number | Date) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ describe('ScriptTransformer', () => {

// Cache the state in `mockFsCopy`
const mockFsCopy = mockFs;
jest.resetModuleRegistry();
jest.resetModules();
reset();

// Restore the cached fs
Expand All @@ -707,7 +707,7 @@ describe('ScriptTransformer', () => {
expect(writeFileAtomic.sync).not.toBeCalled();

// Don't read from the cache when `config.cache` is false.
jest.resetModuleRegistry();
jest.resetModules();
reset();
mockFs = mockFsCopy;
transformConfig.cache = false;
Expand Down Expand Up @@ -737,7 +737,7 @@ describe('ScriptTransformer', () => {

// Cache the state in `mockFsCopy`
const mockFsCopy = mockFs;
jest.resetModuleRegistry();
jest.resetModules();
reset();

// Restore the cached fs
Expand Down
3 changes: 0 additions & 3 deletions test-types/top-level-jest-namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {expectError, expectType} from 'mlh-tsd';
import {jest} from '@jest/globals';
import type {Mock} from 'jest-mock';

expectType<void>(jest.addMatchers({}));
expectType<typeof jest>(jest.autoMockOff());
expectType<typeof jest>(jest.autoMockOn());
expectType<typeof jest>(jest.clearAllMocks());
Expand All @@ -33,7 +32,6 @@ expectType<typeof jest>(jest.mock('moduleName'));
expectType<typeof jest>(jest.mock('moduleName', jest.fn()));
expectType<typeof jest>(jest.mock('moduleName', jest.fn(), {}));
expectType<typeof jest>(jest.mock('moduleName', jest.fn(), {virtual: true}));
expectType<typeof jest>(jest.resetModuleRegistry());
expectType<typeof jest>(jest.resetModules());
expectType<typeof jest>(jest.isolateModules(() => {}));
expectType<typeof jest>(jest.retryTimes(3));
Expand Down Expand Up @@ -62,7 +60,6 @@ expectType<void>(jest.runAllImmediates());
expectType<void>(jest.runAllTicks());
expectType<void>(jest.runAllTimers());
expectType<void>(jest.runOnlyPendingTimers());
expectType<void>(jest.runTimersToTime(9001));
expectType<void>(jest.advanceTimersByTime(9001));

expectType<typeof jest>(jest.setMock('moduleName', {}));
Expand Down

0 comments on commit 2fa34c4

Please sign in to comment.