Skip to content

Commit

Permalink
chore: migrate type tests to TSTyche 2 (#15088)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed May 27, 2024
1 parent 0d222c1 commit b7ae0b8
Show file tree
Hide file tree
Showing 20 changed files with 431 additions and 449 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"strip-json-comments": "^3.1.1",
"tempy": "^1.0.0",
"ts-node": "^10.5.0",
"tstyche": "^2.0.0-beta.0",
"tstyche": "^2.0.0-beta.1",
"typescript": "^5.0.4",
"webpack": "^5.68.0",
"webpack-node-externals": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/expect-utils/__typetests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test('isA', () => {
}

if (isA<Map<unknown, unknown>>('Map', sample)) {
expect(sample).type.toEqual<Map<unknown, unknown>>();
expect(sample).type.toBe<Map<unknown, unknown>>();
}

if (isA<Set<unknown>>('Set', sample)) {
expect(sample).type.toEqual<Set<unknown>>();
expect(sample).type.toBe<Set<unknown>>();
}
});
62 changes: 32 additions & 30 deletions packages/expect/__typetests__/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ describe('Expect', () => {
const tester1: Tester = function (a, b, customTesters) {
expect(a).type.toBeAny();
expect(b).type.toBeAny();
expect(customTesters).type.toEqual<Array<Tester>>();
expect(this).type.toEqual<TesterContext>();
expect(this.equals).type.toEqual<EqualsFunction>();
expect(customTesters).type.toBe<Array<Tester>>();
expect(this).type.toBe<TesterContext>();
expect(this.equals).type.toBe<EqualsFunction>();

return undefined;
};
Expand All @@ -57,7 +57,7 @@ describe('Expect', () => {
(a, b, customTesters) => {
expect(a).type.toBeAny();
expect(b).type.toBeAny();
expect(customTesters).type.toEqual<Array<Tester>>();
expect(customTesters).type.toBe<Array<Tester>>();
expect(this).type.toBeUndefined();

return true;
Expand All @@ -66,9 +66,9 @@ describe('Expect', () => {
function anotherTester(a, b, customTesters) {
expect(a).type.toBeAny();
expect(b).type.toBeAny();
expect(customTesters).type.toEqual<Array<Tester>>();
expect(this).type.toEqual<TesterContext>();
expect(this.equals).type.toEqual<EqualsFunction>();
expect(customTesters).type.toBe<Array<Tester>>();
expect(this).type.toBe<TesterContext>();
expect(this.equals).type.toBe<EqualsFunction>();

return undefined;
},
Expand All @@ -87,26 +87,26 @@ describe('Expect', () => {
// TODO `actual` should be allowed to have only `unknown` type
toBeWithinRange(actual: number, floor: number, ceiling: number) {
expect(this.assertionCalls).type.toBeNumber();
expect(this.currentTestName).type.toEqual<string | undefined>();
expect(this.customTesters).type.toEqual<Array<Tester>>();
expect(this.dontThrow).type.toEqual<() => void>();
expect(this.error).type.toEqual<Error | undefined>();
expect(this.equals).type.toEqual<EqualsFunction>();
expect(this.expand).type.toEqual<boolean | undefined>();
expect(this.expectedAssertionsNumber).type.toEqual<number | null>();
expect(this.expectedAssertionsNumberError).type.toEqual<
expect(this.currentTestName).type.toBe<string | undefined>();
expect(this.customTesters).type.toBe<Array<Tester>>();
expect(this.dontThrow).type.toBe<() => void>();
expect(this.error).type.toBe<Error | undefined>();
expect(this.equals).type.toBe<EqualsFunction>();
expect(this.expand).type.toBe<boolean | undefined>();
expect(this.expectedAssertionsNumber).type.toBe<number | null>();
expect(this.expectedAssertionsNumberError).type.toBe<
Error | undefined
>();
expect(this.isExpectingAssertions).type.toBeBoolean();
expect(this.isExpectingAssertionsError).type.toEqual<
expect(this.isExpectingAssertionsError).type.toBe<
Error | undefined
>();
expect(this.isNot).type.toEqual<boolean | undefined>();
expect(this.isNot).type.toBe<boolean | undefined>();
expect(this.numPassingAsserts).type.toBeNumber();
expect(this.promise).type.toEqual<string | undefined>();
expect(this.suppressedErrors).type.toEqual<Array<Error>>();
expect(this.testPath).type.toEqual<string | undefined>();
expect(this.utils).type.toEqual<MatcherUtils>();
expect(this.promise).type.toBe<string | undefined>();
expect(this.suppressedErrors).type.toBe<Array<Error>>();
expect(this.testPath).type.toBe<string | undefined>();
expect(this.utils).type.toBe<MatcherUtils>();

const pass = actual >= floor && actual <= ceiling;
if (pass) {
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('MatcherFunction', () => {
};
};

expect<ToBeWithinRange>().type.toBeAssignable(toBeWithinRange);
expect<ToBeWithinRange>().type.toBeAssignableWith(toBeWithinRange);
});

test('requires the `actual` argument to be of type `unknown`', () => {
Expand All @@ -173,7 +173,7 @@ describe('MatcherFunction', () => {
};
};

expect<MatcherFunction>().type.not.toBeAssignable(actualMustBeUnknown);
expect<MatcherFunction>().type.not.toBeAssignableWith(actualMustBeUnknown);
});

test('allows omitting the `expected` argument', () => {
Expand All @@ -193,7 +193,9 @@ describe('MatcherFunction', () => {
};
};

expect<AllowOmittingExpected>().type.toBeAssignable(allowOmittingExpected);
expect<AllowOmittingExpected>().type.toBeAssignableWith(
allowOmittingExpected,
);
});

test('the `message` property is required in the return type', () => {
Expand All @@ -203,7 +205,7 @@ describe('MatcherFunction', () => {
};
};

expect<MatcherFunction>().type.not.toBeAssignable(lacksMessage);
expect<MatcherFunction>().type.not.toBeAssignableWith(lacksMessage);
});

test('the `pass` property is required in the return type', () => {
Expand All @@ -213,15 +215,15 @@ describe('MatcherFunction', () => {
};
};

expect<MatcherFunction>().type.not.toBeAssignable(lacksPass);
expect<MatcherFunction>().type.not.toBeAssignableWith(lacksPass);
});

test('context is defined inside a matcher function', () => {
const toHaveContext: MatcherFunction = function (
actual: unknown,
...expected: Array<unknown>
) {
expect(this).type.toEqual<MatcherContext>();
expect(this).type.toBe<MatcherContext>();

if (expected.length > 0) {
throw new Error('This matcher does not take any expected argument.');
Expand All @@ -243,7 +245,7 @@ describe('MatcherFunction', () => {
actual: unknown,
...expected: Array<unknown>
) {
expect(this).type.toEqual<CustomContext>();
expect(this).type.toBe<CustomContext>();
expect(this.customMethod()).type.toBeVoid();

if (expected.length > 0) {
Expand Down Expand Up @@ -272,7 +274,7 @@ describe('MatcherFunction', () => {
CustomContext,
[count: number]
> = function (actual: unknown, count: unknown) {
expect(this).type.toEqual<CustomContext>();
expect(this).type.toBe<CustomContext>();
expect(this.customMethod()).type.toBeVoid();

return {
Expand All @@ -281,7 +283,7 @@ describe('MatcherFunction', () => {
};
};

expect<CustomStateAndExpected>().type.toBeAssignable(
expect<CustomStateAndExpected>().type.toBeAssignableWith(
customContextAndExpected,
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/__typetests__/jest-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import type {Options} from 'yargs';
import {yargsOptions} from 'jest-cli';

test('yargsOptions', () => {
expect(yargsOptions).type.toEqual<{[key: string]: Options}>();
expect(yargsOptions).type.toBe<{[key: string]: Options}>();
});
56 changes: 27 additions & 29 deletions packages/jest-mock/__typetests__/Mocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ describe('Mocked', () => {

const MockSomeClass = SomeClass as Mocked<typeof SomeClass>;

expect(MockSomeClass.mock.calls[0]).type.toEqual<
expect(MockSomeClass.mock.calls[0]).type.toBe<
[one: string, two?: boolean]
>();

expect(MockSomeClass.prototype.methodA.mock.calls[0]).type.toEqual<[]>();
expect(MockSomeClass.prototype.methodB.mock.calls[0]).type.toEqual<
expect(MockSomeClass.prototype.methodA.mock.calls[0]).type.toBe<[]>();
expect(MockSomeClass.prototype.methodB.mock.calls[0]).type.toBe<
[a: string, b?: number]
>();

Expand All @@ -43,19 +43,19 @@ describe('Mocked', () => {
),
).type.toRaiseError();

expect(MockSomeClass.mock.instances[0].methodA.mock.calls[0]).type.toEqual<
expect(MockSomeClass.mock.instances[0].methodA.mock.calls[0]).type.toBe<
[]
>();
expect(MockSomeClass.prototype.methodB.mock.calls[0]).type.toEqual<
expect(MockSomeClass.prototype.methodB.mock.calls[0]).type.toBe<
[a: string, b?: number]
>();

const mockSomeInstance = new MockSomeClass('a') as Mocked<
InstanceType<typeof MockSomeClass>
>;

expect(mockSomeInstance.methodA.mock.calls[0]).type.toEqual<[]>();
expect(mockSomeInstance.methodB.mock.calls[0]).type.toEqual<
expect(mockSomeInstance.methodA.mock.calls[0]).type.toBe<[]>();
expect(mockSomeInstance.methodB.mock.calls[0]).type.toBe<
[a: string, b?: number]
>();

Expand All @@ -68,7 +68,7 @@ describe('Mocked', () => {
}),
).type.toRaiseError();

expect(new SomeClass('sample')).type.toBeAssignable(mockSomeInstance);
expect(new SomeClass('sample')).type.toBeAssignableWith(mockSomeInstance);
});

test('wraps a function type with type definitions of the Jest mock function', () => {
Expand All @@ -78,14 +78,14 @@ describe('Mocked', () => {

const mockFunction = someFunction as Mocked<typeof someFunction>;

expect(mockFunction.mock.calls[0]).type.toEqual<[a: string, b?: number]>();
expect(mockFunction.mock.calls[0]).type.toBe<[a: string, b?: number]>();

expect(mockFunction.mockReturnValue(123)).type.toRaiseError();
expect(
mockFunction.mockImplementation((a: boolean, b?: number) => true),
).type.toRaiseError();

expect(someFunction).type.toBeAssignable(mockFunction);
expect(someFunction).type.toBeAssignableWith(mockFunction);
});

test('wraps an async function type with type definitions of the Jest mock function', () => {
Expand All @@ -97,7 +97,7 @@ describe('Mocked', () => {
typeof someAsyncFunction
>;

expect(mockAsyncFunction.mock.calls[0]).type.toEqual<[Array<boolean>]>();
expect(mockAsyncFunction.mock.calls[0]).type.toBe<[Array<boolean>]>();

expect(mockAsyncFunction.mockResolvedValue(123)).type.toRaiseError();
expect(
Expand All @@ -106,7 +106,7 @@ describe('Mocked', () => {
),
).type.toRaiseError();

expect(someAsyncFunction).type.toBeAssignable(mockAsyncFunction);
expect(someAsyncFunction).type.toBeAssignableWith(mockAsyncFunction);
});

test('wraps a function object type with type definitions of the Jest mock function', () => {
Expand All @@ -126,7 +126,7 @@ describe('Mocked', () => {
typeof someFunctionObject
>;

expect(mockFunctionObject.mock.calls[0]).type.toEqual<
expect(mockFunctionObject.mock.calls[0]).type.toBe<
[a: number, b?: string]
>();

Expand All @@ -135,7 +135,7 @@ describe('Mocked', () => {
mockFunctionObject.mockImplementation(() => true),
).type.toRaiseError();

expect(mockFunctionObject.one.more.time.mock.calls[0]).type.toEqual<
expect(mockFunctionObject.one.more.time.mock.calls[0]).type.toBe<
[time: number]
>();

Expand All @@ -148,7 +148,7 @@ describe('Mocked', () => {
}),
).type.toRaiseError();

expect(someFunctionObject).type.toBeAssignable(mockFunctionObject);
expect(someFunctionObject).type.toBeAssignableWith(mockFunctionObject);
});

test('wraps an object type with type definitions of the Jest mock function', () => {
Expand Down Expand Up @@ -190,26 +190,24 @@ describe('Mocked', () => {

const mockObject = someObject as Mocked<typeof someObject>;

expect(mockObject.methodA.mock.calls[0]).type.toEqual<[]>();
expect(mockObject.methodB.mock.calls[0]).type.toEqual<[b: string]>();
expect(mockObject.methodC.mock.calls[0]).type.toEqual<[c: number]>();
expect(mockObject.methodA.mock.calls[0]).type.toBe<[]>();
expect(mockObject.methodB.mock.calls[0]).type.toBe<[b: string]>();
expect(mockObject.methodC.mock.calls[0]).type.toBe<[c: number]>();

expect(mockObject.one.more.time.mock.calls[0]).type.toEqual<[t: number]>();
expect(mockObject.one.more.time.mock.calls[0]).type.toBe<[t: number]>();

expect(mockObject.SomeClass.mock.calls[0]).type.toEqual<
expect(mockObject.SomeClass.mock.calls[0]).type.toBe<
[one: string, two?: boolean]
>();
expect(mockObject.SomeClass.prototype.methodA.mock.calls[0]).type.toEqual<
expect(mockObject.SomeClass.prototype.methodA.mock.calls[0]).type.toBe<
[]
>();
expect(mockObject.SomeClass.prototype.methodB.mock.calls[0]).type.toEqual<
expect(mockObject.SomeClass.prototype.methodB.mock.calls[0]).type.toBe<
[a: string, b?: number]
>();

expect(mockObject.someClassInstance.methodA.mock.calls[0]).type.toEqual<
[]
>();
expect(mockObject.someClassInstance.methodB.mock.calls[0]).type.toEqual<
expect(mockObject.someClassInstance.methodA.mock.calls[0]).type.toBe<[]>();
expect(mockObject.someClassInstance.methodB.mock.calls[0]).type.toBe<
[a: string, b?: number]
>();

Expand Down Expand Up @@ -265,16 +263,16 @@ describe('Mocked', () => {
),
).type.toRaiseError();

expect(someObject).type.toBeAssignable(mockObject);
expect(someObject).type.toBeAssignableWith(mockObject);
});

test('wraps the global `console` object type with type definitions of the Jest mock function', () => {
const mockConsole = console as Mocked<typeof console>;

expect(console.log).type.toBeAssignable(
expect(console.log).type.toBeAssignableWith(
mockConsole.log.mockImplementation(() => {}),
);
expect<MockInstance<typeof console.log>>().type.toBeAssignable(
expect<MockInstance<typeof console.log>>().type.toBeAssignableWith(
mockConsole.log.mockImplementation(() => {}),
);
});
Expand Down
18 changes: 9 additions & 9 deletions packages/jest-mock/__typetests__/ModuleMocker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,32 @@ const moduleMocker = new ModuleMocker(globalThis);
const exampleMetadata = moduleMocker.getMetadata(exampleModule);

test('getMetadata', () => {
expect(exampleMetadata).type.toEqual<MockMetadata<
expect(exampleMetadata).type.toBe<MockMetadata<
typeof exampleModule
> | null>();
});

test('generateFromMetadata', () => {
const exampleMock = moduleMocker.generateFromMetadata(exampleMetadata!);

expect(exampleMock).type.toEqual<Mocked<typeof exampleModule>>();
expect(exampleMock).type.toBe<Mocked<typeof exampleModule>>();

expect(exampleMock.methodA.mock.calls).type.toEqual<
expect(exampleMock.methodA.mock.calls).type.toBe<
Array<[a: number, b: number]>
>();
expect(exampleMock.methodB.mock.calls).type.toEqual<
expect(exampleMock.methodB.mock.calls).type.toBe<
Array<[a: number, b: number]>
>();

expect(exampleMock.instance.memberA).type.toEqual<Array<number>>();
expect(exampleMock.instance.memberB.mock.calls).type.toEqual<Array<[]>>();
expect(exampleMock.instance.memberA).type.toBe<Array<number>>();
expect(exampleMock.instance.memberB.mock.calls).type.toBe<Array<[]>>();

expect(exampleMock.propertyA.one).type.toBeString();
expect(exampleMock.propertyA.two.mock.calls).type.toEqual<Array<[]>>();
expect(exampleMock.propertyA.two.mock.calls).type.toBe<Array<[]>>();
expect(exampleMock.propertyA.three.nine).type.toBeNumber();
expect(exampleMock.propertyA.three.ten).type.toEqual<Array<number>>();
expect(exampleMock.propertyA.three.ten).type.toBe<Array<number>>();

expect(exampleMock.propertyB).type.toEqual<Array<number>>();
expect(exampleMock.propertyB).type.toBe<Array<number>>();
expect(exampleMock.propertyC).type.toBeNumber();
expect(exampleMock.propertyD).type.toBeString();
expect(exampleMock.propertyE).type.toBeBoolean();
Expand Down
Loading

0 comments on commit b7ae0b8

Please sign in to comment.