diff --git a/types/expect-puppeteer/index.d.ts b/types/expect-puppeteer/index.d.ts index b77bc948bd..018e342ce8 100644 --- a/types/expect-puppeteer/index.d.ts +++ b/types/expect-puppeteer/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Josh Goldberg // Tanguy Krotoff // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.0 /// diff --git a/types/frisby/index.d.ts b/types/frisby/index.d.ts index 85151daa68..3d132bdc91 100644 --- a/types/frisby/index.d.ts +++ b/types/frisby/index.d.ts @@ -3,7 +3,7 @@ // Definitions by: Christopher E. Woodland // Johnny Li // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.0 /// diff --git a/types/jest-axe/index.d.ts b/types/jest-axe/index.d.ts index 60375e3605..506236773f 100644 --- a/types/jest-axe/index.d.ts +++ b/types/jest-axe/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/nickcolley/jest-axe // Definitions by: Josh Goldberg // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.4 +// TypeScript Version: 3.0 /// diff --git a/types/jest-image-snapshot/index.d.ts b/types/jest-image-snapshot/index.d.ts index c15e435cc9..8eeff20990 100644 --- a/types/jest-image-snapshot/index.d.ts +++ b/types/jest-image-snapshot/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/americanexpress/jest-image-snapshot#readme // Definitions by: Janeene Beeforth // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// diff --git a/types/jest-in-case/index.d.ts b/types/jest-in-case/index.d.ts index 02d7d08af9..9762bc39d3 100644 --- a/types/jest-in-case/index.d.ts +++ b/types/jest-in-case/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/thinkmill/jest-in-case#readme // Definitions by: Geovani de Souza // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// /// diff --git a/types/jest-in-case/jest-in-case-tests.ts b/types/jest-in-case/jest-in-case-tests.ts index 283ade76c1..ac99bc4683 100644 --- a/types/jest-in-case/jest-in-case-tests.ts +++ b/types/jest-in-case/jest-in-case-tests.ts @@ -11,8 +11,8 @@ function subtract(minuend: number, subtrahend: number) { } beforeEach(() => { - jest.spyOn(global, 'describe').mockImplementation((title, fn) => fn()); - jest.spyOn(global, 'test').mockImplementation((name, fn) => fn()); + jest.spyOn(global, 'describe').mockImplementation((title, fn) => (fn as () => void)()); + jest.spyOn(global, 'test').mockImplementation((name, fn) => (fn as () => void)()); global.test.skip = jest.fn((name, fn) => fn()); global.test.only = jest.fn((name, fn) => fn()); }); @@ -54,8 +54,8 @@ test('array', () => { }); test('object', () => { - jest.spyOn(global, 'describe').mockImplementation((title, fn) => fn()); - jest.spyOn(global, 'test').mockImplementation((name, fn) => fn()); + jest.spyOn(global, 'describe').mockImplementation((title, fn) => (fn as () => void)()); + jest.spyOn(global, 'test').mockImplementation((name, fn) => (fn as () => void)()); const title = 'add(augend, addend)'; diff --git a/types/jest-json-schema/index.d.ts b/types/jest-json-schema/index.d.ts index 67f1c51f7e..b2133526dc 100644 --- a/types/jest-json-schema/index.d.ts +++ b/types/jest-json-schema/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/americanexpress/jest-json-schema#readme // Definitions by: Igor Korolev // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// import * as ajv from "ajv"; diff --git a/types/jest-matchers/index.d.ts b/types/jest-matchers/index.d.ts index 3b028f8929..705f993ea3 100644 --- a/types/jest-matchers/index.d.ts +++ b/types/jest-matchers/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/facebook/jest#readme // Definitions by: Joscha Feth // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// export = expect; diff --git a/types/jest-plugin-context/index.d.ts b/types/jest-plugin-context/index.d.ts index da2698b78e..814b334b12 100644 --- a/types/jest-plugin-context/index.d.ts +++ b/types/jest-plugin-context/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/negativetwelve/jest-plugins/tree/master/packages/jest-plugin-context // Definitions by: Jonas Heinrich // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// diff --git a/types/jest-specific-snapshot/index.d.ts b/types/jest-specific-snapshot/index.d.ts index 61a65a416a..0e0a43d895 100644 --- a/types/jest-specific-snapshot/index.d.ts +++ b/types/jest-specific-snapshot/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/igor-dv/jest-specific-snapshot#readme // Definitions by: Janeene Beeforth // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.0 /// diff --git a/types/jest-when/index.d.ts b/types/jest-when/index.d.ts index 0579419425..386911ed7c 100644 --- a/types/jest-when/index.d.ts +++ b/types/jest-when/index.d.ts @@ -2,26 +2,21 @@ // Project: https://github.com/timkindberg/jest-when#readme // Definitions by: Alden Taylor // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 /// -export interface When { - (fn: jest.Mocked | jest.Mock): When; - - // due to no-unnecessary-generics lint rule, the generics have been replaced with 'any' - // calledWith(...matchers: any[]): PartialMockInstance; - // expectCalledWith(...matchers: any[]): PartialMockInstance; - calledWith(...matchers: any[]): When; - - expectCalledWith(...matchers: any[]): When; - - mockReturnValue: (value: any) => jest.MockInstance['mockReturnValue'] & When; - mockReturnValueOnce: (value: any) => jest.MockInstance['mockReturnValue'] & When; - mockResolvedValue: (value: any) => jest.MockInstance['mockReturnValue'] & When; - mockResolvedValueOnce: (value: any) => jest.MockInstance['mockReturnValue'] & When; - mockRejectedValue: (value: any) => jest.MockInstance['mockReturnValue'] & When; - mockRejectedValueOnce: (value: any) => jest.MockInstance['mockReturnValue'] & When; +export interface WhenMock extends jest.Mock { + calledWith(...matchers: Y): WhenMock; + expectCalledWith(...matchers: Y): WhenMock; + mockReturnValue(value: T): WhenMock; + mockReturnValueOnce(value: T): WhenMock; + mockResolvedValue(value: T | PromiseLike): WhenMock, Y>; + mockResolvedValueOnce(value: T | PromiseLike): WhenMock, Y>; + mockRejectedValue(value: T | PromiseLike): WhenMock, Y>; + mockRejectedValueOnce(value: T | PromiseLike): WhenMock, Y>; } +export type When = (fn: jest.Mock) => WhenMock; + export const when: When; diff --git a/types/jest-when/jest-when-tests.ts b/types/jest-when/jest-when-tests.ts index 179ea2fae1..55744218ce 100644 --- a/types/jest-when/jest-when-tests.ts +++ b/types/jest-when/jest-when-tests.ts @@ -31,16 +31,20 @@ describe('mock-when test', () => { it('Supports compound declarations:', () => { const fn = jest.fn(); - when(fn).calledWith(1).mockReturnValue('no'); + when(fn).calledWith(1).mockReturnValueOnce('no').mockReturnValue('yes'); when(fn).calledWith(2).mockReturnValue('way?'); - when(fn).calledWith(3).mockReturnValue('yes'); - when(fn).calledWith(4).mockReturnValue('way!'); + when(fn).calledWith(3).mockResolvedValueOnce('no'); + when(fn).calledWith(3).mockResolvedValue('yes'); + when(fn).calledWith(4).mockRejectedValueOnce('no'); + when(fn).calledWith(4).mockRejectedValue('yes'); expect(fn(1)).toEqual('no'); + expect(fn(1)).toEqual('yes'); expect(fn(2)).toEqual('way?'); - expect(fn(3)).toEqual('yes'); - expect(fn(4)).toEqual('way!'); - expect(fn(5)).toEqual(undefined); + expect(fn(3)).resolves.toEqual('no'); + expect(fn(3)).resolves.toEqual('yes'); + expect(fn(4)).rejects.toEqual('no'); + expect(fn(4)).rejects.toEqual('yes'); }); it('Supports chained calls:', () => { diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 368ed83353..072831ac8c 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -17,8 +17,9 @@ // Martin Hochel // Sebastian Sebald // Andy +// Antoine Brault // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 +// TypeScript Version: 3.0 declare var beforeAll: jest.Lifecycle; declare var beforeEach: jest.Lifecycle; @@ -35,6 +36,8 @@ declare var xtest: jest.It; declare const expect: jest.Expect; +type ArgsType = T extends (...args: infer A) => any ? A : never; + interface NodeRequire { /** * Returns the actual module instead of a mock, bypassing all checks on @@ -114,11 +117,11 @@ declare namespace jest { /** * Creates a mock function. Optionally takes a mock implementation. */ - function fn(implementation: (...args: any[]) => T): Mock; + function fn(): Mock; /** * Creates a mock function. Optionally takes a mock implementation. */ - function fn(implementation?: (...args: any[]) => any): Mock; + function fn(implementation: (...args: Y) => T): Mock; /** * Use the automatic mocking system to generate a mocked version of the given module. */ @@ -126,7 +129,7 @@ declare namespace jest { /** * Returns whether the given function is a mock function. */ - function isMockFunction(fn: any): fn is Mock; + function isMockFunction(fn: any): fn is Mock; /** * Mocks a module with an auto-mocked version when it is being required. */ @@ -212,7 +215,9 @@ declare namespace jest { * spy.mockRestore(); * }); */ - function spyOn(object: T, method: M, accessType?: 'get' | 'set'): SpyInstance; + function spyOn(object: T, method: M, accessType: 'get'): SpyInstance; + function spyOn(object: T, method: M, accessType: 'set'): SpyInstance; + function spyOn(object: T, method: M): T[M] extends (...args: any[]) => any ? SpyInstance, ArgsType> : never; /** * Indicates that the module system should never return a mocked version of * the specified module from require() (e.g. that it should always return the real module). @@ -776,12 +781,12 @@ declare namespace jest { new (...args: any[]): any; } - interface Mock extends Function, MockInstance { - new (...args: any[]): T; - (...args: any[]): any; + interface Mock extends Function, MockInstance { + new (...args: Y): T; + (...args: Y): T; } - interface SpyInstance extends MockInstance {} + interface SpyInstance extends MockInstance {} /** * Wrap module with mock definitions @@ -795,14 +800,14 @@ declare namespace jest { * myApi.myApiMethod.mockImplementation(() => "test"); */ type Mocked = { - [P in keyof T]: T[P] & MockInstance; + [P in keyof T]: T[P] & MockInstance>; } & T; - interface MockInstance { + interface MockInstance { /** Returns the mock name string set by calling `mockFn.mockName(value)`. */ getMockName(): string; /** Provides access to the mock's metadata */ - mock: MockContext; + mock: MockContext; /** * Resets all information stored in the mockFn.mock.calls and mockFn.mock.instances arrays. * @@ -842,7 +847,7 @@ declare namespace jest { * * Note: `jest.fn(implementation)` is a shorthand for `jest.fn().mockImplementation(implementation)`. */ - mockImplementation(fn?: (...args: any[]) => any): Mock; + mockImplementation(fn?: (...args: Y) => T): Mock; /** * Accepts a function that will be used as an implementation of the mock for one call to the mocked function. * Can be chained so that multiple function calls produce different results. @@ -858,9 +863,9 @@ declare namespace jest { * * myMockFn((err, val) => console.log(val)); // false */ - mockImplementationOnce(fn: (...args: any[]) => any): Mock; + mockImplementationOnce(fn: (...args: Y) => T): Mock; /** Sets the name of the mock`. */ - mockName(name: string): Mock; + mockName(name: string): Mock; /** * Just a simple sugar function for: * @@ -870,7 +875,7 @@ declare namespace jest { * return this; * }); */ - mockReturnThis(): Mock; + mockReturnThis(): Mock; /** * Accepts a value that will be returned whenever the mock function is called. * @@ -882,7 +887,7 @@ declare namespace jest { * mock.mockReturnValue(43); * mock(); // 43 */ - mockReturnValue(value: any): Mock; + mockReturnValue(value: T): Mock; /** * Accepts a value that will be returned for one call to the mock function. Can be chained so that * successive calls to the mock function return different values. When there are no more @@ -899,11 +904,11 @@ declare namespace jest { * console.log(myMockFn(), myMockFn(), myMockFn(), myMockFn()); * */ - mockReturnValueOnce(value: any): Mock; + mockReturnValueOnce(value: T): Mock; /** * Simple sugar function for: `jest.fn().mockImplementation(() => Promise.resolve(value));` */ - mockResolvedValue(value: any): Mock; + mockResolvedValue(value: T | PromiseLike): Mock, Y>; /** * Simple sugar function for: `jest.fn().mockImplementationOnce(() => Promise.resolve(value));` * @@ -923,7 +928,7 @@ declare namespace jest { * }); * */ - mockResolvedValueOnce(value: any): Mock; + mockResolvedValueOnce(value: T | PromiseLike): Mock, Y>; /** * Simple sugar function for: `jest.fn().mockImplementation(() => Promise.reject(value));` * @@ -935,7 +940,7 @@ declare namespace jest { * await asyncMock(); // throws "Async error" * }); */ - mockRejectedValue(value: any): Mock; + mockRejectedValue(value: any): Mock, Y>; /** * Simple sugar function for: `jest.fn().mockImplementationOnce(() => Promise.reject(value));` @@ -953,7 +958,7 @@ declare namespace jest { * }); * */ - mockRejectedValueOnce(value: any): Mock; + mockRejectedValueOnce(value: any): Mock, Y>; } /** @@ -971,8 +976,8 @@ declare namespace jest { value: any; } - interface MockContext { - calls: any[][]; + interface MockContext { + calls: Y[]; instances: T[]; invocationCallOrder: number[]; /** diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index 4164e009e4..4aa7880e95 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -259,12 +259,43 @@ jest.requireMock("./thisAlwaysReturnsTheMock"); /* Mocks and spies */ -const mock1: jest.Mock = jest.fn(); -const mock2: jest.Mock = jest.fn(() => undefined); -const mock3: jest.Mock = jest.fn(() => "abc"); -const mock4: jest.Mock<"abc"> = jest.fn((): "abc" => "abc"); -const mock5: jest.Mock = jest.fn((...args: string[]) => args.join("")); -const mock6: jest.Mock = jest.fn((arg: {}) => arg); +// $ExpectType Mock +const mock1: jest.Mock = jest.fn(); +// $ExpectType Mock +const mock2 = jest.fn(() => undefined); +// $ExpectType Mock +const mock3 = jest.fn(() => "abc"); +// $ExpectType Mock<"abc", []> +const mock4 = jest.fn((): "abc" => "abc"); +// $ExpectType Mock +const mock5 = jest.fn((...args: string[]) => args.join("")); +// $ExpectType Mock<{}, [{}]> +const mock6 = jest.fn((arg: {}) => arg); +// $ExpectType Mock +const mock7 = jest.fn((arg: number) => arg); +// $ExpectType Mock +const mock8: jest.Mock = jest.fn((arg: number) => arg); +// $ExpectType Mock, [number, string, {}, [], boolean]> +const mock9 = jest.fn((a: number, _b: string, _c: {}, _iReallyDontCare: [], _makeItStop: boolean) => Promise.resolve(_makeItStop)); +// $ExpectType Mock +const mock10 = jest.fn((arg: never) => { throw new Error(arg); }); +// $ExpectType Mock +const mock11 = jest.fn((arg: unknown) => arg); + +// $ExpectType number +mock1('test'); + +// $ExpectError +mock7('abc'); +// $ExpectError +mock7.mockImplementation((arg: string) => 1); + +// compiles because mock8 is declared as jest.Mock<{}, any> +mock8('abc'); +mock8.mockImplementation((arg: string) => 1); + +// mockImplementation not required to declare all arguments +mock9.mockImplementation((a: number) => Promise.resolve(a === 0)); const genMockModule1: {} = jest.genMockFromModule("moduleName"); const genMockModule2: { a: "b" } = jest.genMockFromModule<{ a: "b" }>("moduleName"); @@ -278,8 +309,8 @@ if (jest.isMockFunction(maybeMock)) { } const mockName: string = jest.fn().getMockName(); -const mockContextVoid: jest.MockContext = jest.fn().mock; -const mockContextString: jest.MockContext = jest.fn(() => "").mock; +const mockContextVoid = jest.fn().mock; +const mockContextString = jest.fn(() => "").mock; jest.fn().mockClear(); @@ -289,14 +320,28 @@ jest.fn().mockRestore(); const spiedTarget = { returnsVoid(): void { }, + setValue(value: string): void { + this.value = value; + }, returnsString(): string { return ""; } }; +class SpiedTargetClass { + private _value = 3; + get value() { + return this._value; + } + set value(value) { + this._value = value; + } +} +const spiedTarget2 = new SpiedTargetClass(); + const spy1 = jest.spyOn(spiedTarget, "returnsVoid"); const spy2 = jest.spyOn(spiedTarget, "returnsVoid", "get"); -const spy3 = jest.spyOn(spiedTarget, "returnsString", "set"); +const spy3 = jest.spyOn(spiedTarget, "returnsString"); const spy1Name: string = spy1.getMockName(); const spy2Calls: any[][] = spy2.mock.calls; @@ -304,9 +349,10 @@ const spy2Calls: any[][] = spy2.mock.calls; spy2.mockClear(); spy2.mockReset(); -const spy3Mock: jest.Mock<() => string> = spy3 +const spy3Mock = spy3 .mockImplementation(() => "") .mockImplementation() + // $ExpectError .mockImplementation((arg: {}) => arg) .mockImplementation((...args: string[]) => args.join("")) .mockImplementationOnce(() => "") @@ -320,10 +366,23 @@ const spy3Mock: jest.Mock<() => string> = spy3 .mockRejectedValueOnce("value"); let spy4: jest.SpyInstance; - +// $ExpectType SpyInstance spy4 = jest.spyOn(spiedTarget, "returnsString"); +// compiles because spy4 is declared as jest.SpyInstance +spy4.mockImplementation(() => 1); spy4.mockRestore(); +// $ExpectType SpyInstance +const spy5 = jest.spyOn(spiedTarget2, "value", "get"); +// $ExpectError +spy5.mockReturnValue('5'); + +// $ExpectType SpyInstance +const spy6 = jest.spyOn(spiedTarget2, "value", "set"); + +// should compile +jest.fn().mockImplementation((test: number) => test); + /* Snapshot serialization */ const snapshotSerializerPlugin: jest.SnapshotSerializerPlugin = { diff --git a/types/jest/tsconfig.json b/types/jest/tsconfig.json index 8a02004840..20bc02a53b 100644 --- a/types/jest/tsconfig.json +++ b/types/jest/tsconfig.json @@ -13,6 +13,7 @@ "typeRoots": [ "../" ], + "target": "es5", "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true diff --git a/types/storybook__addon-storyshots/index.d.ts b/types/storybook__addon-storyshots/index.d.ts index 80e8894b29..7de2fa1dca 100644 --- a/types/storybook__addon-storyshots/index.d.ts +++ b/types/storybook__addon-storyshots/index.d.ts @@ -2,7 +2,7 @@ // Project: https://github.com/storybooks/storybook/tree/master/addons/storyshots // Definitions by: Bradley Ayers // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 +// TypeScript Version: 3.0 import * as React from 'react'; import { StoryObject } from '@storybook/react';