Skip to content
Advertisement

Tag: jestjs

Jest: How to undo a global mock for certain tests in a file

I want to mock Math.random for certain tests and use its original implementation for other tests. How can I achieve this? I’ve read about using jest.doMock and jest.dontMock, but I’ve bumped into a number of issues using them, like: I seem to need require in order to use doMock and dontMock, but my project only uses ES6 modules for importing

Unit test with react hook fails

I don’t understand the result I get on this unit test. I expect the second check for textField.valid to be true and instead it returns false. Below is part of the component I’m testing against: … Below is the test I’m running: The output of console.log(wrapper.debug()) is the following: So why does the test fail? Answer The problem was that

Jest: TypeError: replaceAll is not a function

String.prototype.replaceAll() is a useful method and while building and executing everything works fine. However, all Jest-tests fail with the following error: These are my dependencies: How can I fix this behavior? Answer The problem This happens because replaceAll is a new function not implemented in all browsers nor older Node.js versions (older than 15.0), as mentioned in the other answer.

Mocking dayjs extend

In my code that needs testing I use In my test I need to mock dayjs in order to always have the same date when comparing snapshots in jest so I did It fails with Unfortunately similar questions on here didn’t help me. How could I mock both default dayjs but also extend? Answer You could write a more thorough

Trouble finding Jest on Windows

I recently started off with some project over JavaScript, following some documentation. I am supposed to be using Jest for testing purposes. The installation of Jest seems pretty straightforward on Linux using npm: However, I am on Windows, and unable to find Jest over lets using NuGet. How can I retrieve Jest package over Windows? Answer NPM is bundled with

Advertisement