-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
fix(jest-snapshot): fix typings of snapshot matchers #13240
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
* 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 {expectError, expectType} from 'tsd-lite'; | ||
import type {ExpectationResult} from 'expect'; | ||
import { | ||
Context, | ||
SnapshotState, | ||
toMatchInlineSnapshot, | ||
toMatchSnapshot, | ||
toThrowErrorMatchingInlineSnapshot, | ||
toThrowErrorMatchingSnapshot, | ||
} from 'jest-snapshot'; | ||
|
||
// Context | ||
|
||
expectType<SnapshotState>(({} as Context).snapshotState); | ||
|
||
// toMatchSnapshot | ||
|
||
expectType<ExpectationResult>( | ||
toMatchSnapshot.call({} as Context, {received: 'value'}), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toMatchSnapshot.call({} as Context, {received: 'value'}, 'someHint'), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toMatchSnapshot.call({} as Context, {received: 'value'}, {property: 'match'}), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toMatchSnapshot.call( | ||
{} as Context, | ||
{received: 'value'}, | ||
{property: 'match'}, | ||
'someHint', | ||
), | ||
); | ||
|
||
expectError(toMatchSnapshot({received: 'value'})); | ||
|
||
// toMatchInlineSnapshot | ||
|
||
expectType<ExpectationResult>( | ||
toMatchInlineSnapshot.call({} as Context, {received: 'value'}), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toMatchInlineSnapshot.call( | ||
{} as Context, | ||
{received: 'value'}, | ||
'inlineSnapshot', | ||
), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toMatchInlineSnapshot.call( | ||
{} as Context, | ||
{received: 'value'}, | ||
{property: 'match'}, | ||
), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toMatchInlineSnapshot.call( | ||
{} as Context, | ||
{received: 'value'}, | ||
{property: 'match'}, | ||
'inlineSnapshot', | ||
), | ||
); | ||
|
||
expectError(toMatchInlineSnapshot({received: 'value'})); | ||
|
||
// toThrowErrorMatchingSnapshot | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingSnapshot.call({} as Context, new Error('received')), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingSnapshot.call( | ||
{} as Context, | ||
new Error('received'), | ||
'someHint', | ||
), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingSnapshot.call( | ||
{} as Context, | ||
new Error('received'), | ||
'someHint', | ||
true, // fromPromise | ||
), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingSnapshot.call( | ||
{} as Context, | ||
new Error('received'), | ||
undefined, | ||
false, // fromPromise | ||
), | ||
); | ||
|
||
expectError(toThrowErrorMatchingSnapshot({received: 'value'})); | ||
|
||
// toThrowErrorMatchingInlineSnapshot | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingInlineSnapshot.call({} as Context, new Error('received')), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingInlineSnapshot.call( | ||
{} as Context, | ||
new Error('received'), | ||
'inlineSnapshot', | ||
), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingInlineSnapshot.call( | ||
{} as Context, | ||
new Error('received'), | ||
'inlineSnapshot', | ||
true, // fromPromise | ||
), | ||
); | ||
|
||
expectType<ExpectationResult>( | ||
toThrowErrorMatchingInlineSnapshot.call( | ||
{} as Context, | ||
new Error('received'), | ||
undefined, | ||
false, // fromPromise | ||
), | ||
); | ||
|
||
expectError(toThrowErrorMatchingInlineSnapshot({received: 'value'})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,59 +5,65 @@ | |
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {toThrowErrorMatchingSnapshot} from '..'; | ||
import {type Context, toThrowErrorMatchingSnapshot} from '../'; | ||
|
||
let matchFn: jest.Mock; | ||
const mockedMatch = jest.fn(() => ({ | ||
actual: 'coconut', | ||
expected: 'coconut', | ||
})); | ||
|
||
beforeEach(() => { | ||
matchFn = jest.fn(() => ({ | ||
actual: 'coconut', | ||
expected: 'coconut', | ||
})); | ||
const mockedContext = { | ||
snapshotState: {match: mockedMatch}, | ||
} as unknown as Context; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('throw matcher can take func', () => { | ||
const throwMatcher = toThrowErrorMatchingSnapshot.bind({ | ||
snapshotState: {match: matchFn}, | ||
}); | ||
|
||
throwMatcher( | ||
toThrowErrorMatchingSnapshot.call( | ||
mockedContext, | ||
() => { | ||
throw new Error('coconut'); | ||
}, | ||
undefined, | ||
false, | ||
); | ||
|
||
expect(matchFn).toHaveBeenCalledWith( | ||
expect(mockedMatch).toBeCalledTimes(1); | ||
expect(mockedMatch).toHaveBeenCalledWith( | ||
expect.objectContaining({received: 'coconut', testName: ''}), | ||
); | ||
}); | ||
|
||
describe('throw matcher from promise', () => { | ||
let throwMatcher: typeof toThrowErrorMatchingSnapshot; | ||
|
||
beforeEach(() => { | ||
throwMatcher = toThrowErrorMatchingSnapshot.bind({ | ||
snapshotState: {match: matchFn}, | ||
}); | ||
}); | ||
Comment on lines
-40
to
-44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
it('can take error', () => { | ||
throwMatcher(new Error('coconut'), 'testName', true); | ||
toThrowErrorMatchingSnapshot.call( | ||
mockedContext, | ||
new Error('coco'), | ||
'testName', | ||
true, | ||
); | ||
|
||
expect(matchFn).toHaveBeenCalledWith( | ||
expect.objectContaining({received: 'coconut', testName: ''}), | ||
expect(mockedMatch).toBeCalledTimes(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be sure that mock was cleared. |
||
expect(mockedMatch).toHaveBeenCalledWith( | ||
expect.objectContaining({received: 'coco', testName: ''}), | ||
); | ||
}); | ||
|
||
it('can take custom error', () => { | ||
class CustomError extends Error {} | ||
|
||
throwMatcher(new CustomError('coconut'), 'testName', true); | ||
toThrowErrorMatchingSnapshot.call( | ||
mockedContext, | ||
new CustomError('nut'), | ||
'testName', | ||
true, | ||
); | ||
|
||
expect(matchFn).toHaveBeenCalledWith( | ||
expect.objectContaining({received: 'coconut', testName: ''}), | ||
expect(mockedMatch).toBeCalledTimes(1); | ||
expect(mockedMatch).toHaveBeenCalledWith( | ||
expect.objectContaining({received: 'nut', testName: ''}), | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used those in type tests, but in a way they can be useful to defined matcher functions (
ExpectationResult
) or to implement snapshot matchers (Context
).