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
80
votes
3 answers

What's the difference between '.toMatchObject' and 'objectContaining'

I have written the following test: it('Can decrement the current step', function () { expect(reducer(TestState, { type: 'GOTO_PREVIOUS_STEP' })).toMatchObject({ currentStep: 4 }); }); it('Can decrement the current step v2', function () { …
Julito Sanchis
  • 878
  • 1
  • 7
  • 9
79
votes
7 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
  • 88,409
  • 51
  • 246
  • 277
77
votes
7 answers

How can I exclude files from Jest watch?

I'm doing some slightly bizarre stuff using Jest for testing where I'm writing some stuff to disk. If I use the watch flag in Jest however then I'm finding (quite obviously) that each time I write something to disk the tests refire again. I don't…
Ian
  • 30,720
  • 20
  • 100
  • 179
75
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
  • 12,936
  • 13
  • 52
  • 102
74
votes
7 answers

Jest, Enzyme: Invariant Violation: You should not use or withRouter() outside a

I have a which outputs one component and list of contacts presentated by . The problem is that in the test for when I try to mount it, test outputs an error Invariant Violation:…
Maryja Piaredryj
  • 1,314
  • 3
  • 13
  • 27
74
votes
4 answers

Jest how to assert that function is not called

In Jest there are functions like toeCalled or toBeCalledWith to check if a particular function is called. Is there any way to check that a function is not called?
Sachin
  • 3,071
  • 4
  • 21
  • 28
74
votes
5 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
  • 27,627
  • 40
  • 142
  • 201
71
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
  • 4,132
  • 3
  • 33
  • 41
70
votes
5 answers

How to test a className with the Jest and React testing library

I am totally new to JavaScript testing and am working in a new codebase. I would like to write a test that is checking for a className on the element. I am working with Jest and React Testing Library. Below I have a test that will render a button…
Giesburts
  • 5,096
  • 10
  • 34
  • 70
70
votes
7 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
  • 882
  • 1
  • 6
  • 12
69
votes
6 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
  • 17,573
  • 19
  • 82
  • 120
68
votes
7 answers

SyntaxError with Jest and React and importing CSS files

I am trying to get my first Jest Test to pass with React and Babel. I am getting the following error: SyntaxError: /Users/manueldupont/test/avid-sibelius-publishing-viewer/src/components/TransportButton/TransportButton.less: Unexpected token …
Mano Dupont
  • 681
  • 1
  • 5
  • 7
66
votes
9 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
  • 4,167
  • 5
  • 32
  • 50
66
votes
5 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
  • 2,081
  • 1
  • 16
  • 24
64
votes
5 answers

Jest Mock module per test

I am quite confused with mocking in Jest an how to unit test the implementations. The thing is i want to mock different expected behaviours. Is there any way to achieve this? as imports can be only on the top of the file and to be able to mock…
Kanekotic
  • 1,944
  • 2
  • 16
  • 33