/
snapshotsTestsTemplate.ts
64 lines (57 loc) · 1.49 KB
/
snapshotsTestsTemplate.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
/* eslint-disable jest/no-export */
/* eslint-disable jest/expect-expect */
import global from 'global';
import { addSerializer } from 'jest-specific-snapshot';
const { describe, it } = global;
function snapshotTest({ item, asyncJest, framework, testMethod, testMethodParams }: any) {
const { name } = item;
const context = { ...item, framework };
if (asyncJest === true) {
it(
name,
() =>
new Promise<void>((resolve, reject) =>
testMethod({
done: (error: any) => (error ? reject(error) : resolve()),
story: item,
context,
...testMethodParams,
})
),
testMethod.timeout
);
} else {
it(
name,
() =>
testMethod({
story: item,
context,
...testMethodParams,
}),
testMethod.timeout
);
}
}
function snapshotTestSuite({ item, suite, ...restParams }: any) {
const { kind, children } = item;
describe(suite, () => {
describe(kind, () => {
children.forEach((c: any) => {
snapshotTest({ item: c, ...restParams });
});
});
});
}
function snapshotsTests({ data, snapshotSerializers, ...restParams }: any) {
if (snapshotSerializers) {
snapshotSerializers.forEach((serializer: any) => {
addSerializer(serializer);
expect.addSnapshotSerializer(serializer);
});
}
data.forEach((item: any) => {
snapshotTestSuite({ item, ...restParams });
});
}
export default snapshotsTests;