Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate jest-mock to TypeScript #7847

Merged
merged 9 commits into from
Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ interface MockInstance<T, Y extends unknown[]> {
getMockName(): string;
getMockImplementation(): Function | undefined;
mock: MockFunctionState<T, Y>;
mockClear(): void;
mockReset(): void;
mockClear(): this;
mockReset(): this;
mockRestore(): void;
mockImplementation(fn: (...args: Y) => T): Mock<T, Y>;
mockImplementation(fn: () => Promise<T>): Mock<T, Y>;
mockImplementationOnce(fn: (...args: Y) => T): Mock<T, Y>;
mockImplementationOnce(fn: () => Promise<T>): Mock<T, Y>;
mockName(name: string): Mock<T, Y>;
mockReturnThis(): Mock<T, Y>;
mockReturnValue(value: T): Mock<T, Y>;
mockReturnValueOnce(value: T): Mock<T, Y>;
mockResolvedValue(value: T): Mock<T, Y>;
mockResolvedValueOnce(value: T): Mock<T, Y>;
mockRejectedValue(value: T): Mock<T, Y>;
mockRejectedValueOnce(value: T): Mock<T, Y>;
mockImplementation(fn: (...args: Y) => T): this;
mockImplementation(fn: () => Promise<T>): this;
mockImplementationOnce(fn: (...args: Y) => T): this;
mockImplementationOnce(fn: () => Promise<T>): this;
mockName(name: string): this;
mockReturnThis(): this;
mockReturnValue(value: T): this;
mockReturnValueOnce(value: T): this;
mockResolvedValue(value: T): this;
mockResolvedValueOnce(value: T): this;
mockRejectedValue(value: T): this;
mockRejectedValueOnce(value: T): this;
}

const MOCK_CONSTRUCTOR_NAME = 'mockConstructor';
Expand Down
7 changes: 3 additions & 4 deletions packages/jest-util/src/FakeTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

// not yet migrated to TS
// import {ModuleMocker} from 'jest-mock';

type ModuleMocker = any;
type ModuleMocker = typeof import('jest-mock');

import {formatStackTrace, StackTraceConfig} from 'jest-message-util';
import setGlobal from './setGlobal';
Expand Down Expand Up @@ -356,8 +353,10 @@ export default class FakeTimers<TimerRef> {

_createMocks() {
const fn = (impl: Function) =>
// @ts-ignore TODO: figure out better typings here
this._moduleMocker.fn().mockImplementation(impl);

// TODO: add better typings; these are mocks, but typed as regular timers
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the two comments are connected. cc @SimenB

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanna remove the autospying anyways (I've removed it in the Lolex implementation)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, we should be able to infer the type of the function passed in (can do later)

this._fakeTimerAPIs = {
clearImmediate: fn(this._fakeClearImmediate.bind(this)),
clearInterval: fn(this._fakeClearTimer.bind(this)),
Expand Down
5 changes: 3 additions & 2 deletions packages/jest-util/src/__tests__/fakeTimers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

import vm from 'vm';
// @ts-ignore: not yet migrated
import mock, {ModuleMocker} from 'jest-mock';
import mock from 'jest-mock';
import FakeTimers from '../FakeTimers';
// TODO: import this type directly from jest-mock once TS migration is done
type ModuleMocker = typeof mock;

const timerConfig = {
idToRef: (id: number) => id,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-util/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"rootDir": "src",
"outDir": "build"
},
"references": [{"path": "../jest-types"}]
"references": [{"path": "../jest-types"}, {"path": "../jest-mock"}]
}