Skip to content

Commit

Permalink
chore: fix snapshot testing (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Dec 27, 2022
1 parent e93a5af commit 7a305de
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 44 deletions.
60 changes: 35 additions & 25 deletions testenv/modules/inlineSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
}
}
Expand All @@ -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'),
}
}
Expand Down
14 changes: 1 addition & 13 deletions testenv/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/clipboard/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
)
})

Expand Down
4 changes: 2 additions & 2 deletions tests/clipboard/cut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
)
})

Expand Down
4 changes: 2 additions & 2 deletions tests/clipboard/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ describe('without Clipboard API', () => {
test('reject if trying to use missing API', async () => {
const {getEvents} = render(`<input/>`)

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)
})
Expand Down

0 comments on commit 7a305de

Please sign in to comment.