-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
63 lines (53 loc) · 1.97 KB
/
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
"use strict";
require(".");
describe("toMatchJSON", () => {
const json = JSON.stringify({ foo: "bar", spam: "eggs" });
test("matches", () => {
expect(json).toMatchJSON({ foo: "bar", spam: "eggs" });
expect(json).toMatchJSON({ spam: "eggs", foo: "bar" });
});
test(".not doesn't match", () => {
expect(json).not.toMatchJSON({ foo: "baz", spam: "eggs" });
});
test("assertion error has a nice output", () => {
expect(() =>
expect(json).toMatchJSON({ foo: "baz" })
).toThrowErrorMatchingSnapshot();
expect(() =>
expect(json).not.toMatchJSON({ foo: "bar", spam: "eggs" })
).toThrowErrorMatchingSnapshot();
expect(() =>
expect(json).not.toMatchJSON({ foo: expect.anything(), spam: "eggs" })
).toThrowErrorMatchingSnapshot();
});
test("throws on invalid JSON", () => {
expect(() => expect(null).toMatchJSON()).toThrowErrorMatchingSnapshot();
expect(() => expect("").toMatchJSON()).toThrowErrorMatchingSnapshot();
expect(() => expect("fals").toMatchJSON()).toThrowErrorMatchingSnapshot();
expect(() => expect("falsr").toMatchJSON()).toThrowErrorMatchingSnapshot();
expect(() => expect("fals'").toMatchJSON()).toThrowErrorMatchingSnapshot();
expect(() => expect("fals9").toMatchJSON()).toThrowErrorMatchingSnapshot();
});
});
describe("jsonMatching", () => {
test("matches object", () => {
expect(JSON.stringify({ foo: "bar" })).toEqual(
expect.jsonMatching({
foo: expect.any(String),
})
);
expect(JSON.stringify({ foo: "bar", bar: "baz" })).toEqual(
expect.jsonMatching(expect.objectContaining({ foo: "bar" }))
);
});
test("matches array", () => {
expect(JSON.stringify(["foo", "bar"])).toEqual(
expect.jsonMatching(expect.arrayContaining(["bar", "foo"]))
);
});
test("works inside arrayContaining()", () => {
expect([1, JSON.stringify({ foo: "bar" })]).toEqual(
expect.arrayContaining([expect.jsonMatching({ foo: "bar" })])
);
});
});