testing-library / testing-library-docs

docs site for @testing-library/*
https://testing-library.com
MIT License
451 stars 701 forks source link

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

Open skondrashov opened 7 months ago

skondrashov commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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: https://github.com/mswjs/msw/issues/1916#issuecomment-1908544946