Created
June 20, 2018 00:39
-
-
Save flakey-bit/227acc91078498a67ad88875a8d32ad1 to your computer and use it in GitHub Desktop.
Jasmine toBeDeepEqual w/ TypeScript bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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 :) | |
}); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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