Skip to content

Commit

Permalink
Add snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuibu committed Nov 15, 2022
1 parent def8b86 commit 963becf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Expand Up @@ -146,6 +146,24 @@ Received message: <r>"apple"</>
<d>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<d>:24:74)</>
`;

exports[`toThrow error message and cause fail isNot false, incorrect message 1`] = `
<d>expect(</><r>received</><d>).</>toThrow<d>(</><g>expected</><d>)</>

Expected message and cause: <g>"{ message: <i>goo</i>d, cause: { message: B, cause: { message: A }}}"</>
Received message and cause: <r>"{ message: <i>ba</i>d, cause: { message: B, cause: { message: A }}}"</>

<d>at </>packages/expect/src/__tests__/toThrowMatchers.test.ts<d>:315:21</>
`;

exports[`toThrow error message and cause fail isNot true, incorrect cause 1`] = `
<d>expect(</><r>received</><d>).</>toThrow<d>(</><g>expected</><d>)</>

Expected message and cause: <g>"{ message: good, cause: { message:<i> B, cause: { message:</i> A }}<i>}</i>"</>
Received message and cause: <r>"{ message: good, cause: { message: A }}"</>

<d>at </>packages/expect/src/__tests__/toThrowMatchers.test.ts<d>:323:21</>
`;

exports[`toThrow error-message fail isNot false 1`] = `
<d>expect(</><r>received</><d>).</>toThrow<d>(</><g>expected</><d>)</>

Expand Down Expand Up @@ -454,6 +472,24 @@ Received message: <r>"apple"</>
<d>at jestExpect (</>packages/expect/src/__tests__/toThrowMatchers-test.js<d>:24:74)</>
`;

exports[`toThrowError error message and cause fail isNot false, incorrect message 1`] = `
<d>expect(</><r>received</><d>).</>toThrowError<d>(</><g>expected</><d>)</>

Expected message and cause: <g>"{ message: <i>goo</i>d, cause: { message: B, cause: { message: A }}}"</>
Received message and cause: <r>"{ message: <i>ba</i>d, cause: { message: B, cause: { message: A }}}"</>

<d>at </>packages/expect/src/__tests__/toThrowMatchers.test.ts<d>:315:21</>
`;

exports[`toThrowError error message and cause fail isNot true, incorrect cause 1`] = `
<d>expect(</><r>received</><d>).</>toThrowError<d>(</><g>expected</><d>)</>

Expected message and cause: <g>"{ message: good, cause: { message:<i> B, cause: { message:</i> A }}<i>}</i>"</>
Received message and cause: <r>"{ message: good, cause: { message: A }}"</>

<d>at </>packages/expect/src/__tests__/toThrowMatchers.test.ts<d>:323:21</>
`;

exports[`toThrowError error-message fail isNot false 1`] = `
<d>expect(</><r>received</><d>).</>toThrowError<d>(</><g>expected</><d>)</>

Expand Down
27 changes: 23 additions & 4 deletions packages/expect/src/__tests__/toThrowMatchers.test.ts
Expand Up @@ -297,16 +297,35 @@ describe.each(['toThrowError', 'toThrow'] as const)('%s', toThrow => {
});

test('isNot true, incorrect cause', () => {
// less than v16 does not yet support Error.cause
if (Number(process.version.split('.')[0].slice(1)) < 16) {
expect(true).toBe(true);
} else {
// only v16 or higher support Error.cause
if (Number(process.version.split('.')[0].slice(1)) >= 16) {
jestExpect(() => {
throw new Error('good', {cause: errorA});
}).not[toThrow](expected);
}
});
});

describe('fail', () => {
// only v16 or higher support Error.cause
if (Number(process.version.split('.')[0].slice(1)) >= 16) {
test('isNot false, incorrect message', () => {
expect(() =>
jestExpect(() => {
throw new Error('bad', {cause: errorB});
})[toThrow](expected),
).toThrowErrorMatchingSnapshot();
});

test('isNot true, incorrect cause', () => {
expect(() =>
jestExpect(() => {
throw new Error('good', {cause: errorA});
})[toThrow](expected),
).toThrowErrorMatchingSnapshot();
});
}
});
});

describe('asymmetric', () => {
Expand Down

0 comments on commit 963becf

Please sign in to comment.