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: add typecheck for expect tests #13383

Merged
merged 11 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"test": "yarn lint && yarn jest",
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
"typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit",
"typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences}/src/__tests__",
"typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences,expect}/src/__tests__",
"verify-old-ts": "node ./scripts/verifyOldTs.mjs",
"verify-pnp": "node ./scripts/verifyPnP.mjs",
"watch": "yarn build:js && node ./scripts/watch.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,29 @@ Received
Number of returns: <r>3</>
`;

exports[`lastReturnedWith returnedWith incomplete recursive calls are handled properly 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>lastReturnedWith<d>(</><g>expected</><d>)</>

Expected: <g>undefined</>
Received
3: function call has not returned yet
-> 4: function call has not returned yet

Number of returns: <r>0</>
Number of calls: <r>4</>
`;

exports[`lastReturnedWith returnedWith works with more calls than the limit 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>lastReturnedWith<d>(</><g>expected</><d>)</>

Expected: <g>"bar"</>
Received
5: <r>"foo5"</>
-> 6: <r>"foo6"</>

Number of returns: <r>6</>
`;

exports[`lastReturnedWith works only on spies or jest.fn 1`] = `
<d>expect(</><r>received</><d>).</>lastReturnedWith<d>(</><g>expected</><d>)</>

Expand Down Expand Up @@ -2060,6 +2083,29 @@ Received
Number of returns: <r>3</>
`;

exports[`toHaveLastReturnedWith returnedWith incomplete recursive calls are handled properly 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>toHaveLastReturnedWith<d>(</><g>expected</><d>)</>

Expected: <g>undefined</>
Received
3: function call has not returned yet
-> 4: function call has not returned yet

Number of returns: <r>0</>
Number of calls: <r>4</>
`;

exports[`toHaveLastReturnedWith returnedWith works with more calls than the limit 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>toHaveLastReturnedWith<d>(</><g>expected</><d>)</>

Expected: <g>"bar"</>
Received
5: <r>"foo5"</>
-> 6: <r>"foo6"</>

Number of returns: <r>6</>
`;

exports[`toHaveLastReturnedWith works only on spies or jest.fn 1`] = `
<d>expect(</><r>received</><d>).</>toHaveLastReturnedWith<d>(</><g>expected</><d>)</>

Expand Down Expand Up @@ -2720,6 +2766,29 @@ Expected: <g>"foo"</>
Number of returns: <r>0</>
`;

exports[`toHaveReturnedWith lastReturnedWith incomplete recursive calls are handled properly 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>toHaveReturnedWith<d>(</><g>expected</><d>)</>

Expected: <g>0</>
Received
1: function call has not returned yet
2: function call has not returned yet
3: function call has not returned yet

Number of returns: <r>0</>
Number of calls: <r>4</>
`;

exports[`toHaveReturnedWith lastReturnedWith works with three calls 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toHaveReturnedWith<d>(</><g>expected</><d>)</>

Expected: not <g>"foo3"</>
Received
3: <d>"foo3"</>

Number of returns: <r>3</>
`;

exports[`toHaveReturnedWith returnedWith incomplete recursive calls are handled properly 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>toHaveReturnedWith<d>(</><g>expected</><d>)</>

Expand Down Expand Up @@ -3145,6 +3214,29 @@ Expected: <g>"foo"</>
Number of returns: <r>0</>
`;

exports[`toReturnWith lastReturnedWith incomplete recursive calls are handled properly 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>toReturnWith<d>(</><g>expected</><d>)</>

Expected: <g>0</>
Received
1: function call has not returned yet
2: function call has not returned yet
3: function call has not returned yet

Number of returns: <r>0</>
Number of calls: <r>4</>
`;

exports[`toReturnWith lastReturnedWith works with three calls 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>not<d>.</>toReturnWith<d>(</><g>expected</><d>)</>

Expected: not <g>"foo3"</>
Received
3: <d>"foo3"</>

Number of returns: <r>3</>
`;

exports[`toReturnWith returnedWith incomplete recursive calls are handled properly 1`] = `
<d>expect(</><r>jest.fn()</><d>).</>toReturnWith<d>(</><g>expected</><d>)</>

Expand Down
20 changes: 13 additions & 7 deletions packages/expect/src/__tests__/extend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import {equals, iterableEquality, subsetEquality} from '@jest/expect-utils';
import {alignedAnsiStyleSerializer} from '@jest/test-utils';
import * as matcherUtils from 'jest-matcher-utils';
import jestExpect from '../';
import importedExpect from '../';

expect.addSnapshotSerializer(alignedAnsiStyleSerializer);

jestExpect.extend({
importedExpect.extend({
toBeDivisibleBy(actual: number, expected: number) {
const pass = actual % expected === 0;
const message: () => string = pass
Expand Down Expand Up @@ -52,11 +52,6 @@ jestExpect.extend({
});

declare module '../types' {
interface AsymmetricMatchers {
toBeDivisibleBy(expected: number): void;
toBeSymbol(expected: symbol): void;
toBeWithinRange(floor: number, ceiling: number): void;
}
interface Matchers<R> {
toBeDivisibleBy(expected: number): R;
toBeSymbol(expected: symbol): R;
Expand All @@ -69,6 +64,17 @@ declare module '../types' {
}
}

const jestExpect = importedExpect as typeof importedExpect & {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case typings have to be added like this. TypeScript is not happy about merging declaration of AsymmetricMatchers interface – it goes looking for expect object in ../index.ts, does not find properties from the declaration and complains.

not: {
toBeDivisibleBy(expected: number): void;
toBeWithinRange(floor: number, ceiling: number): void;
};

toBeDivisibleBy(expected: number): void;
toBeSymbol(expected: symbol): void;
toBeWithinRange(floor: number, ceiling: number): void;
};

it('is available globally when matcher is unary', () => {
jestExpect(15).toBeDivisibleBy(5);
jestExpect(15).toBeDivisibleBy(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import fc from 'fast-check';
import expect from '..';
import expect from '../';
import {
anythingSettings,
assertSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import fc from 'fast-check';
import expect from '..';
import expect from '../';
import {
anythingSettings,
assertSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import fc from 'fast-check';
import expect from '..';
import expect from '../';
import {
anythingSettings,
assertSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import * as assert from 'assert';
import fc from 'fast-check';
import expect from '..';
import expect from '../';
import {
anythingSettings,
assertSettings,
Expand Down
Loading