diff --git a/types/jest-image-snapshot/index.d.ts b/types/jest-image-snapshot/index.d.ts new file mode 100644 index 0000000000..c15e435cc9 --- /dev/null +++ b/types/jest-image-snapshot/index.d.ts @@ -0,0 +1,75 @@ +// Type definitions for jest-image-snapshot 2.4 +// Project: https://github.com/americanexpress/jest-image-snapshot#readme +// Definitions by: Janeene Beeforth +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +/// + +/** + * Options to be passed to the 'pixelmatch' image diffing function. + */ +export interface PixelmatchOptions { + /** Matching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.1 by default. */ + readonly threshold?: number; + /** If true, disables detecting and ignoring anti-aliased pixels. false by default. */ + readonly includeAA?: boolean; +} + +export interface MatchImageSnapshotOptions { + /** + * Custom config passed to 'pixelmatch' + */ + customDiffConfig?: PixelmatchOptions; + /** + * Custom snapshots directory. + * Absolute path of a directory to keep the snapshot in. + */ + customSnapshotsDir?: string; + /** + * A custom name to give this snapshot. If not provided, one is computed automatically. + */ + customSnapshotIdentifier?: string; + /** + * Removes coloring from the console output, useful if storing the results to a file. + * Defaults to false. + */ + noColors?: boolean; + /** + * Sets the threshold that would trigger a test failure based on the failureThresholdType selected. This is different + * to the customDiffConfig.threshold above - the customDiffConfig.threshold is the per pixel failure threshold, whereas + * this is the failure threshold for the entire comparison. + * Defaults to 0. + */ + failureThreshold?: number; + /** + * Sets the type of threshold that would trigger a failure. + * Defaults to 'pixel'. + */ + failureThresholdType?: 'pixel' | 'percent'; +} + +/** + * Function to be passed to jest's expect.extend. + * Example: + * import { toMatchImageSnapshot } from 'jest-image-snapshot'; + * expect.extend({ toMatchImageSnapshot }); + */ +export function toMatchImageSnapshot(): { message(): string; pass: boolean; }; + +/** + * Configurable function that can be passed to jest's expect.extend. + * Example: + * import { configureToMatchImageSnapshot } from 'jest-image-snapshot'; + * const toMatchImageSnapshot = configureToMatchImageSnapshot({ noColors: true }); + * expect.extend({ toMatchImageSnapshot }); + */ +export function configureToMatchImageSnapshot(options: MatchImageSnapshotOptions): () => { message(): string; pass: boolean; }; + +declare global { + namespace jest { + interface Matchers { + toMatchImageSnapshot(): R; + } + } +} diff --git a/types/jest-image-snapshot/jest-image-snapshot-tests.ts b/types/jest-image-snapshot/jest-image-snapshot-tests.ts new file mode 100644 index 0000000000..a2ccb8c777 --- /dev/null +++ b/types/jest-image-snapshot/jest-image-snapshot-tests.ts @@ -0,0 +1,23 @@ +// Typescript Version: 2.3 +import { toMatchImageSnapshot, configureToMatchImageSnapshot } from 'jest-image-snapshot'; + +it('should be able to use toMatchImageSnapshot in a test', () => { + expect.extend({ toMatchImageSnapshot }); + + expect(400).toMatchImageSnapshot(); +}); + +it('should be able to use configureToMatchImageSnapshot in a test', () => { + const matchFn = configureToMatchImageSnapshot({ + noColors: true, + customDiffConfig: { + threshold: 5, + includeAA: false + }, + failureThreshold: 10, + failureThresholdType: 'percent' + }); + expect.extend({ toMatchImageSnapshot: matchFn }); + + expect('Me').toMatchImageSnapshot(); +}); diff --git a/types/jest-image-snapshot/tsconfig.json b/types/jest-image-snapshot/tsconfig.json new file mode 100644 index 0000000000..5bf932371d --- /dev/null +++ b/types/jest-image-snapshot/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jest-image-snapshot-tests.ts" + ] +} diff --git a/types/jest-image-snapshot/tslint.json b/types/jest-image-snapshot/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jest-image-snapshot/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }