A snapshotting library similar to what jest offers but for Jasmine 2 instead. Written in Typescript
npm install jasmine-snapshotts
import { MatchesSnapshot } from "jasmine-snapshot";
it("matces simple snapshot", () => {
let actual = "fried chicken";
// do stuff . . .
let snapshot = "fried chicken";
MatchesSnapshot(snapshot, actual);
});
`
Javascript objects
The much more useful feature is to compare javascript objects to a snapshot. It will take any JS object, remove circular references, and stringify the JS object. Then it will prettify both the snapshot and the actual JS stringified object. If they don't match, it will give you a diff of the two prettified JSON strings and tell you what to put in your snapshot if you want to update it.
The first thing you need to do is register a snapshot object to use and give it a name:
`ts
let snapshots = {
};
beforeAll(() =>
{
registerSnapshots(snapshots, "snapshot suite name");
});
`
Note you can only have one registered snapshot object for a describe, so you cannot register a snapshot file on a base describe and again on an inner describe (no nesting.) I usually register a snapshot object for each base level describe and put the snapshots in their own file that I then import.
Here is an example of a successfully matching compares assuming you have registered a snapshot object and updated it.
`ts
import { expectjs, registerSnapshots, expectxml } from "jasmine-snapshot";
it("matces JS snapshot ", () =>
{
let actual = { chicken_type: "fried" };
expectjs(actual).toMatchSnapshot();
});
`
XML/HTML objects
Also, you can do checks with XML and XML'ish type things.
Note that the snapshot is JSON formed from the HTML. That is because the JSON can be ordered alphabetically so the order of HTML tag attributes will not affect the result.
`ts
it("does a basic html snapshot", () =>
{
let actual = ;
expectxml(actual).toMatchSnapshot();
});
`
Here is an example checking HTML generated by react checked using Enzyme.
`ts
import * as React from "react";
import * as Enzyme from "enzyme";
import { FormControl } from "react-bootstrap";
import { expectjs, registerSnapshots, expectxml } from "jasmine-snapshot";
it("Render basic text area with bootstrap ", () =>
{
const elem = Enzyme.shallow(
);
expectxml(elem.html()).toMatchSnapshot();
});
``