jest-extended: Type definitions not working
packageversion: jest-extended 3.1.0nodeversion: v17.5.0npm(oryarn) version: yarn 3.2.2typescript: 4.8.4
I followed the instructions to use jest-extended with TypeScript shown on the project home page:
// global.d.ts
import 'jest-extended'
I also tried the triple slash directive using types as suggested in the documentation if the above doesn’t work, and tried using a triple slash directive with a path directive to directly import the types from node_modules//jest-extended/types/index.d.ts.
I get the following error message:
test/jest/__tests__/jsdom-env/state.spec.ts:33:18 - error TS2339: Property 'toBeTrue' does not exist on type 'Matchers<void> & SnapshotMatchers<void, boolean> & Inverse<JestMatchers<void, boolean>> & PromiseMatchers<boolean>'.
33 expect(true).toBeTrue()
I am certain my types directory is being included. I have this typeRoots directive in my common TypeScript config:
"typeRoots": ["./@types/**/*.d.ts", "./node_modules/@types/**/*.d.ts"],
I also have a custom matcher with a type definition in my global @types folder, and it works fine:
// @types/jest/globals/extend.d.ts
declare module 'expect' {
export interface Matchers<R> {
toHaveInProtoChain(...chain: Constructor[]): R
}
}
I noticed that the signature for my (working) custom matcher is different than the type provided by jest-extended:
declare namespace jest {
interface Matchers<R> {
...
Any help in figuring out why I can’t use the Jest extended matchers is greatly appreciated!
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 2
- Comments: 16 (3 by maintainers)
Hi, I got the issue when I start importing
expectfrom@jest/globals.It works fine if I use
@types/jestand don’t import directly from@jest/globalsHere is a mini repo to reproduce the issue. https://github.com/Yupeng-li/jest-extended-type-issure-mini-repo
@krisztianb I’m not sure we’re talking about the exact same issue. I don’t have Cypress in my project, so at least in my case I can’t be having conflicts between it and Jest.
Also, I’m not having issues with ESLint throwing errors, I simply can’t get Typescript itself to compile without the workaround I shared.
I was having the same issue that was fixed by using jest globals instead of importing from
@jest/globalslike @Yupeng-li said.However, I was able to work around it with the following declaration file:
It appears
jest-extendedextends theMatchersinterface in thejestnamespace, but the matchers aren’t actually defined in that namespace when imported from@jest/globals.@Blond11516 thank you for the help.
I can import
SomeTypethe way you showed in my test files without error. However I still get this error message when accessing anything fromjest-expect:One thing that is special in my case is that I must import from
@jest/globalsbecause in our project we are using cypress (that uses Chai) which also has anexpectobject with different methods.Update
I found the problem. The issue was that our test folders were excluded in
tsconfigso that they are not compiled. After I removed thatexcludethe following content ofjest-extended.d.tsworks now for me too:I ended up adding the matchers I need as manual types:
And adding the
@typesdirectory to my tsconfigtypeRootsconfig. I found this library that says that to ensure the global jest declaration is augmented correctly, the Jest setup file with theexpect.extend('...')statement for the matchers should be included via your TypeScript configuration:"include": ["config/jest/setupJest.ts", "..."],I’m still hoping to find a better solution than adding manual types for the matchers. I haven’t tried the approach mentioned above to see if it works though.
I have the same problem. I don’t see how the type declarations (which obviously need to work through TS’s declaration merging) of this package could possibly work. But I guess I’m missing something because otherwise more people would complain about this issue. @webstackdev have you already found a solution?