-
-
Notifications
You must be signed in to change notification settings - Fork 225
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
Type definitions not working #526
Comments
We need more information about your setup. Are you using ts-jest? |
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? |
I ended up adding the matchers I need as manual types: // @types/@jest/globals/expect.d.ts
declare module 'expect' {
export interface Matchers<R> {
/**
* jest-extended typings do not work, so used matchers are manually included:
* https://github.com/jest-community/jest-extended/issues/447
* https://github.com/jest-community/jest-extended/issues/408
*/
toBeNil(): R
toBeObject(): R
...
}
} And adding the
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'd love to help, but as I said, I need some more information about your setup. Are you using ts-jest? If you have a sample little project that reproduces this that'd be very helpful too. |
@keeganwitt sorry I missed your comment offering to help some time ago. It's in a fairly complex repo - I've had a todo to break the testing out into its own package but haven't finished it yet. It is using |
Thanks for your reply. No we are not using |
Hi, I got the issue when I start importing
It works fine if I use Here is a mini repo to reproduce the issue. https://github.com/Yupeng-li/jest-extended-type-issure-mini-repo |
I was having the same issue that was fixed by using jest globals instead of importing from However, I was able to work around it with the following declaration file: // jest-extended.d.ts
import * as matchers from "jest-extended";
declare module "expect" {
type JestExtendedMatchers = typeof matchers;`
export interface Matchers<R> extends JestExtendedMatchers {}
} It appears |
Thank you @Blond11516 for the workaround. If anyone is using asymmetric matches, such as
Also, there is a type issue when we use jest-extended matchers directly. The second param of toHaveBeenCalledBefore/toHaveBeenCalledAfter should be optional but it is not. #651 |
This is the new way matchers need to be added to be picked up by declare module '@jest/expect' {
interface Matchers<R> {
toHaveSortedColumn(columnTitle: string): R;
}
} So a workaround is: import * as matchers from 'jest-extended';
type JestExtendedMatchers = typeof matchers;
declare module '@jest/expect' {
interface Matchers<R> extends JestExtendedMatchers {}
} Thanks @Blond11516 for the base code! |
After analyzing this more deeply I can confirm that the problem was that ESLint has a conflict with the global typings of Jest and Cypress. As soon as ESLint comes across any file that imports something from Cypress it also loads the types of Cypress and from that moment on ESLint is confused and starts reporting errors. So the obvious solution is to exclude any Cypress file from ESLint when it parses Jest test files that rely on the types of Jest and Jest-Extended. Then you can create a separate ESLint config (with overrides) for your Cypress tests. Once you do this you don't even have to import the Jest types any longer from Jest/Globals. This way creating a separate module declaration for the matchers as described by @Blond11516 and @probablyup is not necessary. You can then simply use |
@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. |
Ditto, no Cypress here, just trying to switch from using the global Jest types to |
Sorry for the confusion guys. Currently what works for me (TS wise) is:
What doesn't work is what you seem to describe:
I keep getting this error message:
I did it like this:
import * as matchers from "jest-extended";
type JestExtendedMatchers = typeof matchers;
declare module "@jest/expect" {
interface Matchers<R> extends JestExtendedMatchers {}
} Am I missing anyhting? |
@krisztianb My best guess would be an issue with your TS configuration. In my case, my If you add another type in your file, is it available from your test files? Here's an example: // jest-extended.d.ts
import * as matchers from "jest-extended";
type JestExtendedMatchers = typeof matchers;
declare module "@jest/expect" {
interface Matchers<R> extends JestExtendedMatchers {}
}
declare module "jest" {
interface SomeType {}
} Then in a test file: // some/test/file.test.ts
import type { SomeType } from "jest";
const t: SomeType; If this doesn't compile, it means |
@Blond11516 thank you for the help. I can import
One thing that is special in my case is that I must import from Update I found the problem. The issue was that our test folders were excluded in import * as matchers from "jest-extended";
declare module "expect" {
type JestExtendedMatchers = typeof matchers;
export interface AsymmetricMatchers extends JestExtendedMatchers {}
export interface Matchers<R> extends JestExtendedMatchers {}
} |
package
version: jest-extended 3.1.0node
version: v17.5.0npm
(oryarn
) version: yarn 3.2.2typescript
: 4.8.4I followed the instructions to use
jest-extended
with TypeScript shown on the project home page: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 apath
directive to directly import the types fromnode_modules//jest-extended/types/index.d.ts
.I get the following error message:
I am certain my types directory is being included. I have this
typeRoots
directive in my common TypeScript config:I also have a custom matcher with a type definition in my global @types folder, and it works fine:
I noticed that the signature for my (working) custom matcher is different than the type provided by
jest-extended
:Any help in figuring out why I can't use the Jest extended matchers is greatly appreciated!
The text was updated successfully, but these errors were encountered: