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
186
votes
5 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
  • 5,953
  • 5
  • 24
  • 32
186
votes
11 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
  • 2,620
  • 3
  • 16
  • 20
184
votes
5 answers

Loose match one value in jest.toHaveBeenCalledWith

I have an analytics tracker that will only call after 1 second and with an object where the intervalInMilliseconds (duration) value is not deterministic. How can I use jest.toHaveBeenCalledWith to test the object? test('pageStats - publicationPage…
dotnetCarpenter
  • 10,019
  • 6
  • 32
  • 54
183
votes
5 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
  • 24,894
  • 8
  • 75
  • 90
183
votes
20 answers

Jest test fails : TypeError: window.matchMedia is not a function

This is my first front-end testing experience. In this project, I'm using Jest snapshot testing and got an error TypeError: window.matchMedia is not a function inside my component. I go through Jest documentation, I found the "Manual mocks" section,…
TIJ
  • 2,771
  • 3
  • 19
  • 32
183
votes
12 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
  • 43,228
  • 68
  • 238
  • 348
182
votes
16 answers

react-testing-library why is toBeInTheDocument() not a function

Here is my code for a tooltip that toggles the CSS property display: block on MouseOver and on Mouse Out display: none. it('should show and hide the message using onMouseOver and onMouseOut events respectively', () => { const { queryByTestId,…
dorriz
  • 1,927
  • 3
  • 12
  • 19
181
votes
14 answers

Jest gives an error: "SyntaxError: Unexpected token export"

I'm using Jest to test my React app. Recently, I added DeckGL to my app. My tests fail with this error: Test suite failed to run /my_project/node_modules/deck.gl/src/react/index.js:21 export {default as DeckGL} from…
Don P
  • 60,113
  • 114
  • 300
  • 432
173
votes
20 answers

How to mock window.location.href with Jest + Vuejs?

Currently, I am implementing unit tests for my project and there is a file that contains window.location.href. I want to mock this to test and here is my sample code: it("method A should work correctly", () => { const url =…
Hoang Subin
  • 6,610
  • 6
  • 37
  • 56
173
votes
14 answers

Jest gives `Cannot find module` when importing components with absolute paths

Receiving the following error when running Jest Cannot find module 'src/views/app' from 'index.jsx' at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:179:17) at Object. (src/index.jsx:4:12) index.jsx import…
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
172
votes
8 answers

Run only ONE test with Jest

I want to run just one test with Jest. I use it.only or describe.only, but it still runs a whole lot of tests. I think it runs all the tests since my last commit, but it shouldn't have this behavior with the only flag explicitly set, right? What…
jpenna
  • 8,426
  • 5
  • 28
  • 36
170
votes
8 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
  • 6,879
  • 15
  • 48
  • 85
168
votes
14 answers

Jest SecurityError: localStorage is not available for opaque origins

When I want to run my project with the command npm run test, I get the error below. What is causing this? FAIL ● Test suite failed to run SecurityError: localStorage is not available for opaque origins at Window.get localStorage [as localStorage]…
amirdehghan
  • 1,601
  • 2
  • 7
  • 4
168
votes
21 answers

Absolute paths (baseUrl) gives error: Cannot find module

I am setting a configuration to run my tests in a create-react-app + typescript app (from which I have ejected). I am using jest + enzyme. In my tsconfig.json I have set baseUrl='./src' so I can use absolute paths when I import modules. For example…
TheSoul
  • 4,906
  • 13
  • 44
  • 74
166
votes
13 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
  • 33,605
  • 26
  • 118
  • 198