UNPKG

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