-
-
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
Vitest typings override instead of augmenting module #610
Comments
Same issue here with TS 5.1.3. |
Just FFR - my workaround is to use code as below when I need import type { SpyInstance } from 'vitest/dist/index'; |
Sorry for the issue I introduced. I made those 2 PRs Both of them fixes the issue described by @mlev. I do not have a perfect solution in mind. Each of the solution comes with advantages and drawbacks. But in each of them I have trouble fixing the typescript errors |
On my side I was using jest-extended only for this functionality https://jest-extended.jestcommunity.dev/docs/matchers/Array#toincludesamemembersmembers But I found out that vitest has this natively with the chai api. So I will probably not be using jest-extended anymore |
Also running into this issue, for now having to drop the jest-extended version 😔 Might need some input from @keeganwitt |
I think the preferable alternative here is to go with the PR that exports the types and leaves the Vitest configuration up to the user. This also takes care of the case of using |
I won't be able to get to it this week. But will try to next week |
I had to resolve it temporarily by applying a patch via patch-package. File diff --git a/node_modules/jest-extended/types/index.d.ts b/node_modules/jest-extended/types/index.d.ts
index 16626d3..30474c0 100644
--- a/node_modules/jest-extended/types/index.d.ts
+++ b/node_modules/jest-extended/types/index.d.ts
@@ -1,6 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
-interface CustomMatchers<R> extends Record<string, any> {
+// jest-extended overrides the entire vitest module
+// see https://github.com/jest-community/jest-extended/issues/610
+// see types/vitest.d.ts
+export interface CustomMatchers<R> extends Record<string, any> {
/**
* Note: Currently unimplemented
* Passing assertion
File import type { CustomMatchers } from "jest-extended";
import type { Assertion, AsymmetricMatchersContaining } from "vitest";
// jest-extended overrides the entire vitest module, so we need to re-extend it here
// see https://github.com/jest-community/jest-extended/issues/610
declare module "vitest" {
interface Assertion<T = any> extends CustomMatchers<T> {}
interface AsymmetricMatchersContaining extends CustomMatchers {}
} |
If we were to fix this the same way, this would introduce a new dependency on vitest, which I don't think we want to do. |
also noticed in |
Do folks agree that reverting #600 would be a good at least temporary step? |
I agree. In the current state, However, #614 should solve this issue. Actually import 'vitest'; is the most important part of this. If I add this line manually, everything starts working again |
Keegan addressed this specifically already, and why the pr is not ideal in it's current state: #610 (comment)
|
My vote is still for just exporting the types to the It is |
I agree. It's great we can still use this lib with vitest so a copy and paste of some typings to set it up is no problem. |
Yes, we would need to add vitest as a dev dependency. IMO that's OK, but I understand if you don't want that. However, that's actually not everything which would need to be changed in the typings 🙄 see main...lukaselmer:jest-extended:main At the moment, it's an improvement either way (revert commit + docs, or add dev dependency and merge) 😉 |
The typings are part of the publishing package, and therefore the imports required for these to work correctly become real dependencies. Without this, consumers that only use jest and not vitest would be forced to use ts options to skip lib checking which is not good to impose on consumers. So, IMO it's not OK, even though I am a vitest user. |
I'm not a vitest user. Can someone validate the instructions in the PR I just opened? |
Works for me using latest vitest and jest-extended! Appreciate the fix 💯 |
This was the test I did with the fix, which seemed to work (though I'm no vitest expert) The only thing I'm not sure about is whether this part is actually needed declare namespace jest {
interface Matchers<R> {
toHaveBeenCalledAfter(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoFirstInvocation?: boolean,
): R;
toHaveBeenCalledBefore(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoSecondInvocation?: boolean,
): R;
}
} |
Actually, not sure what I'm doing wrong, but |
Did you also add the setup below (already present in doc)? import { expect } from 'vitest';
import * as matchers from 'jest-extended';
expect.extend(matchers); |
Yeah it's there |
Could there be some difference between our tsconfigs? You could also try this instead /// <reference types="vitest" />
export interface CustomMatchers<R> extends Record<string, any> {
toHaveBeenCalledAfter(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoFirstInvocation?: boolean,
): R;
toHaveBeenCalledBefore(
mock: jest.MockInstance<any, any[]> | import('vitest').MockInstance<any, any[]>,
failIfNoSecondInvocation?: boolean,
): R;
toHaveBeenCalledExactlyOnceWith(...args: unknown[]): R;
}
declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> { }
} |
Oh, I was also using jest-extended v3 to avoid the vitest changes made to the types file. That's another possible difference. |
I'm gonna go ahead and merge this. We can follow-up with improving the documentation as a follow-up. |
Bug
The vitest typings still aren't quite right. The (currently unreleased) changes from PR #600 are an improvement but still not working for me. I believe this is because the vitest module typings are being overwritten instead of augmented so you end up with errors like:
Also - it's currently only applying the matchers as Asymmetric but it should be updating the
Assertion
interface as well (as per vitest doco https://vitest.dev/guide/extending-matchers.html)I can get it working by patching the typings locally - and changing the vitest declaration to this:
I believe you need the top level import to make it an augmentation. However - this requires importing vitest and I'm not sure what will happen when using jest. It might be fine but not 100% sure.
An alternative would be not to include this declaration and let users do the configuration themselves. Not as nice - but better than current solution which overwrites the vitest types.
I'm using Typescript 5.0.4 with NodeNext/ESM enabled.
package
version: main branchnode
version: 18.6.0npm
(oryarn
) version: 9.5.1The text was updated successfully, but these errors were encountered: