Skip to content

Tag: jestjs

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…

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…

‘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…