UNPKG

5.93 kBTypeScriptView Raw
1// Type definitions for jest-image-snapshot 6.1
2// Project: https://github.com/americanexpress/jest-image-snapshot#readme
3// Definitions by: Janeene Beeforth <https://github.com/dawnmist>
4// erbridge <https://github.com/erbridge>
5// Piotr Błażejewicz <https://github.com/peterblazejewicz>
6// Ayc0 <https://github.com/Ayc0>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8// Minimum TypeScript Version: 4.3
9
10/// <reference types="jest" />
11
12import { PixelmatchOptions } from 'pixelmatch';
13import { Options as SSIMOptions } from 'ssim.js';
14
15export interface MatchImageSnapshotOptions {
16 /**
17 * If set to true, the build will not fail when the screenshots to compare have different sizes.
18 * @default false
19 */
20 allowSizeMismatch?: boolean | undefined;
21 /**
22 * Custom config passed to 'pixelmatch' or 'ssim'
23 */
24 customDiffConfig?: PixelmatchOptions | Partial<SSIMOptions> | undefined;
25 /**
26 * The method by which images are compared.
27 * `pixelmatch` does a pixel by pixel comparison, whereas `ssim` does a structural similarity comparison.
28 * @default 'pixelmatch'
29 */
30 comparisonMethod?: 'pixelmatch' | 'ssim' | undefined;
31 /**
32 * Custom snapshots directory.
33 * Absolute path of a directory to keep the snapshot in.
34 */
35 customSnapshotsDir?: string | undefined;
36 /**
37 * A custom absolute path of a directory to keep this diff in
38 */
39 customDiffDir?: string | undefined;
40 /**
41 * Store the received images separately from the composed diff images on failure.
42 * This can be useful when updating baseline images from CI.
43 * @default false
44 */
45 storeReceivedOnFailure?: boolean | undefined;
46 /**
47 * A custom absolute path of a directory to keep this received image in.
48 */
49 customReceivedDir?: string | undefined;
50 /**
51 * A custom name to give this snapshot. If not provided, one is computed automatically. When a function is provided
52 * it is called with an object containing testPath, currentTestName, counter and defaultIdentifier as its first
53 * argument. The function must return an identifier to use for the snapshot.
54 */
55 customSnapshotIdentifier?:
56 | ((parameters: {
57 testPath: string;
58 currentTestName: string;
59 counter: number;
60 defaultIdentifier: string;
61 }) => string)
62 | string
63 | undefined;
64 /**
65 * Changes diff image layout direction.
66 * @default 'horizontal'
67 */
68 diffDirection?: 'horizontal' | 'vertical' | undefined;
69 /**
70 * Either only include the difference between the baseline and the received image in the diff image, or include
71 * the 3 images (following the direction set by `diffDirection`).
72 * @default false
73 */
74 onlyDiff?: boolean | undefined;
75 /**
76 * Will output base64 string of a diff image to console in case of failed tests (in addition to creating a diff image).
77 * This string can be copy-pasted to a browser address string to preview the diff for a failed test.
78 * @default false
79 */
80 dumpDiffToConsole?: boolean | undefined;
81 /**
82 * Will output the image to the terminal using iTerm's Inline Images Protocol.
83 * If the term is not compatible, it does the same thing as `dumpDiffToConsole`.
84 * @default false
85 */
86 dumpInlineDiffToConsole?: boolean | undefined;
87 /**
88 * Removes coloring from the console output, useful if storing the results to a file.
89 * @default false.
90 */
91 noColors?: boolean | undefined;
92 /**
93 * Sets the threshold that would trigger a test failure based on the failureThresholdType selected. This is different
94 * to the customDiffConfig.threshold above - the customDiffConfig.threshold is the per pixel failure threshold, whereas
95 * this is the failure threshold for the entire comparison.
96 * @default 0.
97 */
98 failureThreshold?: number | undefined;
99 /**
100 * Sets the type of threshold that would trigger a failure.
101 * @default 'pixel'.
102 */
103 failureThresholdType?: 'pixel' | 'percent' | undefined;
104 /**
105 * Updates a snapshot even if it passed the threshold against the existing one.
106 * @default false.
107 */
108 updatePassedSnapshot?: boolean | undefined;
109 /**
110 * Applies Gaussian Blur on compared images, accepts radius in pixels as value. Useful when you have noise after
111 * scaling images per different resolutions on your target website, usually setting its value to 1-2 should be
112 * enough to solve that problem.
113 * @default 0.
114 */
115 blur?: number | undefined;
116 /**
117 * Runs the diff in process without spawning a child process.
118 * @default false.
119 */
120 runInProcess?: boolean | undefined;
121}
122
123/**
124 * Function to be passed to jest's expect.extend.
125 * Example:
126 * import { toMatchImageSnapshot } from 'jest-image-snapshot';
127 * expect.extend({ toMatchImageSnapshot });
128 */
129export function toMatchImageSnapshot(options?: MatchImageSnapshotOptions): { message(): string; pass: boolean };
130
131/**
132 * Configurable function that can be passed to jest's expect.extend.
133 * Example:
134 * import { configureToMatchImageSnapshot } from 'jest-image-snapshot';
135 * const toMatchImageSnapshot = configureToMatchImageSnapshot({ noColors: true });
136 * expect.extend({ toMatchImageSnapshot });
137 */
138export function configureToMatchImageSnapshot(
139 options: MatchImageSnapshotOptions,
140): () => { message(): string; pass: boolean };
141
142/**
143 * Mutates original state with new state
144 */
145export function updateSnapshotState<TObject, TPartial>(
146 originalSnapshotState: TObject,
147 partialSnapshotState: TPartial,
148): TObject & TPartial;
149
150declare global {
151 namespace jest {
152 interface Matchers<R, T> {
153 toMatchImageSnapshot(options?: MatchImageSnapshotOptions): R;
154 }
155 }
156}