-
-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript declarations are not working when used with @jest/globals #674
Comments
To make the asymmetric matchers visible, you can do the following in your globals.d.ts or equivalent: import * as jestExtended from 'jest-extended';
type JestExtended = typeof jestExtended;
declare module 'expect' {
interface AsymmetricMatchers extends JestExtended {}
} However, this requires you to use asymmetric matching in all cases by using expect([]).toEqual(expect.toIncludeSameMembers([])); For regular matchers you can also kinda sorta do this: declare module 'expect' {
interface Matchers extends JestExtended {}
} However the problem is that the object exported by jest-extended is of type import {CustomMatchers} from 'jest-extended';
declare module 'expect' {
interface Matchers<R> extends CustomMatchers<R> {}
} Of course, ideally, the package should just do this itself for both kinds of matchers. |
Working with TS 5.4+ // global.d.ts
declare module 'expect' {
interface Matchers<R> extends CustomMatchers<R> {}
interface AsymmetricMatchers extends CustomMatchers<unknown> {}
}
export {}; |
To get typings working in my project I used the following, which also fixes the fixed /* eslint-disable @typescript-eslint/no-explicit-any */
import "@jest/globals";
import JestExtendedMatchers from "jest-extended";
type ReplaceReturnType<T extends (...args: any) => any, TNewReturn> = (
...args: Parameters<T>
) => TNewReturn;
/**
* Jest matchers for Jest Extended.
* JestExtendedMatchers is fixed on type `any`.
* To make them work they need to be of type `R` (which is `void | Promise<void>`).
*/
type JestExtendedMatchersFixed<R> = {
[K in keyof typeof JestExtendedMatchers]: ReplaceReturnType<
(typeof JestExtendedMatchers)[K],
R
>;
};
declare global {
namespace jest {
interface AsymmetricMatchers extends JestExtendedMatchers<R> {}
interface Matchers<R> extends JestExtendedMatchers<R> {}
}
}
declare module "expect" {
interface AsymmetricMatchers extends JestExtendedMatchersFixed<R> {}
interface Matchers<R> extends JestExtendedMatchersFixed<R> {}
} This adheres to the TypeScript example, as documented here: https://jestjs.io/docs/expect#expectextendmatchers |
Hello!
Thank you for this great library!
However, when using TypeScript and Jest with
@jest/globals
, the declarations provided by the package doesn't work. E.g.:Have you considered this use case? Thank you.
The text was updated successfully, but these errors were encountered: