Skip to content

Instantly share code, notes, and snippets.

@flakey-bit
Created June 20, 2018 00:39
Show Gist options
  • Save flakey-bit/227acc91078498a67ad88875a8d32ad1 to your computer and use it in GitHub Desktop.
Save flakey-bit/227acc91078498a67ad88875a8d32ad1 to your computer and use it in GitHub Desktop.
Jasmine toBeDeepEqual w/ TypeScript bindings
import { diff } from "deep-object-diff"; // Available on NPM, includes its own TypeScript bindings
const deepEqualMatcher: jasmine.CustomMatcher = {
compare: function(actual: any, expected: any) {
let diffObj = diff(expected, actual);
let result: jasmine.CustomMatcherResult;
if (Object.keys(diffObj).length > 0) {
result = {
pass: false,
message: "Expected the objects to be deep-equal but they were not:\n" + JSON.stringify(diffObj, null, 2)
}
} else {
result = {
pass: true,
message: "Expected the objects not to be deep-equal but they were"
};
}
return result;
}
};
export const deepEqualMatcherFactory: jasmine.CustomMatcherFactory = () => deepEqualMatcher;
/// <reference path="matcher-types.d.ts" />
import { deepEqualMatcherFactory } from "./deepEqualMatcher";
describe("toBeDeepEqual", () => {
beforeEach(() => {
jasmine.addMatchers({
toBeDeepEqual: deepEqualMatcherFactory});
});
it("example test", () => {
expect({foo: [1,2,3]}).toBeDeepEqual({foo: [1,3,2]}); // Fails with nice output :)
});
})
declare namespace jasmine {
interface Matchers<T> {
toBeDeepEqual(observed: any): boolean;
}
interface ArrayLikeMatchers<T> {
toBeDeepEqual(observed: any): boolean;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment