Skip to content

Commit

Permalink
Release 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mattapperson committed Aug 16, 2019
1 parent 3b9a0e7 commit d1daa82
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mattapperson/slapshot",
"version": "1.3.2",
"version": "1.4.0",
"main": "lib/index.js",
"description": "Mock method calls with snapshots, run your intigation tests online or offline!",
"license": "MIT",
Expand Down
47 changes: 47 additions & 0 deletions src/memorize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,50 @@ test("thrown errors are replayed with custom properties", async () => {

expect(mockedCB).not.toBeCalled();
});

test("consoles error of non-matching snap when validateSnapshot is not set", async () => {
process.argv.push("--updateSnapshot");
process.env.SLAPSHOT_ONLINE = "true";
const spy = jest.spyOn(console, "warn");

memorize("c", () => 22);

process.argv = process.argv.filter(e => e !== "--updateSnapshot");
process.env.SLAPSHOT_ONLINE = "true";

memorize("c", () => {});

expect(spy).toBeCalled();
spy.mockRestore();
});

test("throws error of non-matching snap when validateSnapshot is set to true", async () => {
process.argv.push("--updateSnapshot");
process.env.SLAPSHOT_ONLINE = "true";

memorize("validateSnapshot", () => 22);

process.argv = process.argv.filter(e => e !== "--updateSnapshot");
process.env.SLAPSHOT_ONLINE = "true";
expect(() => {
memorize("validateSnapshot", () => {}, {
validateSnapshot: true
});
}).toThrow();
});

test("deffers to validateSnapshot to validate snapshot when validateSnapshot is a funtion", async () => {
process.argv.push("--updateSnapshot");
process.env.SLAPSHOT_ONLINE = "true";

memorize("c", () => 22);

process.argv = process.argv.filter(e => e !== "--updateSnapshot");
process.env.SLAPSHOT_ONLINE = "false";
const data = memorize("c", () => {}, {
validateSnapshot: liveData => {
expect(liveData).toMatchSnapshot();
}
});
expect(data).toBe(22);
});
65 changes: 42 additions & 23 deletions src/memorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ function returnValues(value: SlapshotDataFormat) {
}
}

type ValidationCallback = (liveData: any, snapshottedData: any) => void;
type ValidationOptions = boolean | ValidationCallback;

export function memorize<ReturnedData = any>(
snapshotName: string,
method: () => Promise<ReturnedData> | ReturnedData,
{ pure = true }: { pure?: boolean } = {}
{
pure = true,
validateSnapshot = false
}: { pure?: boolean; validateSnapshot?: ValidationOptions } = {}
): Promise<ReturnedData> | ReturnedData {
let jestContext: any;
// Hack to get access to jest current test context
Expand Down Expand Up @@ -97,7 +103,8 @@ export function memorize<ReturnedData = any>(
results: null
},
jestContext,
snapshots
snapshots,
validateSnapshot
);
})
.then(methodResults => {
Expand All @@ -109,7 +116,8 @@ export function memorize<ReturnedData = any>(
results: methodResults
},
jestContext,
snapshots
snapshots,
validateSnapshot
);
});
}
Expand All @@ -120,7 +128,8 @@ export function memorize<ReturnedData = any>(
fullSnapshotName,
methodResults,
jestContext,
snapshots
snapshots,
validateSnapshot
);
}

Expand All @@ -130,36 +139,46 @@ function resolveData(
fullSnapshotName: string,
methodResults: SlapshotDataFormat,
jestContext: any,
snapshots: Snapshot
snapshots: Snapshot,
validateSnapshot: ValidationOptions
) {
if (!shouldUpdateSnapshot() && runInOnlineMode()) {
let snapDataToCompare = snap;
if (typeof snapDataToCompare === "object") {
snapDataToCompare = JSON.stringify(snapDataToCompare);
}

let methodResultsToCompare: any = methodResults;
if (typeof methodResultsToCompare === "object") {
methodResultsToCompare = JSON.stringify(methodResultsToCompare);
}

if (
(snap.results || snap.thrownError) &&
methodResultsToCompare !== snapDataToCompare
) {
throw new Error(
`[Warning] Intigration test result does not match the memorized snap file:
if (validateSnapshot && typeof validateSnapshot === "function") {
validateSnapshot(methodResults, snapDataToCompare);
} else {
if (typeof snapDataToCompare === "object") {
snapDataToCompare = JSON.stringify(snapDataToCompare);
}

let methodResultsToCompare: any = methodResults;
if (typeof methodResultsToCompare === "object") {
methodResultsToCompare = JSON.stringify(methodResultsToCompare);
}

if (
(snap.results || snap.thrownError) &&
methodResultsToCompare !== snapDataToCompare
) {
const defaultWarning = `[Warning] Integration test result does not match the memorized snap file:
- Method snapshot name: ${fullSnapshotName}
- Test file: ${jestContext.testPath}
- Live result: ${methodResultsToCompare}
- Existing Snap: ${snapDataToCompare}
${process.env.SLAPSHOT_RERUN_MESSAGE ||
"Please re-run Jest with the --updateSnapshot flag AND the env var SLAPSHOT_ONLINE=true"}.`.replace(
new RegExp(" ", "g"),
new RegExp(" ", "g"),
""
)
);
);

if (!validateSnapshot) {
console.warn(defaultWarning);
} else if (typeof validateSnapshot === "boolean") {
throw new Error(defaultWarning);
}
}
}

return returnValues(methodResults);
Expand Down

0 comments on commit d1daa82

Please sign in to comment.