-
Notifications
You must be signed in to change notification settings - Fork 66
/
test-utils.ts
59 lines (52 loc) · 1.93 KB
/
test-utils.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
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as path from "path";
import { AwsArchitecture } from "@aws/aws-arch";
import * as fs from "fs-extra";
import { toMatchImageSnapshot } from "jest-image-snapshot";
import { kebabCase } from "lodash";
import { IS_DEBUG } from "../../src/internal/debug";
expect.extend({ toMatchImageSnapshot });
export async function makeCdkOutDir(...name: string[]): Promise<string> {
const dir = path.join(__dirname, "..", ".tmp", ...name, "cdk.out");
await fs.ensureDir(dir);
await fs.emptyDir(dir);
return dir;
}
export function cleanseDotSnapshot(dot: string): string {
return dot.replace(
AwsArchitecture.assetDirectory,
"<aws_arch_asset_directory"
);
}
export async function expectToMatchImageSnapshot(
imagePath: string,
name?: string,
pixelThreshold?: number,
failureThreshold?: number
): Promise<void> {
const imageBuffer = fs.readFileSync(imagePath);
// https://github.com/americanexpress/jest-image-snapshot#%EF%B8%8F-api
expect(imageBuffer).toMatchImageSnapshot({
customSnapshotIdentifier({ currentTestName, counter, testPath }) {
const dir = testPath.replace(__dirname, "").split(".")[0];
if (name) {
return path.join(dir, name);
}
return path.join(
dir,
kebabCase(currentTestName).replace(RegExp(`^${dir}`), "") +
(counter <= 1 ? "" : `-${counter}`)
);
},
allowSizeMismatch: true,
customDiffConfig: {
// Prevent rendering variants between environments (CI, MacOS, Ubuntu, etc)
threshold: pixelThreshold || 0.05, // default is 0.01
},
// TODO: Figure out why image rendering is not deterministic in terms of order and then change back to 0.05.
failureThreshold: failureThreshold || 1,
failureThresholdType: "percent",
updatePassedSnapshot: process.env.CI !== "true" && IS_DEBUG,
});
}