|
| 1 | +/* eslint-disable @typescript-eslint/no-namespace */ |
| 2 | + |
| 3 | +// @ts-expect-error: jest-image-snapshot doesn't have built-in types |
| 4 | +import { toMatchImageSnapshot } from "jest-image-snapshot" |
| 5 | + |
| 6 | +expect.extend({ toMatchImageSnapshot }) |
| 7 | + |
| 8 | +declare global { |
| 9 | + namespace jest { |
| 10 | + interface Matchers<R> { |
| 11 | + toMatchImageSnapshot(options?: MatchImageSnapshotOptions): R |
| 12 | + } |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +interface MatchImageSnapshotOptions { |
| 17 | + /** |
| 18 | + * If set to true, the build will not fail when the screenshots to compare have different sizes. |
| 19 | + * @default false |
| 20 | + */ |
| 21 | + allowSizeMismatch?: boolean | undefined |
| 22 | + /** |
| 23 | + * Custom config passed to 'pixelmatch' or 'ssim' |
| 24 | + */ |
| 25 | + customDiffConfig?: { |
| 26 | + /** |
| 27 | + * Per pixel sensitivity threshold, ranges from 0 to 1. |
| 28 | + */ |
| 29 | + threshold: number |
| 30 | + } |
| 31 | + /** |
| 32 | + * The method by which images are compared. |
| 33 | + * `pixelmatch` does a pixel by pixel comparison, whereas `ssim` does a structural similarity comparison. |
| 34 | + * @default 'pixelmatch' |
| 35 | + */ |
| 36 | + comparisonMethod?: "pixelmatch" | "ssim" | undefined |
| 37 | + /** |
| 38 | + * Custom snapshots directory. |
| 39 | + * Absolute path of a directory to keep the snapshot in. |
| 40 | + */ |
| 41 | + customSnapshotsDir?: string | undefined |
| 42 | + /** |
| 43 | + * A custom absolute path of a directory to keep this diff in |
| 44 | + */ |
| 45 | + customDiffDir?: string | undefined |
| 46 | + /** |
| 47 | + * Store the received images separately from the composed diff images on failure. |
| 48 | + * This can be useful when updating baseline images from CI. |
| 49 | + * @default false |
| 50 | + */ |
| 51 | + storeReceivedOnFailure?: boolean | undefined |
| 52 | + /** |
| 53 | + * A custom absolute path of a directory to keep this received image in. |
| 54 | + */ |
| 55 | + customReceivedDir?: string | undefined |
| 56 | + /** |
| 57 | + * A custom name to give this snapshot. If not provided, one is computed automatically. When a function is provided |
| 58 | + * it is called with an object containing testPath, currentTestName, counter and defaultIdentifier as its first |
| 59 | + * argument. The function must return an identifier to use for the snapshot. |
| 60 | + */ |
| 61 | + customSnapshotIdentifier?: |
| 62 | + | ((parameters: { testPath: string; currentTestName: string; counter: number; defaultIdentifier: string }) => string) |
| 63 | + | string |
| 64 | + | undefined |
| 65 | + /** |
| 66 | + * Changes diff image layout direction. |
| 67 | + * @default 'horizontal' |
| 68 | + */ |
| 69 | + diffDirection?: "horizontal" | "vertical" | undefined |
| 70 | + /** |
| 71 | + * Will output base64 string of a diff image to console in case of failed tests (in addition to creating a diff image). |
| 72 | + * This string can be copy-pasted to a browser address string to preview the diff for a failed test. |
| 73 | + * @default false |
| 74 | + */ |
| 75 | + dumpDiffToConsole?: boolean | undefined |
| 76 | + /** |
| 77 | + * Will output the image to the terminal using iTerm's Inline Images Protocol. |
| 78 | + * If the term is not compatible, it does the same thing as `dumpDiffToConsole`. |
| 79 | + * @default false |
| 80 | + */ |
| 81 | + dumpInlineDiffToConsole?: boolean | undefined |
| 82 | + /** |
| 83 | + * Removes coloring from the console output, useful if storing the results to a file. |
| 84 | + * @default false. |
| 85 | + */ |
| 86 | + noColors?: boolean | undefined |
| 87 | + /** |
| 88 | + * Sets the threshold that would trigger a test failure based on the failureThresholdType selected. This is different |
| 89 | + * to the customDiffConfig.threshold above - the customDiffConfig.threshold is the per pixel failure threshold, whereas |
| 90 | + * this is the failure threshold for the entire comparison. |
| 91 | + * @default 0. |
| 92 | + */ |
| 93 | + failureThreshold?: number | undefined |
| 94 | + /** |
| 95 | + * Sets the type of threshold that would trigger a failure. |
| 96 | + * @default 'pixel'. |
| 97 | + */ |
| 98 | + failureThresholdType?: "pixel" | "percent" | undefined |
| 99 | + /** |
| 100 | + * Updates a snapshot even if it passed the threshold against the existing one. |
| 101 | + * @default false. |
| 102 | + */ |
| 103 | + updatePassedSnapshot?: boolean | undefined |
| 104 | + /** |
| 105 | + * Applies Gaussian Blur on compared images, accepts radius in pixels as value. Useful when you have noise after |
| 106 | + * scaling images per different resolutions on your target website, usually setting its value to 1-2 should be |
| 107 | + * enough to solve that problem. |
| 108 | + * @default 0. |
| 109 | + */ |
| 110 | + blur?: number | undefined |
| 111 | + /** |
| 112 | + * Runs the diff in process without spawning a child process. |
| 113 | + * @default false. |
| 114 | + */ |
| 115 | + runInProcess?: boolean | undefined |
| 116 | +} |
0 commit comments