-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
82 lines (73 loc) · 2.74 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const { toMatchSnapshot, toMatchInlineSnapshot } = require("jest-snapshot");
async function toMatchSpeechInlineSnapshotAsync(fn, expectedSnapshot) {
// Jest uses the stacktraces to find the callsite.
// For async stacktraces it'll point to the wrapped call
// i.e. every `toMatchSpeechInlineSnapshotAsync` will point to the same call triggering "Jest: Multiple inline snapshots for the same call are not supported."
this.error = new Error();
const result = await fn();
if (expectedSnapshot === undefined) {
return toMatchInlineSnapshot.call(this, result);
}
return toMatchInlineSnapshot.call(this, result, expectedSnapshot);
}
expect.extend({
async toMatchSpeechInlineSnapshotAsync(fn, expectedSnapshot) {
// Jest uses the stacktraces to find the callsite.
// For async stacktraces it'll point to the wrapped call
// i.e. every `toMatchSpeechInlineSnapshotAsync` will point to the same call triggering "Jest: Multiple inline snapshots for the same call are not supported."
this.error = new Error();
const result = await fn();
if (expectedSnapshot === undefined) {
return toMatchInlineSnapshot.call(this, result);
}
return toMatchInlineSnapshot.call(this, result, expectedSnapshot);
},
toMatchSpeechInlineSnapshot(fn, ...args) {
const result = fn();
return toMatchInlineSnapshot.call(this, result, ...args);
},
async toMatchSpeechSnapshotAsync(fn, ...args) {
const result = await fn();
return toMatchSnapshot.call(this, result, ...args);
},
toMatchSpeechSnapshot(fn, ...args) {
const result = fn();
return toMatchSnapshot.call(this, result, ...args);
},
});
describe("custom snapshot matcher", () => {
it("works if sync", () => {
expect(() => {
return "sync snapshot #1";
}).toMatchSpeechSnapshot();
expect(() => {
return "sync snapshot #2";
}).toMatchSpeechSnapshot();
});
it("works if async", async () => {
await expect(async () => {
return "async snapshot #1";
}).toMatchSpeechSnapshotAsync();
await expect(async () => {
return "async snapshot #2";
}).toMatchSpeechSnapshotAsync();
});
});
describe("custom inline snapshot matcher", () => {
it("works if sync", () => {
expect(() => {
return "sync snapshot #1";
}).toMatchSpeechInlineSnapshot(`"sync snapshot #1"`);
expect(() => {
return "sync snapshot #2";
}).toMatchSpeechInlineSnapshot(`"sync snapshot #2"`);
});
it("works if async", async () => {
await expect(async () => {
return "async inline snapshot #1";
}).toMatchSpeechInlineSnapshotAsync(`"async inline snapshot #1"`);
await expect(async () => {
return "async inline snapshot #2";
}).toMatchSpeechInlineSnapshotAsync(`"async inline snapshot #2"`);
});
});