diff --git a/src/matchers/toReject.js b/src/matchers/toReject.js index 8305fc14..cf460709 100644 --- a/src/matchers/toReject.js +++ b/src/matchers/toReject.js @@ -1,16 +1,31 @@ export async function toReject(actual) { - const { matcherHint } = this.utils; + const { matcherHint, printReceived } = this.utils; - const pass = await actual.then( - () => false, - () => true, - ); + const expectedToResolveButGotRejectedMessage = (value, { isNot } = { isNot: false }) => + matcherHint('toReject', 'received', '', { isNot, promise: true }) + + '\n\n' + + 'Expected promise to resolve, however it rejected with: "' + + printReceived(value) + + '".\n'; + + const expectedToRejectButGotResolvedMessage = (value, { isNot } = { isNot: false }) => + matcherHint('toReject', 'received', '', { isNot, promise: true }) + + '\n\n' + + 'Expected promise to reject, however it resolved with: "' + + printReceived(value) + + '".\n'; - return { - pass, - message: () => - pass - ? matcherHint('.not.toReject', 'received', '') + '\n\nExpected promise to resolve, however it rejected.\n' - : matcherHint('.toReject', 'received', '') + '\n\nExpected promise to reject, however it resolved.\n', - }; + return actual.then( + value => ({ + pass: false, + message: () => expectedToRejectButGotResolvedMessage(value, { isNot: this.isNot }), + }), + value => ({ + pass: true, + message: () => + this.isNot + ? expectedToResolveButGotRejectedMessage(value, { isNot: this.isNot }) + : expectedToRejectButGotResolvedMessage(value, { isNot: this.isNot }), + }), + ); } diff --git a/src/matchers/toResolve.js b/src/matchers/toResolve.js index ba9a663f..bdc66749 100644 --- a/src/matchers/toResolve.js +++ b/src/matchers/toResolve.js @@ -1,16 +1,31 @@ export async function toResolve(actual) { - const { matcherHint } = this.utils; + const { matcherHint, printReceived } = this.utils; - const pass = await actual.then( - () => true, - () => false, - ); + const expectedToResolveButGotRejectedMessage = (value, { isNot } = { isNot: false }) => + matcherHint('toResolve', 'received', '', { isNot, promise: true }) + + '\n\n' + + 'Expected promise to resolve, however it rejected with: "' + + printReceived(value) + + '".\n'; + + const expectedToRejectButGotResolvedMessage = (value, { isNot } = { isNot: false }) => + matcherHint('toResolve', 'received', '', { isNot, promise: true }) + + '\n\n' + + 'Expected promise to reject, however it resolved with: "' + + printReceived(value) + + '".\n'; - return { - pass, - message: () => - pass - ? matcherHint('.not.toResolve', 'received', '') + '\n\nExpected promise to reject, however it resolved.\n' - : matcherHint('.toResolve', 'received', '') + '\n\nExpected promise to resolve, however it rejected.\n', - }; + return actual.then( + value => ({ + pass: true, + message: () => + this.isNot + ? expectedToRejectButGotResolvedMessage(value, { isNot: this.isNot }) + : expectedToResolveButGotRejectedMessage(value, { isNot: this.isNot }), + }), + value => ({ + pass: false, + message: () => expectedToResolveButGotRejectedMessage(value, { isNot: this.isNot }), + }), + ); } diff --git a/test/matchers/__snapshots__/toReject.test.js.snap b/test/matchers/__snapshots__/toReject.test.js.snap index ac609696..3ac99fe4 100644 --- a/test/matchers/__snapshots__/toReject.test.js.snap +++ b/test/matchers/__snapshots__/toReject.test.js.snap @@ -1,15 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`.not.toReject fails when passed a promise that rejects 1`] = ` -"expect(received).not.toReject() +"expect(received).true.not.toReject() -Expected promise to resolve, however it rejected. +Expected promise to resolve, however it rejected with: "{"abc": "something went wrong"}". " `; exports[`.toReject fails when passed a promise that resolved 1`] = ` -"expect(received).toReject() +"expect(received).true.toReject() -Expected promise to reject, however it resolved. +Expected promise to reject, however it resolved with: "123". " `; diff --git a/test/matchers/__snapshots__/toResolve.test.js.snap b/test/matchers/__snapshots__/toResolve.test.js.snap index 5cbf0789..e7bcacd7 100644 --- a/test/matchers/__snapshots__/toResolve.test.js.snap +++ b/test/matchers/__snapshots__/toResolve.test.js.snap @@ -1,15 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`.not.toResolve fails when passed a promise that resolved 1`] = ` -"expect(received).not.toResolve() +"expect(received).true.not.toResolve() -Expected promise to reject, however it resolved. +Expected promise to reject, however it resolved with: "{"result": 123}". " `; exports[`.toResolve fails when passed a promise that rejects 1`] = ` -"expect(received).toResolve() +"expect(received).true.toResolve() -Expected promise to resolve, however it rejected. +Expected promise to resolve, however it rejected with: "[Error: something went wrong]". " `; diff --git a/test/matchers/toReject.test.js b/test/matchers/toReject.test.js index 8c0326e8..46abac0a 100644 --- a/test/matchers/toReject.test.js +++ b/test/matchers/toReject.test.js @@ -9,14 +9,14 @@ describe('.toReject', () => { }); test('fails when passed a promise that resolved', async () => { - const promise = Promise.resolve(); + const promise = Promise.resolve(123); await expect(expect(promise).toReject()).rejects.toThrowErrorMatchingSnapshot(); }); }); describe('.not.toReject', () => { test('fails when passed a promise that rejects', async () => { - const promise = Promise.reject(); + const promise = Promise.reject({ abc: 'something went wrong' }); await expect(expect(promise).not.toReject()).rejects.toThrowErrorMatchingSnapshot(); }); diff --git a/test/matchers/toResolve.test.js b/test/matchers/toResolve.test.js index 1f03bc2b..26d31148 100644 --- a/test/matchers/toResolve.test.js +++ b/test/matchers/toResolve.test.js @@ -9,14 +9,14 @@ describe('.toResolve', () => { }); test('fails when passed a promise that rejects', async () => { - const promise = Promise.reject(); + const promise = Promise.reject(new Error('something went wrong')); await expect(expect(promise).toResolve()).rejects.toThrowErrorMatchingSnapshot(); }); }); describe('.not.toResolve', () => { test('fails when passed a promise that resolved', async () => { - const promise = Promise.resolve(); + const promise = Promise.resolve({ result: 123 }); await expect(expect(promise).not.toResolve()).rejects.toThrowErrorMatchingSnapshot(); }); diff --git a/website/docs/getting-started/setup.md b/website/docs/getting-started/setup.md index 0eb9e8d0..2dc2e904 100644 --- a/website/docs/getting-started/setup.md +++ b/website/docs/getting-started/setup.md @@ -11,7 +11,6 @@ sidebar_position: 2 Create a setup script with the following: ```javascript title="testSetup.js" - // add all jest-extended matchers import * as matchers from 'jest-extended'; expect.extend(matchers); @@ -42,8 +41,8 @@ To automatically extend `expect` with all matchers, you can use `jest-extended` works with `vitest` because their `expect.extend` API is compatible. In your setup script: ```javascript title="testSetup.js" -import {expect} from "vitest"; -import * as matchers from "jest-extended"; +import { expect } from 'vitest'; +import * as matchers from 'jest-extended'; expect.extend(matchers); ``` @@ -52,7 +51,7 @@ Add this setup script to your `vitest.config.js`: ```javascript title="vitest.config.js" export default defineConfig({ test: { - setupFiles: ["./testSetup.js"], + setupFiles: ['./testSetup.js'], }, }); ```