-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.spec.ts
255 lines (221 loc) · 8.46 KB
/
index.spec.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { AssertionError, expect, use as useChaiPlugin } from "chai";
import * as del from "del";
import * as fs from "fs";
import { Align, chaiImage, OutputOptions } from "./index";
useChaiPlugin(chaiImage);
describe("chai-image", () => {
describe("#matchImage assertion", () => {
context("when type of actual value is invalid", () => {
it("should throw AssertionError", () => {
expect(() => {
expect("abc").to.matchImage(Buffer.alloc(8));
}).to.throw(AssertionError).with.property("message", "actual image must be a Buffer, but 'abc' given");
});
});
context("when type of expected value is invalid", () => {
it("should throw AssertionError", () => {
expect(() => {
// Currently TypeScript typing disallows non-Buffer input anyway
expect(Buffer.alloc(8)).to.matchImage("abc" as any);
}).to.throw(AssertionError).with.property("message", "expected image must be a Buffer, but 'abc' given");
});
});
context("when value is not valid PNG image", () => {
it("should throw AssertionError", () => {
expect(() => {
expect(Buffer.alloc(8)).to.matchImage(Buffer.alloc(8));
}).to.throw(AssertionError).with.property("message", "image must be a valid PNG image");
});
});
context("when actual image does not match to expected image", () => {
it("should throw AssertionError", () => {
expect(() => {
// Currently TypeScript typing disallows non-Buffer input anyway
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"),
);
}).to.throw(AssertionError)
.with.property("message", "expected image to match given image, but 202175 pixels different");
});
it("should not throw AssertionError if negated", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.not.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"),
);
}).to.not.throw(AssertionError);
});
});
context("when actual image equals to expected image", () => {
it("should not throw AssertionError", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
);
}).to.not.throw(AssertionError);
});
it("should throw AssertionError if negated", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.not.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
);
}).to.throw(AssertionError)
.with.property("message", "expected image not to match given image, but none was different");
});
});
context("when actual image matches to expected image with threshold", () => {
it("should not throw AssertionError", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"), {
diff: {
threshold: 0.95,
},
},
);
}).to.not.throw(AssertionError);
});
it("should throw AssertionError if negated", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.not.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"), {
diff: {
threshold: 0.95,
},
},
);
}).to.throw(AssertionError)
.with.property("message", "expected image not to match given image, but none was different");
});
});
context("when actual image matches to expected image with align", () => {
it("should not throw AssertionError", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"), {
diff: {
threshold: 0.95,
},
align: Align.CENTER,
},
);
}).to.not.throw(AssertionError);
});
it("should throw AssertionError if negated", () => {
expect(() => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.not.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"), {
diff: {
threshold: 0.95,
},
align: Align.CENTER,
},
);
}).to.throw(AssertionError)
.with.property("message", "expected image not to match given image, but none was different");
});
});
describe("for output creation", () => {
const dir = "test_output";
// Helpers
const pass = (output?: OutputOptions) => {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
{ output },
);
};
const fail = (output?: OutputOptions) => {
try {
expect(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_l.png"),
).to.matchImage(
fs.readFileSync("fixtures/red_velvet_perfect_velvet_all_2_co_m_l.png"),
{ output },
);
} catch (e) { /* no op */ }
};
afterEach(() => {
del.sync(dir);
});
context("when output options is not set", () => {
it("should not save any outputs", () => {
pass();
fail();
expect(() => {
fs.statSync(dir);
}).to.throw(Error).with.property("code", "ENOENT");
});
});
context("when output options is set", () => {
context("with on = failure", () => {
const options = { name: "test", on: "failure" as const, dir };
it("should not save outputs if test passed", () => {
pass(options);
expect(() => {
fs.statSync(dir);
}).to.throw(Error).with.property("code", "ENOENT");
});
it("should save outputs if test failed", () => {
fail(options);
expect(() => {
fs.statSync(`${dir}/test_actual.png`);
fs.statSync(`${dir}/test_expected.png`);
fs.statSync(`${dir}/test_diff.png`);
}).to.not.throw(Error);
});
});
context("with on = always", () => {
const options = { name: "test", on: "always" as const, dir };
it("should save outputs if test passed", () => {
pass(options);
expect(() => {
fs.statSync(`${dir}/test_actual.png`);
fs.statSync(`${dir}/test_expected.png`);
fs.statSync(`${dir}/test_diff.png`);
}).to.not.throw(Error);
});
it("should save outputs if test failed", () => {
fail(options);
expect(() => {
fs.statSync(`${dir}/test_actual.png`);
fs.statSync(`${dir}/test_expected.png`);
fs.statSync(`${dir}/test_diff.png`);
}).to.not.throw(Error);
});
});
context("with diffOnly = true", () => {
const options = { name: "some-name", diffOnly: true, dir };
it("should save diff image only", () => {
fail(options);
expect(() => {
fs.statSync(`${dir}/some-name_actual.png`);
}).to.throw(Error).with.property("code", "ENOENT");
expect(() => {
fs.statSync(`${dir}/some-name_expected.png`);
}).to.throw(Error).with.property("code", "ENOENT");
expect(() => {
fs.statSync(`${dir}/some-name_diff.png`);
}).to.not.throw(Error);
});
});
});
});
});
});