From 7a305dee9ab833d6f338d567fc2e862b4838b76a Mon Sep 17 00:00:00 2001 From: Philipp Fritsche Date: Tue, 27 Dec 2022 19:24:06 +0100 Subject: [PATCH] chore: fix snapshot testing (#1090) --- testenv/modules/inlineSnapshot.js | 60 ++++++++++++++++++------------- testenv/node.js | 14 +------- tests/clipboard/copy.ts | 4 +-- tests/clipboard/cut.ts | 4 +-- tests/clipboard/paste.ts | 4 +-- 5 files changed, 42 insertions(+), 44 deletions(-) diff --git a/testenv/modules/inlineSnapshot.js b/testenv/modules/inlineSnapshot.js index 23bb09d3..a884dd39 100644 --- a/testenv/modules/inlineSnapshot.js +++ b/testenv/modules/inlineSnapshot.js @@ -7,22 +7,18 @@ export function toMatchInlineSnapshot( actual, expected, ) { - const normalizedActual = stripAddedLinebreaks(stripAddedIndentation(actual.snapshot ?? actual)) + const normalizedActual = stripAddedLinebreaks(stripAddedIndentation(String(actual?.snapshot ?? actual))) const normalizedExpected = stripAddedLinebreaks(stripAddedIndentation(expected)) return { pass: normalizedActual === normalizedExpected, message: () => [ - this.utils.matcherHint( - `${this.isNot ? '.not' : ''}.toMatchInlineSnapshot`, - 'element', - '', - ), + this.utils.matcherHint('toMatchInlineSnapshot', undefined, undefined, { + isNot: this.isNot, + promise: this.promise, + }), '', - `Expected: ${this.isNot ? 'not ' : ''}${this.promise}`, - ` ${this.utils.printExpected(normalizedExpected)}`, - 'Received:', - ` ${this.utils.printReceived(normalizedActual)}`, + this.utils.diff(normalizedExpected, normalizedActual, {expand: this.expand}), ].join('\n'), } } @@ -31,30 +27,44 @@ export function toThrowErrorMatchingInlineSnapshot( callback, expected, ) { - let didThrow = false, actual = undefined - try { - callback() - } catch (e) { - didThrow = true - actual = e + if (typeof callback === 'function') { + try { + const promise = callback() + if (typeof promise === 'object' && 'then' in promise) { + return promise + .then(() => ({ didThrow: false }), r => ({ didThrow: true, actual: r })) + .then(({ didThrow, actual }) => matchError.call(this, didThrow, actual, expected)) + } + } catch (e) { + return matchError.call(this, true, e, expected) + } + return matchError.call(this, false, undefined, expected) + } else if (this.promise === 'rejects') { + return matchError.call(this, true, callback, expected) } + throw new Error('Invalid argument') +} + +function matchError( + didThrow, + actual, + expected, +) { const normalizedActual = didThrow && stripAddedLinebreaks(stripAddedIndentation(typeof actual === 'object' && 'message' in actual ? actual.message : String(actual))) const normalizedExpected = stripAddedLinebreaks(stripAddedIndentation(expected)) return { pass: this.isNot === !didThrow && normalizedActual === normalizedExpected, message: () => [ - this.utils.matcherHint( - `${this.isNot ? '.not' : ''}.toThrowErrorMatchingInlineSnapshot`, - 'callback', - '', - ), + this.utils.matcherHint('toThrowErrorMatchingInlineSnapshot', undefined, undefined, { + isNot: this.isNot, + promise: this.promise, + }), '', - `Expected: ${this.isNot ? 'not ' : ''}${this.promise}`, - ` ${this.utils.printExpected(normalizedExpected)}`, - 'Received:', - ` ${didThrow ? this.utils.printReceived(normalizedActual) : '[Did not throw]'}`, + didThrow + ? this.utils.diff(normalizedExpected, normalizedActual, { expand: this.expand }) + : '[Did not throw]', ].join('\n'), } } diff --git a/testenv/node.js b/testenv/node.js index 2b0eb766..c4655b21 100644 --- a/testenv/node.js +++ b/testenv/node.js @@ -2,24 +2,12 @@ import './modules/expect.js' import './modules/mocks.js' import './modules/timers.js' import './modules/testinglibrary.js' +import './modules/inlineSnapshot.js' import './modules/console.js' import 'css.escape' -import jestSnapshot from 'jest-snapshot' import {JSDOM} from 'jsdom' -expect.setState({ - snapshotState: new jestSnapshot.SnapshotState('tests/__snapshot__/', { - updateSnapshot: 'none', - }) -}) -expect.extend({ - toMatchInlineSnapshot: jestSnapshot.toMatchInlineSnapshot, - toMatchSnapshot: jestSnapshot.toMatchSnapshot, - toThrowErrorMatchingInlineSnapshot: jestSnapshot.toThrowErrorMatchingInlineSnapshot, - toThrowErrorMatchingSnapshot: jestSnapshot.toThrowErrorMatchingSnapshot, -}) - const jsdom = new JSDOM() globalThis.navigator = jsdom.window.navigator globalThis.window = jsdom.window diff --git a/tests/clipboard/copy.ts b/tests/clipboard/copy.ts index 5306fd0a..1b0e3b11 100644 --- a/tests/clipboard/copy.ts +++ b/tests/clipboard/copy.ts @@ -84,8 +84,8 @@ describe('without Clipboard API', () => { await expect( userEvent.copy({writeToClipboard: true}), - ).rejects.toMatchInlineSnapshot( - `[Error: The Clipboard API is unavailable.]`, + ).rejects.toThrowErrorMatchingInlineSnapshot( + `The Clipboard API is unavailable.`, ) }) diff --git a/tests/clipboard/cut.ts b/tests/clipboard/cut.ts index e99aa266..b8c2fc45 100644 --- a/tests/clipboard/cut.ts +++ b/tests/clipboard/cut.ts @@ -92,8 +92,8 @@ describe('without Clipboard API', () => { await expect( userEvent.cut({writeToClipboard: true}), - ).rejects.toMatchInlineSnapshot( - `[Error: The Clipboard API is unavailable.]`, + ).rejects.toThrowErrorMatchingInlineSnapshot( + `The Clipboard API is unavailable.`, ) }) diff --git a/tests/clipboard/paste.ts b/tests/clipboard/paste.ts index c792ae1a..cd5eb967 100644 --- a/tests/clipboard/paste.ts +++ b/tests/clipboard/paste.ts @@ -143,8 +143,8 @@ describe('without Clipboard API', () => { test('reject if trying to use missing API', async () => { const {getEvents} = render(``) - await expect(userEvent.paste()).rejects.toMatchInlineSnapshot( - `[Error: \`userEvent.paste()\` without \`clipboardData\` requires the \`ClipboardAPI\` to be available.]`, + await expect(userEvent.paste()).rejects.toThrowErrorMatchingInlineSnapshot( + `\`userEvent.paste()\` without \`clipboardData\` requires the \`ClipboardAPI\` to be available.`, ) expect(getEvents()).toHaveLength(0) })