-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
337 lines (288 loc) · 9.93 KB
/
index.test.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { imageSnapshot, waitMillis } from "./index";
import { Browser, createBrowser } from "./internal/browser";
import { createSnapshotter, SnapshotterOptions } from "./internal/snapshotter";
import { ImageSnapshotOptions, TestMethodContext } from "./types";
import { createBrowserMockImplementation } from "./internal/__mocks__/browser";
import "./internal/test-utils/to-take-millis-to-resolve";
jest.mock("./internal/browser");
const createBrowserMock = createBrowser as jest.MockedFunction<typeof createBrowser>;
jest.mock("./internal/snapshotter");
const createSnapshotterMock = createSnapshotter as jest.MockedFunction<typeof createSnapshotter>;
beforeEach(() => {
createSnapshotterMock.mockReset();
createBrowserMock.mockReset();
createBrowserMock.mockImplementation(createBrowserMockImplementation);
});
const FIREFOX = {
id: "firefox",
capabilities: {
browserName: "firefox",
},
};
const CHROME = {
id: "chrome",
capabilities: {
browserName: "chrome",
},
};
const CONTEXT: TestMethodContext = {
fileName: "./index.stories.ts",
framework: "react",
name: "Story",
story: "story",
kind: "a-kind",
id: "a-story",
};
describe("index", () => {
let consoleErrorSpy: jest.SpyInstance;
beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, "error");
});
afterEach(() => {
consoleErrorSpy.mockReset();
});
it("returns a test-method with beforeAll and afterAll hooks", async () => {
const testMethod = imageSnapshot({
browsers: [
{
id: "firefox",
capabilities: {
browserName: "firefox",
},
},
],
});
expect(testMethod).toBeInstanceOf(Function);
expect(testMethod.beforeAll).toBeInstanceOf(Function);
expect(testMethod.afterAll).toBeInstanceOf(Function);
});
describe("the test-method", () => {
it("calls the create-snapshot function for each browser", async () => {
const snapshotterMock = {
createSnapshots: jest.fn(),
errors: [],
};
createSnapshotterMock.mockReturnValue(snapshotterMock);
await runTestMethodWithLifeCycle({ browsers: [FIREFOX, CHROME] }, CONTEXT);
expect(createSnapshotterMock).toHaveBeenCalledTimes(1);
expect(snapshotterMock.createSnapshots).toHaveBeenCalledTimes(2);
expect(snapshotterMock.createSnapshots.mock.calls).toContainEqual([
expect.objectContaining({ id: "chrome" }),
"http://localhost:6006",
]);
expect(snapshotterMock.createSnapshots.mock.calls).toContainEqual([
expect.objectContaining({ id: "firefox" }),
"http://localhost:6006",
]);
});
it("waits until the test method is finished, then resolves the returned promise", async () => {
const timeline = [];
createSnapshotterMock.mockImplementation((options: SnapshotterOptions) => {
return {
async createSnapshots() {
timeline.push("Starting " + options.context.id);
await waitMillis(200)();
timeline.push("Done " + options.context.id);
},
errors: [],
};
});
const testMethod = imageSnapshot({ browsers: [FIREFOX] });
await testMethod.beforeAll();
try {
for (let i = 0; i < 3; i++) {
await testMethod({ context: { ...CONTEXT, kind: "kind", id: "story " + i } });
}
} finally {
await testMethod.afterAll();
}
expect(timeline).toEqual([
"Starting story 0",
"Done story 0",
"Starting story 1",
"Done story 1",
"Starting story 2",
"Done story 2",
]);
});
it("does not call the create-snapshot function if the story-parameter 'ignore' is set to true", async () => {
const snapshotterMock = {
createSnapshots: jest.fn(),
errors: [],
};
createSnapshotterMock.mockReturnValue(snapshotterMock);
await runTestMethodWithLifeCycle(
{ browsers: [FIREFOX, CHROME] },
{
...CONTEXT,
parameters: {
storyshotSelenium: {
ignore: true,
},
},
}
);
expect(createSnapshotterMock).toHaveBeenCalledTimes(0);
});
});
describe("applies default values when creating the snapshotter", () => {
beforeEach(() => {
jest.useFakeTimers();
createSnapshotterMock.mockReturnValue({
createSnapshots: jest.fn(),
errors: [],
});
});
afterEach(() => {
jest.useRealTimers();
});
it("passes 'context'", async () => {
const actualOptions = await getActualSnapshotterOptions({}, CONTEXT);
expect(actualOptions.context).toEqual(CONTEXT);
});
it("uses default 'sizes'", async () => {
const actualOptions = await getActualSnapshotterOptions({}, CONTEXT);
expect(actualOptions.sizes).toEqual(["1024x768"]);
});
it("uses the default 'match-options'", async () => {
const actualOptions = await getActualSnapshotterOptions({}, CONTEXT);
const actualMatchOptions = actualOptions.getMatchOptions({
browserId: "firefox",
context: CONTEXT,
size: "1024x768",
url: "http://example.com",
});
expect(actualMatchOptions).toEqual({
customDiffConfig: {
includeAA: true,
threshold: 0.02,
},
customSnapshotIdentifier: "a-story-1024x768-firefox",
customSnapshotsDir: "src/__image_snapshots_selenium__/a-story",
failureThreshold: 10,
failureThresholdType: "pixel",
});
});
it("uses the default 'beforeFirstScreenshot'-function", async () => {
const actualOptions = await getActualSnapshotterOptions({}, CONTEXT);
await expect(actualOptions.beforeFirstScreenshot).toTakeMillisToResolve(1000);
});
it("uses the default 'beforeEachScreenshot'-function", async () => {
const actualOptions = await getActualSnapshotterOptions({}, CONTEXT);
await expect(actualOptions.beforeEachScreenshot).toTakeMillisToResolve(200);
});
it("uses the default 'afterEachScreenshot'-function", async () => {
const actualOptions = await getActualSnapshotterOptions({}, CONTEXT);
await expect(actualOptions.afterEachScreenshot).toTakeMillisToResolve(0);
});
});
describe("applies provided options when calling createSnapshot", () => {
beforeEach(() => {
createSnapshotterMock.mockReturnValue({
createSnapshots: jest.fn(),
errors: [],
});
});
it("uses the provided 'sizes'", async () => {
const actualOptions = await getActualSnapshotterOptions({ sizes: ["100x100"] }, CONTEXT);
expect(actualOptions.sizes).toEqual(["100x100"]);
});
it("uses the provided 'merges the getMatchOptions'", async () => {
const getMatchOptions = () => ({ customSnapshotsDir: "aaa" });
const actualOptions = await getActualSnapshotterOptions({ getMatchOptions }, CONTEXT);
const matchOptions = actualOptions.getMatchOptions({
browserId: "firefox",
context: CONTEXT,
size: "1024x768",
url: "http://example.com",
});
expect(matchOptions).toEqual({
customDiffConfig: {
includeAA: true,
threshold: 0.02,
},
customSnapshotIdentifier: "a-story-1024x768-firefox",
customSnapshotsDir: "aaa",
failureThreshold: 10,
failureThresholdType: "pixel",
});
});
it("uses the provided 'beforeFirstScreenshot'-function", async () => {
const beforeFirstScreenshot = jest.fn();
const actualOptions = await getActualSnapshotterOptions({ beforeFirstScreenshot }, CONTEXT);
expect(actualOptions.beforeFirstScreenshot).toBe(beforeFirstScreenshot);
});
it("uses the default 'beforeEachScreenshot'-function", async () => {
const beforeEachScreenshot = jest.fn();
const actualOptions = await getActualSnapshotterOptions({ beforeEachScreenshot }, CONTEXT);
expect(actualOptions.beforeEachScreenshot).toBe(beforeEachScreenshot);
});
it("uses the default 'afterEachScreenshot'-function", async () => {
const afterEachScreenshot = jest.fn();
const actualOptions = await getActualSnapshotterOptions({ afterEachScreenshot }, CONTEXT);
expect(actualOptions.afterEachScreenshot).toBe(afterEachScreenshot);
});
});
it("if the errors-property of the snapshotter is not empty after execution, it throws the first and logs the others", async () => {
createSnapshotterMock.mockReturnValue({
createSnapshots: jest.fn(),
errors: [new Error("error1"), new Error("error2")],
});
await expect(() => runTestMethodWithLifeCycle({ browsers: [CHROME] }, CONTEXT)).rejects.toThrow(
/error1/
);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringMatching(/Found 2 errors/));
});
it("creates browsers with the correct options", async () => {
createSnapshotterMock.mockReturnValue({
createSnapshots: jest.fn(),
errors: [],
});
const seleniumUrl = "http://someUrl:9009";
const testMethod = imageSnapshot({ seleniumUrl, browsers: [CHROME, FIREFOX] });
await testMethod.beforeAll();
expect(createBrowserMock).toHaveBeenCalledTimes(2);
expect(createBrowserMock).toHaveBeenCalledWith(seleniumUrl, CHROME);
expect(createBrowserMock).toHaveBeenCalledWith(seleniumUrl, FIREFOX);
});
it("closes all browsers afterwards", async () => {
createSnapshotterMock.mockReturnValue({
createSnapshots: jest.fn(),
errors: [],
});
const testMethod = imageSnapshot({ browsers: [CHROME, FIREFOX] });
await testMethod.beforeAll();
await testMethod.afterAll();
const browsers: jest.MockResult<Browser>[] = createBrowserMock.mock.results;
expect(browsers.length).toEqual(2);
expect(browsers[0].value.quit).toHaveBeenCalled();
expect(browsers[1].value.quit).toHaveBeenCalled();
});
it("afterAll ignore missing browsers", async () => {
createSnapshotterMock.mockReturnValue({
createSnapshots: jest.fn(),
errors: [],
});
const testMethod = imageSnapshot({ browsers: [CHROME, FIREFOX] });
await expect(() => testMethod.afterAll()).not.toThrow();
});
});
async function runTestMethodWithLifeCycle(
options: ImageSnapshotOptions,
context: TestMethodContext
) {
const testMethod = imageSnapshot(options);
await testMethod.beforeAll();
try {
await testMethod({ context });
} finally {
await testMethod.afterAll();
}
}
async function getActualSnapshotterOptions(
nonRequiredOptions: Partial<ImageSnapshotOptions>,
context: TestMethodContext
): Promise<SnapshotterOptions> {
await runTestMethodWithLifeCycle({ browsers: [CHROME], ...nonRequiredOptions }, context);
return createSnapshotterMock.mock.calls[0][0];
}