forked from duailibe/jest-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
65 lines (53 loc) · 1.93 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
64
65
"use strict";
const hasAnsi = require("has-ansi");
const stripAnsi = require("strip-ansi");
expect.addSnapshotSerializer({
test: value => typeof value === "string" && hasAnsi(value),
print: stripAnsi
});
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("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("throws for non-strings", () => {
expect(() => {
expect({}).toEqual(expect.jsonMatching(expect.anything()));
}).toThrow(/You must provide a string/);
});
test("throws for invalid JSON", () => {
expect(() => {
expect("not json").toEqual(expect.jsonMatching(expect.anything()));
}).toThrow(/Actual is not valid JSON/);
});
});