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

15677 questions
93
votes
3 answers

Skip one test in test file Jest

I'm using Jest framework and have a test suite. I want to turn off/skip one of my tests. Googling documentation doesn't give me answers. Do you know the answer or source of information to check?
Gleichmut
  • 3,806
  • 3
  • 20
  • 29
93
votes
3 answers

What is the difference between describe and it in Jest?

When writing a unit test in Jest or Jasmine when do you use describe? When do you use it? I usually do describe('my beverage', () => { test('is delicious', () => { }); }); When is it time for a new describe or a new it?
Brown Limie
  • 1,239
  • 1
  • 11
  • 16
92
votes
7 answers

Does Jest support ES6 import/export?

If I use import/export from ES6 then all my Jest tests fail with error: Unexpected reserved word I convert my object under test to use old school IIFE syntax and suddenly my tests pass. Or, take an even simpler test case: var Validation =…
P.Brian.Mackey
  • 39,360
  • 59
  • 210
  • 327
91
votes
2 answers

How to properly make mock throw an error in Jest?

I'm testing my GraphQL api using Jest. I'm using a separate test suit for each query/mutation I have 2 tests (each one in a separate test suit) where I mock one function (namely, Meteor's callMethod) that is used in mutations. it('should throw…
Le garcon
  • 3,929
  • 5
  • 23
  • 38
88
votes
9 answers

How to mock an exported const in jest

I have a file that relies on an exported const variable. This variable is set to true but if ever needed can be set to false manually to prevent some behavior if downstream services request it. I am not sure how to mock a const variable in Jest so…
Mdd
  • 3,240
  • 9
  • 38
  • 65
86
votes
7 answers

Jest: How to mock one specific method of a class

Let's suppose I have the following class: export default class Person { constructor(first, last) { this.first = first; this.last = last; } sayMyName() { console.log(this.first + " " + this.last); } bla()…
CrazySynthax
  • 9,442
  • 21
  • 70
  • 136
85
votes
4 answers

What is the difference between 'toBe' and 'toEqual' in Jest?

Jest documentation reads: toBe just checks that a value is what you expect. It uses === to check strict equality. And for toEqual: Use .toEqual when you want to check that two objects have the same value. This matcher recursively checks the…
sshh
  • 2,883
  • 3
  • 14
  • 20
85
votes
7 answers

How to mock functions in the same module using jest

What's the best way to correctly mock the following example? The problem is that after import time, foo keeps the reference to the original unmocked bar. module.js: export function bar () { return 'bar'; } export function foo () { return…
Mark
  • 11,575
  • 4
  • 18
  • 35
85
votes
3 answers

Running CRA Jest in non-interactive mode

Update: my use case is mainly to run tests at CI, but overriding default CRA Jest parameters is something I'm generally wondering about. I'm running tests using the Jest, config that came with Create React App. It always launches into the…
AlexStack
  • 13,994
  • 14
  • 68
  • 98
84
votes
5 answers

Is there an option to show all test descriptions when I run jest tests?

I'm using jest and enzyme with my create-react-app project. When I run npm test, I get an output that shows the names of the test files that passed but I'd like the output to also include the names of the tests. Example: Button.test.js it…
Sendai
  • 1,009
  • 1
  • 5
  • 9
84
votes
9 answers

Is there an Array equality match function that ignores element position in jest.js?

I get that .toEqual() checks equality of all fields for plain objects: expect( {"key1":"pink wool","key2":"diorite"} ).toEqual( {"key2":"diorite","key1":"pink wool"} ); So this passes. But the same is not true for arrays: expect(["pink…
Benjamin H Boruff
  • 1,350
  • 1
  • 14
  • 18
83
votes
4 answers

How to reset or clear a spy in Jest?

I have a spy that is used in multiple assertions across multiple tests in a suite. How do I clear or reset the spy so that in each test the method that the spy intercepts is considered not to have been invoked? For example, how to make the assertion…
sdgluck
  • 18,456
  • 4
  • 56
  • 83
82
votes
3 answers

how to change jest mock function return value in each test?

I have a mock module like this in my component test file jest.mock('../../../magic/index', () => ({ navigationEnabled: () => true, guidanceEnabled: () => true })); these functions will be called in render function of my component to…
pashaplus
  • 2,730
  • 1
  • 22
  • 23
81
votes
6 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
  • 4,813
  • 2
  • 27
  • 42
80
votes
20 answers

Cannot find name 'describe'. Do you need to install type definitions for a test runner?

When using TypeScript in conjunction with Jest, my specs would fail with error messages like: test/unit/some.spec.ts:1:1 - error TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest`…
Ronin
  • 5,059
  • 3
  • 29
  • 52