I have created a custom matcher with Jest named toHaveSameText
the tests are running fine from both command line and from IntelliJ, However, IntelliJ editor is showing the matcher in red and project reports compilation error. If I hover over the error in intelliJ, I get following error on hover: TS2339: Property toHaveSameText does not exist on type Assertion<string>
Here are various files I created to created my custom matcher.
setup.d.ts
type OwnMatcher<Params extends unknown[]> = (
this: jest.MatcherContext,
actual: unknown,
...params: Params
) => jest.CustomMatcherResult
//@ts-ignore
declare global {
namespace jest {
interface Matchers<R, T> {
toHaveSameText(actual: string, expected: string): T
}
interface ExpectExtendMap {
// Here, we're describing the call signature of our
// matcher for the "expect.extend()" call.
toHaveSameText: OwnMatcher<[actual: string, expected: string]>
}
}
}
setup.js
import { expect, afterEach } from 'vitest';
import { Assertion, assert } from 'chai';
import { cleanup } from '@testing-library/react';
expect.extend({ toHaveSameText(actual, expected) {
let test = new Assertion(actual, expected, assert.equal, true);
test.assert(actual.replace(/s/g, '') === expected.replace(/s/g, ''),
'expected #{this} to equal #{exp}',
'expected #{this} to not equal #{act}',
expected,
actual,
true);
return { pass: true };
}
});
afterEach(() => {
cleanup();
});
In vite.config.js
I have setupFiles: ['@testing-library/jest-dom', './test/setup.js'],
I also tried adding "include": ["./test/setup.d.ts"]
in my tsconfig.json, but it didn’t work.
Am I missing something for IntelliJ to recognize the custom matcher.