I have to components imported with the new React lazy API (16.6). In my tests, I’m doing the snapshots of this component. It’s a very straightforward test: In the logs, the test is failing with this error: Do I have to wrap in every single test suite with <Suspense>…? If I do that, I o…
Tag: jestjs
How to mock window.navigator.language using jest
I am trying to mock the window.navigator.language attribute in the browser in my jest unit tests so I can test that the content on my page is using the correct language I have found people online using this: Object.defineProperty(window.navigator, ‘language’, {value: ‘es’, configurable…
Testing debounced function in React component with Jest and Enzyme
I am testing a React component using Jest and Enzyme, and am having difficulty testing that a debounced function is called properly (or at all). I’ve simplified the component code below (edited to make code even simpler), link to codepen here I figured that the debounced function test should be pretty s…
Jest unit test: setTimeout not firing in async test
I’m trying to understand how asynchronous testing works in Jest. What I’m trying to do is similar to an example from the Jest documentation. This works fine .. But I want to delay the callback invocations so I tried this …. but the test fails with the message Expected two assertions to be ca…
How to use Jest to test functions using crypto or window.msCrypto
When running unit tests with Jest in react the window.crypto API is causing problems. I haven’t found a way to incorporate crypto in Jest without installing other packages which is something I can’t do. So without using another npm package is there a way to test functions that use: crypto.getRando…
Remove logging the origin line in Jest
Jest has this feature to log the line that outputs to console methods. In some cases, this can become annoying: Any idea how I can turn it off? Answer None of the above options worked for me. The (current) simplest solution is this: 1: Create a file with this code (e.g. config.js) 2: Add this line to your jes…
How to determine if JEST is running the code or not?
I am creating a JS test on my react-native project. I’m specifically using firebase for react native, in which I would like to replace firebase instance with a mockfirebase instance if JS is running the code of my class. For example I have class setup like below. I’d like to have a check if jest i…
‘command not found: jest’
I have a test file like so: (I am using create-react-app) and a package.json: when I run jest –updateSnapshot I get: but jest is installed. Answer Jest is installed, but is likely in your ./node_modules/.bin directory. You can append that to your command ./node_modules/.bin/jest –updateSnapshot. S…
Jest TypeError: fetch is not a function
I have the following Jest test code to test a fetch to an endpoint: But I am getting: I know that the movieApiService.getPopularMovies() is a JavaScript fetch request, but Node.js does not have the fetch API, so how I can I make this test to work using Jest? Answer I can’t test this with the code you su…
asserting against thrown error objects in jest
I have a function which throws an object, how can I assert that the correct object is thrown in jest? https://repl.it/repls/FrayedViolentBoa Answer If you are looking to test the contents of a custom error (which I think is what you are trying to do). You could catch the error then perform an assertion afterw…