I am trying to write TypeScript for the first time in my life, and I want to use a testing library that can do snapshot testing, with combinations of parameters.
I have already tried jest + jest-extended-snapshot.
Code:
function myFunction(aNumber, aString) {
if (aNumber > 0) {
return `${aString} #${aNumber}`;
}
if (aString === "foo") {
return `${aNumber} bar`;
}
return "This is twisted…";
}
it("should continue working as before", () => {
expect(myFunction).toVerifyAllCombinations([1, -1], ["random", "foo"]);
});
Actual output:
> [email protected] test
> jest
FAIL test/gilded-rose.spec.ts
● Test suite failed to run
test/gilded-rose.spec.ts:20:33 - error TS2339: Property 'toVerifyAllCombinations' does not exist on type 'JestMatchersShape<Matchers<void, () => string>, Matchers<Promise<void>, () => string>>'.
20 expect(doUpdateQuality).toVerifyAllCombinations(
~~~~~~~~~~~~~~~~~~~~~~~
Expected output:
// Jest Snapshot v1, (redacted URL)
exports[`should continue working as before 1`] = `
Object {
"-1,foo": "-1 bar",
"-1,random": "This is twisted…",
"1,foo": "foo #1",
"1,random": "random #1",
}
`;
If jest&jest-extended-snapshot are not suitable, what should I use instead?
I found a workaround, which I know is the wrong way to fix it, but it does work for me.
I added a file jest.d.ts
:
declare global {
namespace jest {
interface Matchers<R> {
toVerifyAllCombinations(expected: string[], [], []): R
}
}
}
export {};
This is not maintainable. What is the proper way to tell jest that it needs to add toVerifyAllCombinations
to its matchers?