From 271a222b655b0a04f50083d3ce49e41ea566251e Mon Sep 17 00:00:00 2001 From: Harel Moshe Date: Mon, 8 Aug 2022 13:43:46 +0300 Subject: [PATCH 1/7] Simple rejection case --- src/matchers/toResolve.js | 42 ++++++++++++++----- .../__snapshots__/toResolve.test.js.snap | 10 ++--- test/matchers/toResolve.test.js | 4 +- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/src/matchers/toResolve.js b/src/matchers/toResolve.js index 4ec0ae9f..d3daff54 100644 --- a/src/matchers/toResolve.js +++ b/src/matchers/toResolve.js @@ -1,16 +1,38 @@ export async function toResolve(actual) { const { matcherHint } = this.utils; - const passMessage = - matcherHint('.not.toResolve', 'received', '') + '\n\n' + 'Expected promise to reject, however it resolved.\n'; - - const failMessage = - matcherHint('.toResolve', 'received', '') + '\n\n' + 'Expected promise to resolve, however it rejected.\n'; - - const pass = await actual.then( - () => true, - () => false, + const matcherResult = await actual.then( + value => ({ + pass: true, + message: () => + this.isNot + ? matcherHint('toResolve', 'received', '', { isNot: true, promise: true }) + + '\n\n' + + 'Expected promise to reject, however it resolved with: "' + + value + + '".\n' + : matcherHint('toResolve', 'received', '', { isNot: false, promise: true }) + + '\n\n' + + 'Expected promise to resolve, however it rejected with: "' + + value + + '".\n', + }), + value => ({ + pass: false, + message: () => + this.isNot + ? matcherHint('toResolve', 'received', '', { isNot: true, promise: true }) + + '\n\n' + + 'Expected promise to resolve, however it rejected with: "' + + value + + '".\n' + : matcherHint('toResolve', 'received', '', { isNot: false, promise: true }) + + '\n\n' + + 'Expected promise to resolve, however it rejected with: "' + + value + + '".\n', + }), ); - return { pass, message: () => (pass ? passMessage : failMessage) }; + return matcherResult; } diff --git a/test/matchers/__snapshots__/toResolve.test.js.snap b/test/matchers/__snapshots__/toResolve.test.js.snap index 5cbf0789..84b108ef 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: \\"undefined\\". " `; -exports[`.toResolve fails when passed a promise that rejects 1`] = ` -"expect(received).toResolve() +exports[`.toResolve fails when passed a promise that rejects with a primitive 1`] = ` +"expect(received).true.toResolve() -Expected promise to resolve, however it rejected. +Expected promise to resolve, however it rejected with: \\"something went wrong\\". " `; diff --git a/test/matchers/toResolve.test.js b/test/matchers/toResolve.test.js index 1f03bc2b..6536a32f 100644 --- a/test/matchers/toResolve.test.js +++ b/test/matchers/toResolve.test.js @@ -8,8 +8,8 @@ describe('.toResolve', () => { await expect(promise).toResolve(); }); - test('fails when passed a promise that rejects', async () => { - const promise = Promise.reject(); + test('fails when passed a promise that rejects with a primitive', async () => { + const promise = Promise.reject('something went wrong'); await expect(expect(promise).toResolve()).rejects.toThrowErrorMatchingSnapshot(); }); }); From 5c7a3b730b58b1a1cb3c20799fc6b1ffce66d368 Mon Sep 17 00:00:00 2001 From: Harel Moshe Date: Mon, 8 Aug 2022 13:47:26 +0300 Subject: [PATCH 2/7] Refactor --- src/matchers/toResolve.js | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/matchers/toResolve.js b/src/matchers/toResolve.js index d3daff54..e08d8988 100644 --- a/src/matchers/toResolve.js +++ b/src/matchers/toResolve.js @@ -1,36 +1,31 @@ export async function toResolve(actual) { const { matcherHint } = this.utils; + const expectedToResolveButGotRejectedMessage = (value, { isNot } = { isNot: false }) => + matcherHint('toResolve', 'received', '', { isNot, promise: true }) + + '\n\n' + + 'Expected promise to resolve, however it rejected with: "' + + value + + '".\n'; + + const expectedToRejectButGotResolvedMessage = (value, { isNot } = { isNot: false }) => + matcherHint('toResolve', 'received', '', { isNot, promise: true }) + + '\n\n' + + 'Expected promise to reject, however it resolved with: "' + + value + + '".\n'; + const matcherResult = await actual.then( value => ({ pass: true, message: () => this.isNot - ? matcherHint('toResolve', 'received', '', { isNot: true, promise: true }) + - '\n\n' + - 'Expected promise to reject, however it resolved with: "' + - value + - '".\n' - : matcherHint('toResolve', 'received', '', { isNot: false, promise: true }) + - '\n\n' + - 'Expected promise to resolve, however it rejected with: "' + - value + - '".\n', + ? expectedToRejectButGotResolvedMessage(value, { isNot: this.isNot }) + : expectedToResolveButGotRejectedMessage(value, { isNot: this.isNot }), }), value => ({ pass: false, - message: () => - this.isNot - ? matcherHint('toResolve', 'received', '', { isNot: true, promise: true }) + - '\n\n' + - 'Expected promise to resolve, however it rejected with: "' + - value + - '".\n' - : matcherHint('toResolve', 'received', '', { isNot: false, promise: true }) + - '\n\n' + - 'Expected promise to resolve, however it rejected with: "' + - value + - '".\n', + message: () => expectedToResolveButGotRejectedMessage(value, { isNot: this.isNot }), }), ); From 4686c10c9afba1ad1c22e185ac4c897e9ce933d0 Mon Sep 17 00:00:00 2001 From: Harel Moshe Date: Mon, 8 Aug 2022 13:54:46 +0300 Subject: [PATCH 3/7] Test both cases with values --- src/matchers/toResolve.js | 6 +++--- test/matchers/__snapshots__/toResolve.test.js.snap | 6 +++--- test/matchers/toResolve.test.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/matchers/toResolve.js b/src/matchers/toResolve.js index e08d8988..1172d64f 100644 --- a/src/matchers/toResolve.js +++ b/src/matchers/toResolve.js @@ -1,18 +1,18 @@ export async function toResolve(actual) { - const { matcherHint } = this.utils; + const { matcherHint, printReceived } = this.utils; const expectedToResolveButGotRejectedMessage = (value, { isNot } = { isNot: false }) => matcherHint('toResolve', 'received', '', { isNot, promise: true }) + '\n\n' + 'Expected promise to resolve, however it rejected with: "' + - value + + 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: "' + - value + + printReceived(value) + '".\n'; const matcherResult = await actual.then( diff --git a/test/matchers/__snapshots__/toResolve.test.js.snap b/test/matchers/__snapshots__/toResolve.test.js.snap index 84b108ef..6b9d4595 100644 --- a/test/matchers/__snapshots__/toResolve.test.js.snap +++ b/test/matchers/__snapshots__/toResolve.test.js.snap @@ -3,13 +3,13 @@ exports[`.not.toResolve fails when passed a promise that resolved 1`] = ` "expect(received).true.not.toResolve() -Expected promise to reject, however it resolved with: \\"undefined\\". +Expected promise to reject, however it resolved with: \\"{\\"result\\": 123}\\". " `; -exports[`.toResolve fails when passed a promise that rejects with a primitive 1`] = ` +exports[`.toResolve fails when passed a promise that rejects 1`] = ` "expect(received).true.toResolve() -Expected promise to resolve, however it rejected with: \\"something went wrong\\". +Expected promise to resolve, however it rejected with: \\"[Error: something went wrong]\\". " `; diff --git a/test/matchers/toResolve.test.js b/test/matchers/toResolve.test.js index 6536a32f..26d31148 100644 --- a/test/matchers/toResolve.test.js +++ b/test/matchers/toResolve.test.js @@ -8,15 +8,15 @@ describe('.toResolve', () => { await expect(promise).toResolve(); }); - test('fails when passed a promise that rejects with a primitive', async () => { - const promise = Promise.reject('something went wrong'); + test('fails when passed a promise that rejects', async () => { + 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(); }); From 2010faf82f05d9fa7fbc83cd956cca44f03fc1d1 Mon Sep 17 00:00:00 2001 From: Harel Moshe Date: Thu, 11 Aug 2022 18:47:54 +0300 Subject: [PATCH 4/7] Refactor --- src/matchers/toResolve.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/matchers/toResolve.js b/src/matchers/toResolve.js index 1172d64f..bdc66749 100644 --- a/src/matchers/toResolve.js +++ b/src/matchers/toResolve.js @@ -15,7 +15,7 @@ export async function toResolve(actual) { printReceived(value) + '".\n'; - const matcherResult = await actual.then( + return actual.then( value => ({ pass: true, message: () => @@ -28,6 +28,4 @@ export async function toResolve(actual) { message: () => expectedToResolveButGotRejectedMessage(value, { isNot: this.isNot }), }), ); - - return matcherResult; } From 14d718d5ca3df4154f8c080811203015c38aa0bb Mon Sep 17 00:00:00 2001 From: Harel Moshe Date: Thu, 11 Aug 2022 18:49:04 +0300 Subject: [PATCH 5/7] toReject --- src/matchers/toReject.js | 35 +++++++++++++------ .../__snapshots__/toReject.test.js.snap | 8 ++--- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/matchers/toReject.js b/src/matchers/toReject.js index 142508db..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 passMessage = - matcherHint('.not.toReject', 'received', '') + '\n\n' + 'Expected promise to resolve, however it rejected.\n'; + 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 failMessage = - matcherHint('.toReject', 'received', '') + '\n\n' + 'Expected promise to reject, however it resolved.\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'; - const pass = await actual.then( - () => false, - () => true, + 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 }), + }), ); - - return { pass, message: () => (pass ? passMessage : failMessage) }; } diff --git a/test/matchers/__snapshots__/toReject.test.js.snap b/test/matchers/__snapshots__/toReject.test.js.snap index ac609696..103838fd 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: \\"undefined\\". " `; 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: \\"undefined\\". " `; From 17de82ec18f2b959a581689fbd2cb3bdb7df0480 Mon Sep 17 00:00:00 2001 From: Harel Moshe Date: Thu, 11 Aug 2022 18:52:35 +0300 Subject: [PATCH 6/7] Add toReject test values --- test/matchers/__snapshots__/toReject.test.js.snap | 4 ++-- test/matchers/toReject.test.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/matchers/__snapshots__/toReject.test.js.snap b/test/matchers/__snapshots__/toReject.test.js.snap index 103838fd..b4b76665 100644 --- a/test/matchers/__snapshots__/toReject.test.js.snap +++ b/test/matchers/__snapshots__/toReject.test.js.snap @@ -3,13 +3,13 @@ exports[`.not.toReject fails when passed a promise that rejects 1`] = ` "expect(received).true.not.toReject() -Expected promise to resolve, however it rejected with: \\"undefined\\". +Expected promise to resolve, however it rejected with: \\"{\\"abc\\": \\"something went wrong\\"}\\". " `; exports[`.toReject fails when passed a promise that resolved 1`] = ` "expect(received).true.toReject() -Expected promise to reject, however it resolved with: \\"undefined\\". +Expected promise to reject, however it resolved with: \\"123\\". " `; 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(); }); From 0ed0bf9e3763b1bf460cb5697d292ef9749850fd Mon Sep 17 00:00:00 2001 From: Keegan Witt Date: Tue, 14 Mar 2023 19:21:33 -0400 Subject: [PATCH 7/7] Update test snapshot --- test/matchers/__snapshots__/toReject.test.js.snap | 4 ++-- test/matchers/__snapshots__/toResolve.test.js.snap | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/matchers/__snapshots__/toReject.test.js.snap b/test/matchers/__snapshots__/toReject.test.js.snap index b4b76665..3ac99fe4 100644 --- a/test/matchers/__snapshots__/toReject.test.js.snap +++ b/test/matchers/__snapshots__/toReject.test.js.snap @@ -3,13 +3,13 @@ exports[`.not.toReject fails when passed a promise that rejects 1`] = ` "expect(received).true.not.toReject() -Expected promise to resolve, however it rejected with: \\"{\\"abc\\": \\"something went wrong\\"}\\". +Expected promise to resolve, however it rejected with: "{"abc": "something went wrong"}". " `; exports[`.toReject fails when passed a promise that resolved 1`] = ` "expect(received).true.toReject() -Expected promise to reject, however it resolved with: \\"123\\". +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 6b9d4595..e7bcacd7 100644 --- a/test/matchers/__snapshots__/toResolve.test.js.snap +++ b/test/matchers/__snapshots__/toResolve.test.js.snap @@ -3,13 +3,13 @@ exports[`.not.toResolve fails when passed a promise that resolved 1`] = ` "expect(received).true.not.toResolve() -Expected promise to reject, however it resolved with: \\"{\\"result\\": 123}\\". +Expected promise to reject, however it resolved with: "{"result": 123}". " `; exports[`.toResolve fails when passed a promise that rejects 1`] = ` "expect(received).true.toResolve() -Expected promise to resolve, however it rejected with: \\"[Error: something went wrong]\\". +Expected promise to resolve, however it rejected with: "[Error: something went wrong]". " `;