Skip to content

Commit d1daa82

Browse files
committed
Release 1.4.0
1 parent 3b9a0e7 commit d1daa82

File tree

3 files changed

+90
-24
lines changed

3 files changed

+90
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mattapperson/slapshot",
3-
"version": "1.3.2",
3+
"version": "1.4.0",
44
"main": "lib/index.js",
55
"description": "Mock method calls with snapshots, run your intigation tests online or offline!",
66
"license": "MIT",

src/memorize.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,50 @@ test("thrown errors are replayed with custom properties", async () => {
273273

274274
expect(mockedCB).not.toBeCalled();
275275
});
276+
277+
test("consoles error of non-matching snap when validateSnapshot is not set", async () => {
278+
process.argv.push("--updateSnapshot");
279+
process.env.SLAPSHOT_ONLINE = "true";
280+
const spy = jest.spyOn(console, "warn");
281+
282+
memorize("c", () => 22);
283+
284+
process.argv = process.argv.filter(e => e !== "--updateSnapshot");
285+
process.env.SLAPSHOT_ONLINE = "true";
286+
287+
memorize("c", () => {});
288+
289+
expect(spy).toBeCalled();
290+
spy.mockRestore();
291+
});
292+
293+
test("throws error of non-matching snap when validateSnapshot is set to true", async () => {
294+
process.argv.push("--updateSnapshot");
295+
process.env.SLAPSHOT_ONLINE = "true";
296+
297+
memorize("validateSnapshot", () => 22);
298+
299+
process.argv = process.argv.filter(e => e !== "--updateSnapshot");
300+
process.env.SLAPSHOT_ONLINE = "true";
301+
expect(() => {
302+
memorize("validateSnapshot", () => {}, {
303+
validateSnapshot: true
304+
});
305+
}).toThrow();
306+
});
307+
308+
test("deffers to validateSnapshot to validate snapshot when validateSnapshot is a funtion", async () => {
309+
process.argv.push("--updateSnapshot");
310+
process.env.SLAPSHOT_ONLINE = "true";
311+
312+
memorize("c", () => 22);
313+
314+
process.argv = process.argv.filter(e => e !== "--updateSnapshot");
315+
process.env.SLAPSHOT_ONLINE = "false";
316+
const data = memorize("c", () => {}, {
317+
validateSnapshot: liveData => {
318+
expect(liveData).toMatchSnapshot();
319+
}
320+
});
321+
expect(data).toBe(22);
322+
});

src/memorize.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ function returnValues(value: SlapshotDataFormat) {
2020
}
2121
}
2222

23+
type ValidationCallback = (liveData: any, snapshottedData: any) => void;
24+
type ValidationOptions = boolean | ValidationCallback;
25+
2326
export function memorize<ReturnedData = any>(
2427
snapshotName: string,
2528
method: () => Promise<ReturnedData> | ReturnedData,
26-
{ pure = true }: { pure?: boolean } = {}
29+
{
30+
pure = true,
31+
validateSnapshot = false
32+
}: { pure?: boolean; validateSnapshot?: ValidationOptions } = {}
2733
): Promise<ReturnedData> | ReturnedData {
2834
let jestContext: any;
2935
// Hack to get access to jest current test context
@@ -97,7 +103,8 @@ export function memorize<ReturnedData = any>(
97103
results: null
98104
},
99105
jestContext,
100-
snapshots
106+
snapshots,
107+
validateSnapshot
101108
);
102109
})
103110
.then(methodResults => {
@@ -109,7 +116,8 @@ export function memorize<ReturnedData = any>(
109116
results: methodResults
110117
},
111118
jestContext,
112-
snapshots
119+
snapshots,
120+
validateSnapshot
113121
);
114122
});
115123
}
@@ -120,7 +128,8 @@ export function memorize<ReturnedData = any>(
120128
fullSnapshotName,
121129
methodResults,
122130
jestContext,
123-
snapshots
131+
snapshots,
132+
validateSnapshot
124133
);
125134
}
126135

@@ -130,36 +139,46 @@ function resolveData(
130139
fullSnapshotName: string,
131140
methodResults: SlapshotDataFormat,
132141
jestContext: any,
133-
snapshots: Snapshot
142+
snapshots: Snapshot,
143+
validateSnapshot: ValidationOptions
134144
) {
135145
if (!shouldUpdateSnapshot() && runInOnlineMode()) {
136146
let snapDataToCompare = snap;
137-
if (typeof snapDataToCompare === "object") {
138-
snapDataToCompare = JSON.stringify(snapDataToCompare);
139-
}
140-
141-
let methodResultsToCompare: any = methodResults;
142-
if (typeof methodResultsToCompare === "object") {
143-
methodResultsToCompare = JSON.stringify(methodResultsToCompare);
144-
}
145147

146-
if (
147-
(snap.results || snap.thrownError) &&
148-
methodResultsToCompare !== snapDataToCompare
149-
) {
150-
throw new Error(
151-
`[Warning] Intigration test result does not match the memorized snap file:
148+
if (validateSnapshot && typeof validateSnapshot === "function") {
149+
validateSnapshot(methodResults, snapDataToCompare);
150+
} else {
151+
if (typeof snapDataToCompare === "object") {
152+
snapDataToCompare = JSON.stringify(snapDataToCompare);
153+
}
154+
155+
let methodResultsToCompare: any = methodResults;
156+
if (typeof methodResultsToCompare === "object") {
157+
methodResultsToCompare = JSON.stringify(methodResultsToCompare);
158+
}
159+
160+
if (
161+
(snap.results || snap.thrownError) &&
162+
methodResultsToCompare !== snapDataToCompare
163+
) {
164+
const defaultWarning = `[Warning] Integration test result does not match the memorized snap file:
152165
- Method snapshot name: ${fullSnapshotName}
153166
- Test file: ${jestContext.testPath}
154167
- Live result: ${methodResultsToCompare}
155168
- Existing Snap: ${snapDataToCompare}
156-
169+
157170
${process.env.SLAPSHOT_RERUN_MESSAGE ||
158171
"Please re-run Jest with the --updateSnapshot flag AND the env var SLAPSHOT_ONLINE=true"}.`.replace(
159-
new RegExp(" ", "g"),
172+
new RegExp(" ", "g"),
160173
""
161-
)
162-
);
174+
);
175+
176+
if (!validateSnapshot) {
177+
console.warn(defaultWarning);
178+
} else if (typeof validateSnapshot === "boolean") {
179+
throw new Error(defaultWarning);
180+
}
181+
}
163182
}
164183

165184
return returnValues(methodResults);

0 commit comments

Comments
 (0)