Skip to content

Commit

Permalink
add unit test named snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
bakamitai456 committed Jun 15, 2023
1 parent 7dfe361 commit b684d88
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ Snapshot state must be initialized
Snapshot state has value: undefined
`;

exports[`matcher error toThrowErrorMatchingNamedSnapshot Received value must be a function 1`] = `
<d>expect(</><r>received</><d>).</>toThrowErrorMatchingNamedSnapshot<d>()</>

<b>Matcher error</>: <r>received</> value must be a function

Received has type: number
Received has value: <r>13</>
`;

exports[`matcher error toThrowErrorMatchingNamedSnapshot Snapshot matchers cannot be used with not 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toThrowErrorMatchingNamedSnapshot<d>()</>

<b>Matcher error</>: Snapshot matchers cannot be used with <b>not</>
`;

exports[`matcher error toThrowErrorMatchingSnapshot Received value must be a function 1`] = `
<d>expect(</><r>received</><d>).</>toThrowErrorMatchingSnapshot<d>()</>

Expand Down
40 changes: 40 additions & 0 deletions packages/jest-snapshot/src/__tests__/printSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
toMatchNamedSnapshot,
toMatchSnapshot,
toThrowErrorMatchingInlineSnapshot,
toThrowErrorMatchingNamedSnapshot,
toThrowErrorMatchingSnapshot,
} from '../';
import type SnapshotState from '../State';
Expand Down Expand Up @@ -529,6 +530,45 @@ describe('matcher error', () => {

// Future test: Snapshot hint must be a string
});

describe('toThrowErrorMatchingNamedSnapshot', () => {
test('Received value must be a function', () => {
const context = {
isNot: false,
promise: '',
} as Context;
const received = 13;
const fromPromise = false;

expect(() => {
toThrowErrorMatchingNamedSnapshot.call(
context,
received,
'',
fromPromise,
);
}).toThrowErrorMatchingSnapshot();
});

test('Snapshot matchers cannot be used with not', () => {
const snapshotName = 'to-throw-snapshot';
const context = {
isNot: true,
promise: '',
} as Context;
const received = new Error('received');
const fromPromise = true;

expect(() => {
toThrowErrorMatchingNamedSnapshot.call(
context,
received,
snapshotName,
fromPromise,
);
}).toThrowErrorMatchingSnapshot();
});
});
});

describe('other error', () => {
Expand Down
118 changes: 87 additions & 31 deletions packages/jest-snapshot/src/__tests__/throwMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* LICENSE file in the root directory of this source tree.
*/

import {type Context, toThrowErrorMatchingSnapshot} from '../';
import {
Context,
toThrowErrorMatchingNamedSnapshot,
toThrowErrorMatchingSnapshot,
} from '../';

const mockedMatch = jest.fn(() => ({
actual: 'coconut',
Expand All @@ -20,50 +24,102 @@ afterEach(() => {
jest.clearAllMocks();
});

it('throw matcher can take func', () => {
toThrowErrorMatchingSnapshot.call(
mockedContext,
() => {
throw new Error('coconut');
},
undefined,
false,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'coconut', testName: ''}),
);
});

describe('throw matcher from promise', () => {
it('can take error', () => {
describe('throw matcher can take func', () => {
it('toThrowErrorMatchingSnapshot', () => {
toThrowErrorMatchingSnapshot.call(
mockedContext,
new Error('coco'),
'testName',
true,
() => {
throw new Error('coconut');
},
undefined,
false,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'coco', testName: ''}),
expect.objectContaining({received: 'coconut', testName: ''}),
);
});

it('can take custom error', () => {
class CustomError extends Error {}

toThrowErrorMatchingSnapshot.call(
it('toThrowErrorMatchingNamedSnapshot', () => {
toThrowErrorMatchingNamedSnapshot.call(
mockedContext,
new CustomError('nut'),
'testName',
true,
() => {
throw new Error('coconut');
},
undefined,
false,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'nut', testName: ''}),
expect.objectContaining({received: 'coconut', testName: ''}),
);
});
});

describe('throw matcher from promise', () => {
describe('toThrowErrorMatchingSnapshot', () => {
it('can take error', () => {
toThrowErrorMatchingSnapshot.call(
mockedContext,
new Error('coco'),
'testName',
true,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'coco', testName: ''}),
);
});

it('can take custom error', () => {
class CustomError extends Error {}

toThrowErrorMatchingSnapshot.call(
mockedContext,
new CustomError('nut'),
'testName',
true,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'nut', testName: ''}),
);
});
});

describe('toThrowErrorMatchingNamedSnapshot', () => {
it('can take error', () => {
toThrowErrorMatchingNamedSnapshot.call(
mockedContext,
new Error('coco'),
'snapshot name',
true,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'coco', testName: 'snapshot name'}),
);
});

it('can take custom error', () => {
class CustomError extends Error {}

toThrowErrorMatchingNamedSnapshot.call(
mockedContext,
new CustomError('nut'),
'snapshot name',
true,
);

expect(mockedMatch).toHaveBeenCalledTimes(1);
expect(mockedMatch).toHaveBeenCalledWith(
expect.objectContaining({received: 'nut', testName: 'snapshot name'}),
);
});
});
});

0 comments on commit b684d88

Please sign in to comment.