My current challenge involves adding multiple new custom matchers to jasmine and utilizing them effectively. I aim to create a file that registers 2 matchers, making all matchers readily available for each test file with just one registration.
In this case, I have a file called custom-matchers.ts
/// <reference path="custom-matchers.d.ts"/>
(() => {
beforeEach(() => {
jasmine.addMatchers({
toHaveText: () => {
return {
compare: (actual, expected) => {
return {
pass: actual.getText().then(pass => pass === expected)
};
},
};
},
toBePresent: () => {
return {
compare: (actual, expected) => {
return {
pass: actual.isPresent().then(pass => pass === expected),
};
},
};
}
});
});
})();
I also have a type definition file named custom-matchers.d.ts
(I attempted using a different name than the references file but encountered the same issue):
declare namespace jasmine {
interface Matchers<T> {
toHaveText(text: any): boolean;
toBePresent(text: any): boolean;
}
}
This particular file is registered in another file named e2e-helper, which is then imported into every test file as e2e-helper.ts
:
/// <reference path="custom-matchers.d.ts"/>
import './custom-matchers';
However, when attempting to use it in a test file, for example:
expect(object as any).toBePresent();
An error arises:
Property 'toBePresent' does not exist on type 'Matchers'