Questions tagged [jestjs]

Jest is a JavaScript unit testing framework made by Facebook based on Jasmine and provides automated mock creation and a jsdom environment. It's often used for testing React components.

It has a couple of advantages compared to vanilla

  • Automatically finds tests to execute in your source code
  • Automatically mocks dependencies when running your tests
  • Allows you to test asynchronous code synchronously
  • Runs your tests with a fake DOM implementation (via ) so that your tests can be run on the command line
  • Runs tests in parallel processes so that they finish sooner

Resources

22701 questions
120
votes
2 answers

How can I test part of object using Jest?

I would like to test that time is parsed correctly and I am only interested in checking some of the properties and not the entire object. In this case hour and minutes. I tried using expect(object).toContain(value) but as you can see in the snippet…
mancristiana
  • 1,896
  • 3
  • 18
  • 28
118
votes
11 answers

In Jest, how can I make a test fail?

I know I could throw an error from inside the test, but I wonder if there is something like the global fail() method provided by Jasmine?
kYuZz
  • 1,572
  • 4
  • 14
  • 25
115
votes
8 answers

How do I set a timezone in my Jest config?

✗ npx jest --version 24.5.0 Got a set of jest tests that are timezone sensitive. We typically run them with an npm script: "jest": "TZ=utc jest" With the TZ set to utc I get values like this in snapshots:…
jcollum
  • 43,623
  • 55
  • 191
  • 321
115
votes
9 answers

Watch and rerun Jest JS tests

The Jest documentation suggests using npm test to execute tests. Is there a way of watching your source and tests to rerun Jest tests automatically when relevant files have been changed?
Martin Dow
  • 5,273
  • 4
  • 30
  • 43
111
votes
8 answers

How to test a react component that is dependent on useContext hook?

I have a component that uses useContext and then its output is dependent on the value in the context. A simple example: import React, { useContext } from 'react'; const MyComponent = () => { const name = useContext(NameContext); return…
bensampaio
  • 3,266
  • 5
  • 21
  • 19
111
votes
22 answers

ReferenceError: You are trying to `import` a file after the Jest environment has been torn down

I have a component that makes use of Animated component from react native. I started writing a test case to simulate onPress of a component, which calls a function that has Animated.timing in it, and setState. running jest works fine, but the tests…
nrion
  • 4,258
  • 4
  • 17
  • 33
111
votes
6 answers

Mocking globals in Jest

Is there any way in Jest to mock global objects, such as navigator, or Image*? I've pretty much given up on this, and left it up to a series of mockable utility methods. For example: // Utils.js export isOnline() { return…
Andrew
  • 14,204
  • 15
  • 60
  • 104
107
votes
20 answers

'command not found: jest'

I have a test file like so: (I am using create-react-app) import React from 'react'; import ReactDOM from 'react-dom'; import App from './components/Calculator'; import { getAction, getResult } from './actions/' import {shallow} from…
Aessandro
  • 5,517
  • 21
  • 66
  • 139
107
votes
7 answers

Jest looping through dynamic test cases

How do I loop through dynamic test cases in Jest? I have test cases like the following how do I dynamically create jest test case using it/test methods. Here is what I have tried , However it just passes without excuting the test cases in the…
mehari
  • 3,057
  • 2
  • 22
  • 34
107
votes
9 answers

Testing with Jest and Webpack aliases

I am looking to be able to use webpack aliases to resolve imports when using jest, and optimally, reference the webpack.aliases to avoid duplication. Jest conf: "jest": { "modulePaths": ["src"], "moduleDirectories": ["node_modules"], …
speak
  • 5,202
  • 4
  • 36
  • 41
104
votes
4 answers

describe is not defined when installing jest

I installed jest v24.7.1in my project with: npm install jest -D Then I start writing some test files, However I got these eslint errors: 'describe' is not defined. eslint (no-undef) 'it' is not defined. eslint (no-undef) 'expect' is not defined.…
Slim
  • 5,527
  • 13
  • 45
  • 81
103
votes
8 answers

Jest mock inner function

I have one file called helper.js that consist of two functions export const funcA = (key) => { return funcB(key) }; export const funcB = (key,prop) => { return someObj; }; I have my helper.spec.js to test the helper.js file functions. import…
Alexander Gorelik
  • 3,985
  • 6
  • 37
  • 53
102
votes
1 answer

Upgrading Jest to v29 - Error Test environment jest-environment-jsdom cannot be found

Has anyone successfully upgraded to latest Jest version 29? I'm receiving an error: Error: Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module.
marko424
  • 3,839
  • 5
  • 17
  • 27
102
votes
8 answers

Jest - how to test if a component does not exist?

How do I check if a component is not present, i.e. that a specific component has not been rendered?
JoeTidee
  • 24,754
  • 25
  • 104
  • 149
102
votes
2 answers

Why does Jest --runInBand speed up tests?

I read that the --runInBand flag speeds up Jest test duration by 50% on CI servers. I can't really find an explanation online on what that flag does except that it lets tests run in the same thread and sequentially. Why does running the test in the…
Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141