Skip to content

docs: Update React setup.mdx with Vitest configuration instructions #1195

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

Closed

Conversation

pjchender
Copy link

Vitest is becoming a popular tool for starting a React project from scratch. However, there is still no documentation on how to use it. I felt it would be useful to update the React developer documentation with Vitest instructions.

Please let me know if the section needs to be reorganized or if further information is required.

@netlify
Copy link

netlify bot commented Dec 28, 2022

Deploy Preview for testing-library ready!

Name Link
🔨 Latest commit e08c6d2
🔍 Latest deploy log https://app.netlify.com/sites/testing-library/deploys/63ed0519695d500008899f98
😎 Deploy Preview https://deploy-preview-1195--testing-library.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much appreciated! There are some details that we should clarify before merging though.

npm install --save-dev @testing-library/react @testing-library/jest-dom
```

Since the testing library uses jsdom, you must set the `environments`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither /react nor /dom "use" JSDOM. But both libraries require a DOM implementation to be available. The config and recommendation for JSDOM is fine but the wording needs some tweaking to make sure people are aware that they could also use a Browser or other DOM implementations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing this out. I tweak the wording to make it more clear.

testing. However, some further steps might help you develop more efficiently.

When utilizing testing-library APIs, you must first import
`@testing-library/jest-dom` in every file. It's quite inconvenient.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For /react you don't need @testing-library/jest-dom. Are we also sure @testing-library/jest-dom library even works Vitest?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I am not sure if this is what you are asking. If we set the globals to true, then the matchers will be automatically extended when importing @testing-library/jest-dom. However, we must extend the matchers manually if we do not set it to true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure but this document is about @testing-library/react not @testing-library/jest-dom. Let's split this out into a separate PR since I'm not sure jest-dom wants to support Vitest.

By default, vitest does not provide [global](https://vitest.dev/config/#globals) APIs
for explicitness, so you have to import the `describe` or `test` from vitest
when using them. If you want to use them globally without importing them,
you can set `globals` to `true` in the `vite.config.js`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check if auto-cleanup works with Vitest with globals? We should document test cleanup for both config options.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auto-cleanup works as usual if we set the globals to true.

I also add a paragraph to describe how to do the cleanup and extend matchers manually when the globals is false.

@pjchender pjchender requested a review from eps1lon January 3, 2023 02:14
Comment on lines 583 to 604
To extends the matchers in Vitest, you just need to import the `@testing-library/jest-dom`
after setting `test.globals` to `true`. To do this, create a `setupTests` file
and import `@testing-library/jest-dom`.

```js
// src/setupTests.js
import '@testing-library/jest-dom'
```

Vitest will run `src/setupTests.js` before each test file, as configured in `setupFiles`.

```js
// vite.config.js
export default defineConfig({
test: {
environment: "jsdom",
globals: true,
// highlight-next-line
setupFiles: "src/setupTests.js"
},
})
```
Copy link
Author

@pjchender pjchender Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eps1lon, do you suggest removing this paragraph from this PR?

Having said that, even though it's more related to /jest-dom, developers who use @testing-library/react must know how to set it up if they want to use /react. They probably only check and follow the documentation here, assuming that /react will work out without reading the doc in /jest-dom.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. jest-dom docs should handle this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for Vitest you'd use vitest-dom instead of jest-dom

Copy link
Author

@pjchender pjchender Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think many people use vitest-dom for testing react components with Vitest. Additionally, without addressing the step of creating the setupTests file, it can be challenging to set up the testing environment for React. Please correct me if I am wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not really relevant. jest-dom is not for usage with Vitest. vitest-dom is. I don't see how the popularity of vitest-dom is relevant here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reply. I understand your points now. My documentation appears to be written for @testing-library/jest-dom but not for @testing-library/react, right?

Should I close this Pull Request here and create a new one for @testing-library/jest-dom? Since you mentioned that jest-dom is not meant to be used with Vitest, I'm considering whether I should create a Pull Request. I know that Vitest is compatible with Jest. Does this mean that Vitest is also compatible with jest-dom? What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your PR is targetting @testing-library/react. There's no need to target @testing-library/jest-dom since that library is not supposed to be used with Vitest.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eps1lon thanks. I've removed the paragraph. Could you check it again?

@pjchender pjchender closed this Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants