Skip to content

Commit

Permalink
Update Jest type definitions to v29
Browse files Browse the repository at this point in the history
Summary:
@public

Changelog: [Internal]

TSIA

Reviewed By: jacdebug

Differential Revision: D42570767

fbshipit-source-id: 26deffc1a749ed9403a71dcef64db02d0bf652e8
  • Loading branch information
motiz88 authored and facebook-github-bot committed Jan 18, 2023
1 parent ed55455 commit 1d51ffd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 42 deletions.
106 changes: 69 additions & 37 deletions flow-typed/jest.js
Expand Up @@ -9,12 +9,13 @@
* @oncall react_native
*/

// Copied from https://raw.githubusercontent.com/flow-typed/flow-typed/master/definitions/npm/jest_v26.x.x/flow_v0.134.x-/jest_v26.x.x.js
// Modified from https://raw.githubusercontent.com/flow-typed/flow-typed/master/definitions/npm/jest_v29.x.x/flow_v0.134.x-/jest_v29.x.x.js
// Modifications are explained inline by comments beginning with `// MODIFIED`.

type JestMockFn<
TArguments: $ReadOnlyArray<any> = $ReadOnlyArray<any>,
TReturn = any,
> = {
// MODIFIED: Added ESLint suppression comment - no-unused-vars doesn't understand declaration files
/* eslint-disable no-unused-vars */

type JestMockFn<TArguments: $ReadOnlyArray<any>, TReturn> = {
(...args: TArguments): TReturn,
/**
* An object for introspecting mock calls
Expand All @@ -26,6 +27,12 @@ type JestMockFn<
* passed during the call.
*/
calls: Array<TArguments>,
/**
* An array containing the call arguments of the last call that was made
* to this mock function. If the function was not called, it will return
* undefined.
*/
lastCall: TArguments,
/**
* An array that contains all the object instances that have been
* instantiated from this mock function.
Expand Down Expand Up @@ -174,6 +181,32 @@ type JestPromiseType = {
*/
type JestTestName = string | Function;

type FakeableAPI =
| 'Date'
| 'hrtime'
| 'nextTick'
| 'performance'
| 'queueMicrotask'
| 'requestAnimationFrame'
| 'cancelAnimationFrame'
| 'requestIdleCallback'
| 'cancelIdleCallback'
| 'setImmediate'
| 'clearImmediate'
| 'setInterval'
| 'clearInterval'
| 'setTimeout'
| 'clearTimeout';

type FakeTimersConfig = {
advanceTimers?: boolean | number,
doNotFake?: Array<FakeableAPI>,
now?: number | Date,
timerLimit?: number,
legacyFakeTimers?: boolean,
...
};

/**
* Plugin: jest-styled-components
*/
Expand Down Expand Up @@ -231,7 +264,7 @@ type EnzymeMatchersType = {
toIncludeText(text: string): void,
toMatchElement(
element: React$Element<any>,
options?: {ignoreProps?: boolean, verbose?: boolean},
options?: {|ignoreProps?: boolean, verbose?: boolean|},
): void,
toMatchSelector(selector: string): void,
// 7.x
Expand Down Expand Up @@ -265,7 +298,7 @@ type DomTestingLibraryType = {
toHaveStyle(css: string | {[name: string]: any, ...}): void,
toHaveTextContent(
text: string | RegExp,
options?: {normalizeWhitespace: boolean},
options?: {|normalizeWhitespace: boolean|},
): void,
toHaveValue(value?: string | string[] | number): void,

Expand Down Expand Up @@ -570,14 +603,14 @@ type SnapshotDiffType = {
*/
toMatchDiffSnapshot(
valueB: any,
options?: {
options?: {|
expand?: boolean,
colors?: boolean,
contextLines?: number,
stablePatchmarks?: boolean,
aAnnotation?: string,
bAnnotation?: string,
},
|},
testName?: string,
): void,
...
Expand All @@ -591,11 +624,21 @@ interface JestExpectType {
JestStyledComponentsMatchersType &
JestExtendedMatchersType &
SnapshotDiffType;
/**
* If you have a mock function, you can use .lastCalledWith to test what
* arguments it was last called with.
*/
lastCalledWith(...args: Array<any>): void;
/**
* toBe just checks that a value is what you expect. It uses === to check
* strict equality.
*/
toBe(value: any): void;
/**
* Use .toBeCalledWith to ensure that a mock function was called with
* specific arguments.
*/
toBeCalledWith(...args: Array<any>): void;
/**
* Using exact equality with floating point numbers is a bad idea. Rounding
* means that intuitive things fail.
Expand Down Expand Up @@ -708,20 +751,12 @@ interface JestExpectType {
* specific arguments.
*/
toHaveBeenCalledWith(...args: Array<any>): void;
/**
* Use .toBeCalledWith to ensure that a mock function was called with
* specific arguments.
*/
toBeCalledWith(...args: Array<any>): void;
/**
* Use .toHaveBeenLastCalledWith to ensure that a mock function was last called
* with specific arguments.
*/
toHaveBeenLastCalledWith(...args: Array<any>): void;
/**
* If you have a mock function, you can use .lastCalledWith to test what
* arguments it was last called with.
*/
lastCalledWith(...args: Array<any>): void;
/**
* Check that an object has a .length property and it is set to a certain
Expand Down Expand Up @@ -837,7 +872,8 @@ type JestObjectType = {
* Returns a new, unused mock function. Optionally takes a mock
* implementation.
*/
fn<TArguments: $ReadOnlyArray<any> = $ReadOnlyArray<any>, TReturn = any>(
// MODIFIED: Added defaults to type arguments.
fn<TArguments: $ReadOnlyArray<mixed> = $ReadOnlyArray<any>, TReturn = any>(
implementation?: (...args: TArguments) => TReturn,
): JestMockFn<TArguments, TReturn>,
/**
Expand Down Expand Up @@ -907,13 +943,6 @@ type JestObjectType = {
* or setInterval() and setImmediate()).
*/
advanceTimersByTime(msToRun: number): void,
/**
* Executes only the macro task queue (i.e. all tasks queued by setTimeout()
* or setInterval() and setImmediate()).
*
* Renamed to `advanceTimersByTime`.
*/
runTimersToTime(msToRun: number): void,
/**
* Executes only the macro-tasks that are currently pending (i.e., only the
* tasks that have been queued by setTimeout() or setInterval() up to this
Expand All @@ -937,7 +966,7 @@ type JestObjectType = {
* (setTimeout, setInterval, clearTimeout, clearInterval, nextTick,
* setImmediate and clearImmediate).
*/
useFakeTimers(mode?: 'modern' | 'legacy'): JestObjectType,
useFakeTimers(fakeTimersConfig?: FakeTimersConfig): JestObjectType,
/**
* Instructs Jest to use the real versions of the standard timer functions.
*/
Expand All @@ -961,10 +990,10 @@ type JestObjectType = {

type JestSpyType = {calls: JestCallsType, ...};

type JestDoneFn = {
type JestDoneFn = {|
(error?: Error): void,
fail: (error: Error) => void,
};
|};

/** Runs this function after every test inside this context */
declare function afterEach(
Expand Down Expand Up @@ -1037,7 +1066,7 @@ declare var it: {
* @param {Function} Test
* @param {number} Timeout for the test, in milliseconds.
*/
only: {
only: {|
(
name: JestTestName,
fn?: (done: JestDoneFn) => ?Promise<mixed>,
Expand All @@ -1050,15 +1079,15 @@ declare var it: {
fn?: (...args: Array<any>) => ?Promise<mixed>,
timeout?: number,
) => void,
},
|},
/**
* Skip running this test
*
* @param {JestTestName} Name of Test
* @param {Function} Test
* @param {number} Timeout for the test, in milliseconds.
*/
skip: {
skip: {|
(
name: JestTestName,
fn?: (done: JestDoneFn) => ?Promise<mixed>,
Expand All @@ -1071,7 +1100,7 @@ declare var it: {
fn?: (...args: Array<any>) => ?Promise<mixed>,
timeout?: number,
) => void,
},
|},
/**
* Highlight planned tests in the summary output
*
Expand Down Expand Up @@ -1151,9 +1180,11 @@ type JestPrettyFormatColors = {
};

type JestPrettyFormatIndent = string => string;
type JestPrettyFormatRefs = Array<any>;
type JestPrettyFormatPrint = any => string;
type JestPrettyFormatStringOrNull = string | null;

type JestPrettyFormatOptions = {
type JestPrettyFormatOptions = {|
callToJSON: boolean,
edgeSpacing: string,
escapeRegex: boolean,
Expand All @@ -1164,14 +1195,14 @@ type JestPrettyFormatOptions = {
plugins: JestPrettyFormatPlugins,
printFunctionName: boolean,
spacing: string,
theme: {
theme: {|
comment: string,
content: string,
prop: string,
tag: string,
value: string,
},
};
|},
|};

type JestPrettyFormatPlugin = {
print: (
Expand Down Expand Up @@ -1208,7 +1239,8 @@ declare var expect: {
hasAssertions(): void,
any(value: mixed): JestAsymmetricEqualityType,
anything(): any,
arrayContaining(value: Array<mixed>): Array<mixed>,
// MODIFIED: Array -> $ReadOnlyArray
arrayContaining(value: $ReadOnlyArray<mixed>): Array<mixed>,
objectContaining(value: Object): Object,
/** Matches any received string that contains the exact expected string. */
stringContaining(value: string): string,
Expand Down
Expand Up @@ -19,7 +19,7 @@ describe('AutoCleanFileStore', () => {
jest
.resetModules()
.resetAllMocks()
.useFakeTimers('legacy') // Legacy fake timers are reset by `resetAllMocks()`
.useFakeTimers({legacyFakeTimers: true}) // Legacy fake timers are reset by `resetAllMocks()`
.mock('fs', () => new (require('metro-memory-fs'))());

AutoCleanFileStore = require('../AutoCleanFileStore');
Expand Down
Expand Up @@ -18,7 +18,7 @@ describe('FileStore', () => {
jest
.resetModules()
.resetAllMocks()
.useFakeTimers('legacy') // Legacy fake timers are reset by `resetAllMocks()`
.useFakeTimers({legacyFakeTimers: true}) // Legacy fake timers are reset by `resetAllMocks()`
.mock('fs', () => new (require('metro-memory-fs'))());

FileStore = require('../FileStore');
Expand Down
Expand Up @@ -41,7 +41,7 @@ describe('HttpGetStore', () => {
jest
.resetModules()
.resetAllMocks()
.useFakeTimers('legacy') // Legacy fake timers are reset by `resetAllMocks()`
.useFakeTimers({legacyFakeTimers: true}) // Legacy fake timers are reset by `resetAllMocks()`
.mock('http');

httpPassThrough = new PassThrough();
Expand Down
Expand Up @@ -52,7 +52,7 @@ describe('HttpStore', () => {
jest
.resetModules()
.resetAllMocks()
.useFakeTimers('legacy') // Legacy fake timers are reset by `resetAllMocks()`
.useFakeTimers({legacyFakeTimers: true}) // Legacy fake timers are reset by `resetAllMocks()`
.mock('http')
.mock('https');

Expand Down
2 changes: 1 addition & 1 deletion packages/metro/src/__tests__/HmrServer-test.js
Expand Up @@ -69,7 +69,7 @@ describe('HmrServer', () => {

async function emitChangeEvent() {
// TODO: Can we achieve this with less mocking / special-casing?
jest.useFakeTimers('legacy');
jest.useFakeTimers({legacyFakeTimers: true});
changeEventSource.emit('change');
jest.runAllTimers();
jest.useRealTimers();
Expand Down

0 comments on commit 1d51ffd

Please sign in to comment.