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

decentralize jest-mock types #7971

Merged
merged 2 commits into from Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -39,7 +39,7 @@
- `[@jest/types]`: New package to handle shared types ([#7834](https://github.com/facebook/jest/pull/7834))
- `[jest-util]`: Migrate to TypeScript ([#7844](https://github.com/facebook/jest/pull/7844))
- `[jest-watcher]`: Migrate to TypeScript ([#7843](https://github.com/facebook/jest/pull/7843))
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850))
- `[jest-mock]`: Migrate to TypeScript ([#7847](https://github.com/facebook/jest/pull/7847), [#7850](https://github.com/facebook/jest/pull/7850), [#7971](https://github.com/facebook/jest/pull/7971))
- `[jest-worker]`: Migrate to TypeScript ([#7853](https://github.com/facebook/jest/pull/7853))
- `[jest-haste-map]`: Migrate to TypeScript ([#7854](https://github.com/facebook/jest/pull/7854), [#7951](https://github.com/facebook/jest/pull/7951))
- `[docs]`: Fix image paths in SnapshotTesting.md for current and version 24 ([#7872](https://github.com/facebook/jest/pull/7872))
Expand Down
63 changes: 44 additions & 19 deletions packages/jest-mock/src/index.ts
Expand Up @@ -5,10 +5,36 @@
* LICENSE file in the root directory of this source tree.
*/

import {Mocks} from '@jest/types';

type Global = NodeJS.Global; // | Window – add once TS improves typings;

namespace JestMock {
export type ModuleMocker = ModuleMockerClass;
export type MockFunctionMetadataType =
| 'object'
| 'array'
| 'regexp'
| 'function'
| 'constant'
| 'collection'
| 'null'
| 'undefined';

export type MockFunctionMetadata<
T,
Y extends unknown[],
Type = MockFunctionMetadataType
> = {
ref?: number;
members?: {[key: string]: MockFunctionMetadata<T, Y>};
mockImpl?: (...args: Y) => T;
name?: string;
refID?: number;
type?: Type;
value?: T;
length?: number;
};
}

/**
* Possible types of a MockFunctionResult.
* 'return': The call completed by returning normally.
Expand Down Expand Up @@ -273,7 +299,7 @@ function getObjectType(value: unknown): string {
return Object.prototype.toString.apply(value).slice(8, -1);
}

function getType(ref?: unknown): Mocks.MockFunctionMetadataType | null {
function getType(ref?: unknown): JestMock.MockFunctionMetadataType | null {
const typeName = getObjectType(ref);
if (
typeName === 'Function' ||
Expand Down Expand Up @@ -449,31 +475,31 @@ class ModuleMockerClass {
}

private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'object'>,
metadata: JestMock.MockFunctionMetadata<T, Y, 'object'>,
restore?: () => void,
): Object;
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'array'>,
metadata: JestMock.MockFunctionMetadata<T, Y, 'array'>,
restore?: () => void,
): Array<unknown>;
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'regexp'>,
metadata: JestMock.MockFunctionMetadata<T, Y, 'regexp'>,
restore?: () => void,
): RegExp;
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<
metadata: JestMock.MockFunctionMetadata<
T,
Y,
'constant' | 'collection' | 'null' | 'undefined'
>,
restore?: () => void,
): T;
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y, 'function'>,
metadata: JestMock.MockFunctionMetadata<T, Y, 'function'>,
restore?: () => void,
): Mock<T, Y>;
private _makeComponent<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
metadata: JestMock.MockFunctionMetadata<T, Y>,
restore?: () => void,
): Object | Array<unknown> | RegExp | T | undefined | Mock<T, Y> {
if (metadata.type === 'object') {
Expand Down Expand Up @@ -719,7 +745,7 @@ class ModuleMockerClass {
}

private _createMockFunction<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
metadata: JestMock.MockFunctionMetadata<T, Y>,
mockConstructor: Function,
): Function {
let name = metadata.name;
Expand Down Expand Up @@ -779,7 +805,7 @@ class ModuleMockerClass {
}

private _generateMock<T, Y extends unknown[]>(
metadata: Mocks.MockFunctionMetadata<T, Y>,
metadata: JestMock.MockFunctionMetadata<T, Y>,
callbacks: Array<Function>,
refs: {
[key: string]:
Expand Down Expand Up @@ -829,7 +855,7 @@ class ModuleMockerClass {
* getMetadata method of this module.
*/
generateFromMetadata<T, Y extends unknown[]>(
_metadata: Mocks.MockFunctionMetadata<T, Y>,
_metadata: JestMock.MockFunctionMetadata<T, Y>,
): Mock<T, Y> {
const callbacks: Function[] = [];
const refs = {};
Expand All @@ -845,7 +871,7 @@ class ModuleMockerClass {
getMetadata<T, Y extends unknown[]>(
component: T,
_refs?: Map<T, number>,
): Mocks.MockFunctionMetadata<T, Y> | null {
): JestMock.MockFunctionMetadata<T, Y> | null {
const refs = _refs || new Map<T, number>();
const ref = refs.get(component);
if (ref != null) {
Expand All @@ -857,7 +883,7 @@ class ModuleMockerClass {
return null;
}

const metadata: Mocks.MockFunctionMetadata<T, Y> = {type};
const metadata: JestMock.MockFunctionMetadata<T, Y> = {type};
if (
type === 'constant' ||
type === 'collection' ||
Expand All @@ -880,7 +906,7 @@ class ModuleMockerClass {
refs.set(component, metadata.refID);

let members: {
[key: string]: Mocks.MockFunctionMetadata<T, Y>;
[key: string]: JestMock.MockFunctionMetadata<T, Y>;
} | null = null;
// Leave arrays alone
if (type !== 'array') {
Expand Down Expand Up @@ -1077,7 +1103,6 @@ class ModuleMockerClass {
}
}

// TODO: bring this type export back once done with TS migration
// export type ModuleMocker = ModuleMockerClass;

export = new ModuleMockerClass(global);
/* eslint-disable-next-line no-redeclare */
const JestMock = new ModuleMockerClass(global);
export = JestMock;
31 changes: 0 additions & 31 deletions packages/jest-types/src/Mocks.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/jest-types/src/index.ts
Expand Up @@ -8,7 +8,6 @@
import * as Config from './Config';
import * as Console from './Console';
import * as Matchers from './Matchers';
import * as Mocks from './Mocks';
import * as PrettyFormat from './PrettyFormat';
import * as Snapshot from './Snapshot';
import * as SourceMaps from './SourceMaps';
Expand All @@ -20,7 +19,6 @@ export {
Config,
Console,
Matchers,
Mocks,
PrettyFormat,
Snapshot,
SourceMaps,
Expand Down