From da66e73945b5494ba8a589d9e5bc22ef6856dc87 Mon Sep 17 00:00:00 2001 From: eug48 Date: Fri, 2 Jun 2017 01:11:18 +1000 Subject: [PATCH] jest: added addSnapshotSerializer (#16334) based on flow typings at https://github.com/facebook/jest/blob/e56103cf142d2e87542ddfb6bd892bcee262c0e6/types/PrettyFormat.js --- types/jest/index.d.ts | 40 ++++++++++++++++++++++++++++++++++++++++ types/jest/jest-tests.ts | 24 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/types/jest/index.d.ts b/types/jest/index.d.ts index 36011e67a7..9297ebfced 100644 --- a/types/jest/index.d.ts +++ b/types/jest/index.d.ts @@ -152,6 +152,44 @@ declare namespace jest { [key: string]: (this: MatcherUtils, received: any, actual: any) => { message: () => string, pass: boolean }; } + interface SnapshotSerializerOptions { + callToJSON?: boolean; + edgeSpacing?: string; + spacing?: string; + escapeRegex?: boolean; + highlight?: boolean; + indent?: number; + maxDepth?: number; + min?: boolean; + plugins?: Array + printFunctionName?: boolean; + theme?: SnapshotSerializerOptionsTheme; + + // see https://github.com/facebook/jest/blob/e56103cf142d2e87542ddfb6bd892bcee262c0e6/types/PrettyFormat.js + } + interface SnapshotSerializerOptionsTheme { + comment?: string; + content?: string; + prop?: string; + tag?: string; + value?: string; + } + interface SnapshotSerializerColor { + close: string; + open: string; + } + interface SnapshotSerializerColors { + comment: SnapshotSerializerColor; + content: SnapshotSerializerColor; + prop: SnapshotSerializerColor; + tag: SnapshotSerializerColor; + value: SnapshotSerializerColor; + } + interface SnapshotSerializerPlugin { + print(val:any, serialize:((val:any) => string), indent:((str:string) => string), opts:SnapshotSerializerOptions, colors: SnapshotSerializerColors) : string; + test(val:any) : boolean; + } + /** The `expect` function is used every time you want to test a value. You will rarely call `expect` by itself. */ interface Expect { /** @@ -169,6 +207,8 @@ declare namespace jest { assertions(num: number): void; /** You can use `expect.extend` to add your own matchers to Jest. */ extend(obj: ExpectExtendMap): void; + /** Adds a module to format application-specific data structures for serialization. */ + addSnapshotSerializer(serializer: SnapshotSerializerPlugin) : void; /** Matches any object that recursively matches the provided keys. This is often handy in conjunction with other asymmetric matchers. */ objectContaining(obj: {}): any; /** Matches any string that contains the exact provided string */ diff --git a/types/jest/jest-tests.ts b/types/jest/jest-tests.ts index d951153842..d8f6d54dfd 100644 --- a/types/jest/jest-tests.ts +++ b/types/jest/jest-tests.ts @@ -335,6 +335,30 @@ describe('toThrowErrorMatchingSnapshot', function () { }); }); +const testSerializerPluginString = "set by testSerializerPlugin"; +let testSerializerPluginCallCount = 0; +expect.addSnapshotSerializer({ + print(val, serialize, indent, opts, colors) { + val.willOverwrite = testSerializerPluginString; + testSerializerPluginCallCount += 1; + return 'plugin called: ' + serialize(val.willOverwrite); + }, + test(val) { + return val && val.willOverwrite && val.willOverwrite !== testSerializerPluginString; + }, +}); +describe('addSnapshotSerializer', function () { + it('the plugin does its work', function () { + testSerializerPluginCallCount = 0; + expect({ willOverwrite: { x: 1, y: 2, } }).toMatchSnapshot(); + expect({ willOverwrite: "this will get overwritten by testSerializerPlugin" }).toMatchSnapshot(); + expect({ willOverwrite: "so will this" }).toMatchSnapshot(); + expect({ foo: "this will not" }).toMatchSnapshot(); + expect(testSerializerPluginCallCount).toBe(3); + }); +}); + + function testInstances() { var mockFn = jest.fn(); var a = new mockFn();