Replies: 2 comments 10 replies
-
Jest makes The TextEncoder issue comes from JSDOM: jsdom/jsdom#2524 The solution is to provide the TextEncoder within your Check this repository out: https://github.com/vercel/next.js/tree/canary/examples/with-jest ~ it also uses TypeScript across the board Several things are kind of hit or miss when testing. You have to understand that, to simulate the DOM, we use JSDOM and are limited by its capabilities. And also Jest itself, doesn't really understand TypeScript, nor ESM, etc, so you have to handle that yourself. Next.js' setup handles the TypeScript processing for you. As your unit testing skill grows, you'll see that one has to balance between, adding stuff to the JSDOM globals, mocking them, or just straight up changing your testing strategy, because, otherwise, one would sink a lot more of time adding/mocking, and most likely end up testing your own mocks, instead of your application code. |
Beta Was this translation helpful? Give feedback.
-
Ok, I got there in the end. I'm not gonna leave everyone in the dust, so to recap my changes on top of what you'll get from the documentation (at the time of writing this): 1. Changes to
|
Beta Was this translation helpful? Give feedback.
-
Summary
I'm following the official documentation for this: https://nextjs.org/docs/app/building-your-application/testing/jest
First of all I'm curious how long ago this documentation was reviewed with the latest versions of Next.js and related packages, because there are problems from the get-go.
1. Typescript errors in jest.config.ts
Using the exact same config as in the documentation, the very last line of the config, where it says:
Has an error:
In order to fix this:
I wonder why this is neccesary, and why this hasn't been added to the documentation.
2. Errors in the test file
The documentation carries on to suggest a sample test file, called
page.test.jsx
. For some reason this has to be a JSX suddenly, while the rest of the documentation is typescript. So I naively make it apage.test.tsx
and I am immediately confronted with errors.The functions
describe
,it
andexpect
are not defined. No wonder, they are not imported from anywhere. Where do I import them from? My VSCode suggest to import them fromnode:test
but I am fairly confident this won't be the correct package to import them from.So which is it? And then please also add this to the documentation - and add a typescript version of the test as well. Without the correct imports of test functions, surely it will error in javascript with type checking, just as it will in typescript.
3. Errors in the test itself
If I carry on and mentally ignore the errors from (2), to run the example test, I find that it too, produces errors. And not the right kinds of errors.
There are quite a few people running into this problem and providing a workaround that seems to mitigate the situation, where in the
jest.setup.ts
, you would add:I wonder if this should be neccesary. Especially because after adding this, it just starts complaining about the next global it reportedly can't find:
So I'm like, "what's next?" Do I assign all possible global objects to the global object? Surely, this shouldn't be neccesary. Surely, some kind of config is missing, that the documentation skips. But what? And please, whatever the solution turns out to be, add it to the documentation.
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions