Skip to content

Commit 7a305de

Browse files
authored
chore: fix snapshot testing (#1090)
1 parent e93a5af commit 7a305de

5 files changed

Lines changed: 42 additions & 44 deletions

File tree

testenv/modules/inlineSnapshot.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ export function toMatchInlineSnapshot(
77
actual,
88
expected,
99
) {
10-
const normalizedActual = stripAddedLinebreaks(stripAddedIndentation(actual.snapshot ?? actual))
10+
const normalizedActual = stripAddedLinebreaks(stripAddedIndentation(String(actual?.snapshot ?? actual)))
1111
const normalizedExpected = stripAddedLinebreaks(stripAddedIndentation(expected))
1212

1313
return {
1414
pass: normalizedActual === normalizedExpected,
1515
message: () => [
16-
this.utils.matcherHint(
17-
`${this.isNot ? '.not' : ''}.toMatchInlineSnapshot`,
18-
'element',
19-
'',
20-
),
16+
this.utils.matcherHint('toMatchInlineSnapshot', undefined, undefined, {
17+
isNot: this.isNot,
18+
promise: this.promise,
19+
}),
2120
'',
22-
`Expected: ${this.isNot ? 'not ' : ''}${this.promise}`,
23-
` ${this.utils.printExpected(normalizedExpected)}`,
24-
'Received:',
25-
` ${this.utils.printReceived(normalizedActual)}`,
21+
this.utils.diff(normalizedExpected, normalizedActual, {expand: this.expand}),
2622
].join('\n'),
2723
}
2824
}
@@ -31,30 +27,44 @@ export function toThrowErrorMatchingInlineSnapshot(
3127
callback,
3228
expected,
3329
) {
34-
let didThrow = false, actual = undefined
35-
try {
36-
callback()
37-
} catch (e) {
38-
didThrow = true
39-
actual = e
30+
if (typeof callback === 'function') {
31+
try {
32+
const promise = callback()
33+
if (typeof promise === 'object' && 'then' in promise) {
34+
return promise
35+
.then(() => ({ didThrow: false }), r => ({ didThrow: true, actual: r }))
36+
.then(({ didThrow, actual }) => matchError.call(this, didThrow, actual, expected))
37+
}
38+
} catch (e) {
39+
return matchError.call(this, true, e, expected)
40+
}
41+
return matchError.call(this, false, undefined, expected)
42+
} else if (this.promise === 'rejects') {
43+
return matchError.call(this, true, callback, expected)
4044
}
4145

46+
throw new Error('Invalid argument')
47+
}
48+
49+
function matchError(
50+
didThrow,
51+
actual,
52+
expected,
53+
) {
4254
const normalizedActual = didThrow && stripAddedLinebreaks(stripAddedIndentation(typeof actual === 'object' && 'message' in actual ? actual.message : String(actual)))
4355
const normalizedExpected = stripAddedLinebreaks(stripAddedIndentation(expected))
4456

4557
return {
4658
pass: this.isNot === !didThrow && normalizedActual === normalizedExpected,
4759
message: () => [
48-
this.utils.matcherHint(
49-
`${this.isNot ? '.not' : ''}.toThrowErrorMatchingInlineSnapshot`,
50-
'callback',
51-
'',
52-
),
60+
this.utils.matcherHint('toThrowErrorMatchingInlineSnapshot', undefined, undefined, {
61+
isNot: this.isNot,
62+
promise: this.promise,
63+
}),
5364
'',
54-
`Expected: ${this.isNot ? 'not ' : ''}${this.promise}`,
55-
` ${this.utils.printExpected(normalizedExpected)}`,
56-
'Received:',
57-
` ${didThrow ? this.utils.printReceived(normalizedActual) : '[Did not throw]'}`,
65+
didThrow
66+
? this.utils.diff(normalizedExpected, normalizedActual, { expand: this.expand })
67+
: '[Did not throw]',
5868
].join('\n'),
5969
}
6070
}

testenv/node.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,12 @@ import './modules/expect.js'
22
import './modules/mocks.js'
33
import './modules/timers.js'
44
import './modules/testinglibrary.js'
5+
import './modules/inlineSnapshot.js'
56
import './modules/console.js'
67

78
import 'css.escape'
8-
import jestSnapshot from 'jest-snapshot'
99
import {JSDOM} from 'jsdom'
1010

11-
expect.setState({
12-
snapshotState: new jestSnapshot.SnapshotState('tests/__snapshot__/', {
13-
updateSnapshot: 'none',
14-
})
15-
})
16-
expect.extend({
17-
toMatchInlineSnapshot: jestSnapshot.toMatchInlineSnapshot,
18-
toMatchSnapshot: jestSnapshot.toMatchSnapshot,
19-
toThrowErrorMatchingInlineSnapshot: jestSnapshot.toThrowErrorMatchingInlineSnapshot,
20-
toThrowErrorMatchingSnapshot: jestSnapshot.toThrowErrorMatchingSnapshot,
21-
})
22-
2311
const jsdom = new JSDOM()
2412
globalThis.navigator = jsdom.window.navigator
2513
globalThis.window = jsdom.window

tests/clipboard/copy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ describe('without Clipboard API', () => {
8484

8585
await expect(
8686
userEvent.copy({writeToClipboard: true}),
87-
).rejects.toMatchInlineSnapshot(
88-
`[Error: The Clipboard API is unavailable.]`,
87+
).rejects.toThrowErrorMatchingInlineSnapshot(
88+
`The Clipboard API is unavailable.`,
8989
)
9090
})
9191

tests/clipboard/cut.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ describe('without Clipboard API', () => {
9292

9393
await expect(
9494
userEvent.cut({writeToClipboard: true}),
95-
).rejects.toMatchInlineSnapshot(
96-
`[Error: The Clipboard API is unavailable.]`,
95+
).rejects.toThrowErrorMatchingInlineSnapshot(
96+
`The Clipboard API is unavailable.`,
9797
)
9898
})
9999

tests/clipboard/paste.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ describe('without Clipboard API', () => {
143143
test('reject if trying to use missing API', async () => {
144144
const {getEvents} = render(`<input/>`)
145145

146-
await expect(userEvent.paste()).rejects.toMatchInlineSnapshot(
147-
`[Error: \`userEvent.paste()\` without \`clipboardData\` requires the \`ClipboardAPI\` to be available.]`,
146+
await expect(userEvent.paste()).rejects.toThrowErrorMatchingInlineSnapshot(
147+
`\`userEvent.paste()\` without \`clipboardData\` requires the \`ClipboardAPI\` to be available.`,
148148
)
149149
expect(getEvents()).toHaveLength(0)
150150
})

0 commit comments

Comments
 (0)