Last active
June 13, 2020 07:04
-
-
Save thulka/6784263 to your computer and use it in GitHub Desktop.
jasmine matcher in typescript - example. This one does the same as toEqual does on arrays.
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
module jasmine { | |
interface Matchers { | |
toHaveSameSequence(expected: any[]): boolean; | |
} | |
} | |
beforeEach(function () { | |
this.addMatchers( | |
{ | |
toHaveSameSequence: function(expected: any[]) { | |
var a = this.actual; | |
for (var i = 0; i < a.length; i++) { | |
if (a[i] != expected[i]) return false; | |
} | |
return true; | |
} | |
}); | |
}); |
Load the file containing the custom matcher as spec file and the beforeEach() gets invoked automatically in the context of the test environment. Then you should have your custom-matcher available:
describe ("MyTest", () => {
const expected = [1,2,3];
it("should have thee same sequence", () => {
const actual = [1,2,3];
expect(actual).toHaveSameSequence(expected);
});
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you import this in you test file?