Skip to content
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

example-intro page provides an example using msw, which does not work with jest out of the box #1367

Open
skondrashov opened this issue Feb 14, 2024 · 4 comments

Comments

@skondrashov
Copy link

I've been working on setting up tests in a project for a few days now, and I am genuinely confused how people have been setting up RTL using the docs for the last 3 or 4 years since msw replaced the mocking examples that were previously in the docs. The docs explicitly recommend jest, msw explicitly does not work with jsdom, which is required to run tests with jest. How have people been able to use the docs for the last several years? I feel like I'm missing something huge since I don't see a flood of other developers leaving similar issues and comments, but what is the recommended thing to do? Should I abandon jest like @kettanaito recommends, and can the docs reflect that if so? Can the docs include the required polyfills if everyone is just stackoverflowing their way through it like I've been trying to?

@kettanaito
Copy link
Contributor

Hi, @skondrashov.

I think a link to here in the RTL example page would be useful. Something like: Note that you need additional configuration for Jest in order to use it with the global Fetch API ().

I would also love to see that example just mentioning Vitest, which eliminates the need for any kind of fixes.

@skondrashov
Copy link
Author

skondrashov commented Feb 14, 2024

That doc does not include a ReadableStream polyfill (and possibly other polyfills if I get that one to work), which I've found that you've created a package for, but a link to that document is not sufficient to get the example working.

EDIT: require('readable-stream-polyfill/globals') throws Cannot find module 'readable-stream-polyfill/globals' from 'jest.polyfills.js'. I can't really be the only person who has tried to start using RTL in the last 3 years, can I? It's totally beyond my understanding why the docs would recommend a library that just straight up does not work with jest or why it's not in msw's interests to just include the polyfills that it requires to run.

@skondrashov
Copy link
Author

If anyone finds themselves here in search of usable docs, they can be found here: https://github.com/testing-library/testing-library-docs/blob/cb41b06ed6cf01d9f894262f1cd58adbb0d2dbfc/docs/react-testing-library/example-intro.md

@kettanaito
Copy link
Contributor

That doc does not include a ReadableStream polyfill

Polyfilling ReadableStream is not recommended. Not everyone has issues with ReadableStream missing so it's dangerous to include that in the docs. If you polyfill ReadableStream, you start relying on structuredClone(), which is yet another Node.js global that jest-environment-jsdom takes away from you and incorrectly polyfills back via core-js. Read more: mswjs/msw#1916 (comment)

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

No branches or pull requests

2 participants