Skip to content
Advertisement

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: true}); I have set it right at the top of my test file and it is working

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 called but received zero assertion calls.. The

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 jest.config.js Before:

‘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. Since you already have jest as a scripts command in your package.json you can also

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 supply, but installing

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 afterwards. As Dez has suggested the toThrowError

Advertisement