Skip to content

Instantly share code, notes, and snippets.

@thulka
Last active June 13, 2020 07:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
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;
}
});
});
@jonatanskogsfors
Copy link

How do you import this in you test file?

@chillinski
Copy link

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