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
164
votes
13 answers

How to add custom message to Jest expect?

Image following test case: it('valid emails checks', () => { ['abc@y.com', 'a@b.nz'/*, ...*/].map(mail => { expect(isValid(mail)).toBe(true); }); }); I would like to add auto-generated message for each email like Email 'f@f.com' should be…
Jurosh
  • 6,984
  • 7
  • 40
  • 51
163
votes
7 answers

Jest.js error: "Received: serializes to the same string"

I've having a strange problem with this test: deal.test.js import Deal from "../src/models/Deal"; import apiProducts from "../__mocks__/api/products"; describe("Deal", () => { describe("Deal.fromApi", () => { it("takes an api product and…
Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
163
votes
7 answers

How to make Jest wait for all asynchronous code to finish execution before expecting an assertion

I am writing an integration test for for a React application, i.e. a test that tests many components together, and I want to mock any calls to external services. The issue is that the test seems to execute before the async callback is executed…
Dan
  • 29,100
  • 43
  • 148
  • 207
160
votes
5 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
  • 5,964
  • 4
  • 17
  • 20
158
votes
12 answers

react-testing-library: some portion of debug's output is not visible

I am using react jest with react testing library to test my component. I am facing a weird issue. I am using debug, returned by render from testing-library. test('component should work', async () => { const { findByText, debug } =…
Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37
158
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
  • 3,596
  • 2
  • 26
  • 25
155
votes
7 answers

How do I test axios in Jest?

I have this action in React: export function fetchPosts() { const request = axios.get(`${WORDPRESS_URL}`); return { type: FETCH_POSTS, payload: request } } How do I test Axios in this case? Jest has this use case on…
Adear
  • 1,885
  • 2
  • 11
  • 18
152
votes
15 answers

Jest - Simple tests are slow

I am using Jest to test an angular app and it is taking a really long time for simple tests to run and I can not seem to figure out why. My Jest setup in package.json: "jest": { "modulePaths": [ "/src", "/node_modules" …
Tucker
  • 1,722
  • 2
  • 10
  • 12
150
votes
15 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
  • 4,140
  • 12
  • 45
  • 70
149
votes
11 answers

How to check multiple arguments on multiple calls for jest spies?

I have the following function in a React component: onUploadStart(file, xhr, formData) { formData.append('filename', file.name); formData.append('mimeType', file.type); } This is my test that at least gets the spy to be called: const formData…
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
148
votes
3 answers

Jest: how to mock console when it is used by a third-party-library?

I am trying to mock console.warn/error but I can't. I use a third-party-library which calls console.warn inside it. I need to test was it called or wasn't. In my test case I was trying to stub console.warn but it didn't help. After that I was trying…
Errorpro
  • 2,253
  • 2
  • 16
  • 17
147
votes
18 answers

Jest won't transform the module - SyntaxError: Cannot use import statement outside a module

I couldn't get rid of this SyntaxError: Cannot use import statement outside a module error no matter what I have tried and it got so frustrating. Is there anybody out here solved this issue? I have read a million stackoverflow and github issue…
dugong
  • 3,690
  • 4
  • 11
  • 27
145
votes
6 answers

Can I mock functions with specific arguments using Jest?

I want to mock a function with Jest, but only if it is called with specific arguments, for example: function sum(x, y) { return x + y; } // mock sum(1, 1) to return 4 sum(1, 1) // returns 4 (mocked) sum(1, 2) // returns 3 (not mocked) There…
Nícolas Iensen
  • 3,899
  • 4
  • 22
  • 26
143
votes
9 answers

Simulate a button click in Jest

Simulating a button click seems like a very easy/standard operation. Yet, I can't get it to work in Jest.js tests. This is what I tried (and also doing it using jQuery), but it didn't seem to trigger anything: import { mount } from 'enzyme'; page =…
foobar
  • 3,849
  • 8
  • 22
  • 32
140
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,625
  • 2
  • 8
  • 14