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
feat!: local types, supporting jest, @jest/globals, vitest #511
Conversation
Moves matcher types back into this package and adds support for @jest/globals and vitest. BREAKING CHANGE: Removes the extend-expect script. Users should use the default import path or one of the new test platform-specific paths to automatically extend the appropriate "expect" instance. extend-expect was not documented in the Readme, so this change should have minimal impact. Users can now use the following import paths to automatically extend "expect" for their chosen test platform: - @testing-library/jest-dom - jest (@types/jest) - @testing-library/jest-dom/jest-globals - @jest/globals - @testing-library/jest-dom/vitest - vitest For example: import '@testing-library/jest-dom/jest-globals' Importing from one of the above paths will augment the appropriate matcher interface for the given test platform, assuming the import is done in a .ts file that is included in the user's tsconfig.json. It's also (still) possible to import the matchers directly without side effects: import * as matchers from '@testing-library/jest-dom/matchers'
| @@ -0,0 +1,4 @@ | |||
| const globals = require('@jest/globals') | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my testing, @jest/globals had to use CommonJS
| @@ -0,0 +1,4 @@ | |||
| import {expect} from 'vitest' | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And Vitest only supports ESM 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, should this file extension be .mjs?
Have you test this setup in a vitest project? If not, could you verify this will work as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question. Using .mjs would be technically correct, but then all imported files would need to be ESM, which is not the case for matchers.js. Basically, vitest fails with "Error: Cannot find module".
I think we could change the extension after dual-publishing ES (#437?), but for now this works in vitest.
Have you test this setup in a vitest project?
Yeah, I've got 4 test projects that I tested this PR against: Jest+JS, Jest+TS, @jest/globals+TS, vitest+TS. I'll publish them soon.
| @@ -0,0 +1 @@ | |||
| /// <reference path="types/vitest.d.ts" /> | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A combination of <reference> and export {} in the referenced definitions seemed to be required to get TypeScript to augment the appropriate interfaces when this file was imported from node_modules.
Codecov Report
@@ Coverage Diff @@
## main #511 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 27 27
Lines 664 664
Branches 251 251
=========================================
Hits 664 664
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much for doing this work! I think it's going to be a very positive thing. I just have a couple questions.
| @@ -144,6 +180,18 @@ haven't already: | |||
| ], | |||
| ``` | |||
|
|
|||
| ### With another Jest-compatible `expect` | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be the approach if you're not using vitest's globals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure what you mean by vitest's globals. Vitest's default mode is globals: false, which requires explicit imports of describe, expect, etc., and the new vitest entrypoint is designed to work with that. I didn't actually think to test with globals: true and importing the vitest/globals types — I'll do that tomorrow.
This section was intended as a hedge against future testing libraries that might emerge that retain some level of compatibility with Jest. But if you think it might be more confusing than helpful, I'm happy to remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't actually think to test with
globals: trueand importing thevitest/globalstypes — I'll do that tomorrow.
Tested this and it works.
| @@ -0,0 +1,4 @@ | |||
| import {expect} from 'vitest' | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, should this file extension be .mjs?
Have you test this setup in a vitest project? If not, could you verify this will work as expected?
|
Thank you so much for helping with this! |
|
🎉 This PR is included in version 6.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
Very happy to see this 😊 |
|
Really excited to see this! FYI when I first plugged in this change (upgrading No idea why that happened, but the error went away when I upgraded |
This reverts #9673 and #9711. With v6 of `@testing-library/jest-dom`, `yarn rw type-check` fails in Redwood projects because it can't locate the types for `@testing-libary/jest-dom`. They absorbed the definitely types package (`@types/testing-library__jest-dom`) in this PR: testing-library/jest-dom#511. Here's an example of a failing run: https://github.com/redwoodjs/redwood/actions/runs/7213177056/job/19668365614?pr=9708. The logical thing to do is remove `@testing-libary/jest-dom` from the `types` array in `web/tsconfig.json`, but when we do that we get a different failure from TS saying that it doesn't understand the extended matchers provided by `testing-library/jest-dom` like `toBeInTheDocument`. @Tobbe filed an issue in their repo here testing-library/jest-dom#559. In the PR I linked to, the maintainer notes that... > But in my local testing with the changes in this PR, following the Readme was sufficient to get full auto-complete support even for JS-only users. As long as they have a jest-setup file that imports the bare export and add a dependency on @types/jest, recent versions of VS Code will figure it out. Users don't have jest setup files in their project (we just point to a jest preset) or `@types/jest` as an explicit dev dependency. I took a look at it but couldn't crack it today. Instead of holding up other PRs in CI, better to revert it for now and come back to later when we have more leads.
What:
Moves matcher types back into this package and adds support for
@jest/globalsandvitest, both in terms of automatically extendingexpectand augmenting the appropriate TypeScript interfaces.There are three breaking changes here:
extend-expectwas not documented in the Readme, so this change should have minimal impact.@types/dependency. This might affect JavaScript users who were relying on the automatic inclusion of the@types/package to get auto-complete in their editor for jest-dom assertions.But in my local testing with the changes in this PR, following the Readme was sufficient to get full auto-complete support even for JS-only users. As long as they have a jest-setup file that imports the bare export and add a dependency on
@types/jest, recent versions of VS Code will figure it out.In addition to the above changes,
@jest/globalsandvitestusers can now use jest-dom without caveats. Users can now use the following import paths to automatically extend "expect" for their chosen test platform:For example:
Importing from one of the above paths will augment the appropriate matcher interface for the given test platform, assuming the import is done in a .ts file that is included in the user's tsconfig.json.
It's also (still) possible to import the matchers directly without side effects:
Why:
There have been numerous issues filed over the last couple years as the JS testing ecosystem has evolved and users wanted to use this library in more environments. It was usually possible, thanks to broad compatibility efforts in the ecosystem at large, but using jest-dom outside of Jest was not always straightforward.
Originally, moving the types to DT was in service of a noble "it should just work" goal, but in practice, it traded convenience in simple cases for confusion in more complex cases. It also broke with convention around DT packages — why does the actual package have a dependency on its own types? Why not simply move the types into the package? Those were valid questions that had legitimate answers, but they grew stale with time.
Fixes #314
Fixes #427
Closes #472
Fixes #506
Fixes #476
Closes #484
Closes #496
Closes #483
Closes #460
How:
@types/testing-library__jest-dominto this repojest.d.tsfor extending global interfaces when using @types/jestjest-globals.d.tsfor extending interfaces when using @jest/configvitest.d.tsfor extending interfaces when using vitest/jest-globalsand/vitestexports for automatically extending @jest/globals and vitest, respectivelyChecklist: