Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62afb83
commit 4b4b03d
Showing
9 changed files
with
159 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* 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 {expectAssignable, expectType} from 'tsd-lite'; | ||
import type {MockInstance, Mocked} from 'jest-mock'; | ||
|
||
/// mocks class | ||
|
||
class ExampleClass { | ||
constructor(c: string, d?: boolean) {} | ||
|
||
// _propertyB: false; | ||
|
||
methodA() { | ||
return true; | ||
} | ||
methodB(a: string, b: number) { | ||
return; | ||
} | ||
methodC(e: any) { | ||
throw new Error(); | ||
} | ||
|
||
// propertyA: 'abc', | ||
} | ||
|
||
const MockExampleClass = ExampleClass as Mocked<typeof ExampleClass>; | ||
|
||
const xx = MockExampleClass.mock.calls[0]; | ||
const yy = MockExampleClass.prototype.methodB.mock.calls[0]; | ||
|
||
const ww = MockExampleClass.mock.instances[0].methodB.mock.calls[0]; | ||
|
||
const mockExample = new MockExampleClass('c') as Mocked< | ||
InstanceType<typeof MockExampleClass> | ||
>; | ||
|
||
const zz = mockExample.methodB.mock.calls[0]; | ||
|
||
/// mocks function | ||
|
||
function someFunction(a: number, b?: string): boolean { | ||
return true; | ||
} | ||
|
||
async function someAsyncFunction(a: Array<boolean>): Promise<string> { | ||
return 'true'; | ||
} | ||
|
||
const mockFunction = someFunction as Mocked<typeof someFunction>; | ||
|
||
expectType<number>(mockFunction.mock.calls[0][0]); | ||
expectType<string | undefined>(mockFunction.mock.calls[0][1]); | ||
|
||
const mockFunctionResult = mockFunction.mock.results[0]; | ||
|
||
if (mockFunctionResult.type === 'return') { | ||
expectType<boolean>(mockFunctionResult.value); | ||
} | ||
|
||
const mockAsyncFunction = someAsyncFunction as Mocked<typeof someAsyncFunction>; | ||
|
||
expectType<Array<boolean>>(mockAsyncFunction.mock.calls[0][0]); | ||
// expectError(mockAsyncFunction.mock.calls[0][1]); | ||
|
||
const mockAsyncFunctionResult = mockAsyncFunction.mock.results[0]; | ||
|
||
if (mockAsyncFunctionResult.type === 'return') { | ||
expectType<Promise<string>>(mockAsyncFunctionResult.value); | ||
} | ||
|
||
// mocks object | ||
|
||
const mockConsole = console as Mocked<typeof console>; | ||
|
||
expectAssignable<typeof console.log>( | ||
mockConsole.log.mockImplementation(() => {}), | ||
); | ||
expectAssignable<MockInstance<typeof console.log>>( | ||
mockConsole.log.mockImplementation(() => {}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
"noUnusedParameters": false, | ||
"skipLibCheck": true, | ||
|
||
"types": [] | ||
"types": ["node"] | ||
}, | ||
"include": ["./**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* 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 type {MockInstance} from './index'; | ||
|
||
type ClassLike = {new (...args: any): any}; | ||
type FunctionLike = (...args: any) => any; | ||
|
||
type MockedClass<T extends ClassLike> = MockInstance< | ||
(...args: ConstructorParameters<T>) => Mocked<InstanceType<T>> | ||
> & { | ||
prototype: T extends {prototype: any} ? Mocked<T['prototype']> : never; | ||
} & MockedObject<T>; | ||
|
||
type MockedFunction<T extends FunctionLike> = MockInstance<T> & MockedObject<T>; | ||
|
||
type MockedObject<T> = { | ||
[K in keyof T]: T[K] extends ClassLike | ||
? MockedClass<T[K]> | ||
: T[K] extends FunctionLike | ||
? MockedFunction<T[K]> | ||
: T[K] extends object | ||
? MockedObject<T[K]> | ||
: T[K]; | ||
} & T; | ||
|
||
export type Mocked<T> = T extends ClassLike | ||
? MockedClass<T> | ||
: T extends FunctionLike | ||
? MockedFunction<T> | ||
: T extends object | ||
? MockedObject<T> | ||
: T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters