jest: added addSnapshotSerializer (#16334)

based on flow typings at https://github.com/facebook/jest/blob/e56103cf142d2e87542ddfb6bd892bcee262c0e6/types/PrettyFormat.js
This commit is contained in:
eug48
2017-06-01 08:11:18 -07:00
committed by Andy
parent 350e27c474
commit da66e73945
2 changed files with 64 additions and 0 deletions
+40
View File
@@ -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<SnapshotSerializerPlugin>
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 */
+24
View File
@@ -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<Function>();
var a = new mockFn();