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
138
votes
10 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 `I…
Mark
  • 12,359
  • 5
  • 21
  • 37
137
votes
15 answers

How to mock a constructor like new Date()

I have a method which depends on new Date to create a date object and then manipulates it. I'm testing that the manipulation works as expected, so I need to compare the returned date with expected date. In order to do that I need to make sure that…
Seth Feldkamp
  • 1,406
  • 2
  • 9
  • 8
136
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
  • 16,766
  • 21
  • 72
  • 104
134
votes
4 answers

Jest how to assert that function is not called

In Jest there are functions like tobeCalled or toBeCalledWith to check if a particular function is called. Is there any way to check that a function is not called?
Sachin
  • 4,621
  • 6
  • 25
  • 31
132
votes
5 answers

How to solve the "update was not wrapped in act()" warning in testing-library-react?

I'm working with a simple component that does a side effect. My test passes, but I'm getting the warning Warning: An update to Hello inside a test was not wrapped in act(...).. I'm also don't know if waitForElement is the best way to write this…
Pablo Darde
  • 5,844
  • 10
  • 37
  • 55
132
votes
13 answers

How do you debug Jest Tests?

I can't find any information on debugging my unit tests written with Jest. How do you debug Jest Tests ?
BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
130
votes
18 answers

Property 'toBeInTheDocument' does not exist on type 'Matchers'

I'm trying to write tests for my simple React App that creates a UI for a dog Shelter using API etc. I have imported the modules shown below and ran the following command npm install jest-dom react-testing-library --save-dev However, I'm getting…
mangokitty
  • 1,759
  • 3
  • 12
  • 17
130
votes
7 answers

Typescript and Jest: Avoiding type errors on mocked functions

When wanting to mock external modules with Jest, we can use the jest.mock() method to auto-mock functions on a module. We can then manipulate and interrogate the mocked functions on our mocked module as we wish. For example, consider the following…
duncanhall
  • 11,035
  • 5
  • 54
  • 86
130
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
  • 1,406
  • 2
  • 10
  • 10
130
votes
7 answers

Service mocked with Jest causes "The module factory of jest.mock() is not allowed to reference any out-of-scope variables" error

I'm trying to mock a call to a service but I'm struggeling with the following message: The module factory of jest.mock() is not allowed to reference any out-of-scope variables. I'm using babel with ES6 syntax, jest and enzyme. I have a simple…
Ria
  • 1,900
  • 4
  • 14
  • 21
129
votes
9 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
  • 1,301
  • 2
  • 8
  • 7
128
votes
6 answers

How to change the behaviour of a mocked import?

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
  • 2,824
  • 3
  • 21
  • 35
125
votes
2 answers

Difference between resetAllMocks, resetModules, resetModuleRegistry, restoreAllMocks in Jest

I'm trying to wrap my head around the following in Jest: resetAllMocks, resetModules, resetModuleRegistry and restoreAllMocks and I'm finding it difficult. I read the jest documentation but it's not too clear. I would appreciate it if someone can…
tmp dev
  • 8,043
  • 16
  • 53
  • 108
125
votes
10 answers

How to test anchor's href with react-testing-library

I am trying to test my anchor tag. Once I click it, I want to see if the window.location.href is what I expect. I've tried to render the anchor, click it, and then test window.location.href: test('should navigate to ... when link is clicked', () =>…
jaypee
  • 1,757
  • 3
  • 9
  • 12
121
votes
4 answers

How do I test a jest console.log

I'm using create-react-app and trying to write a jest test that checks the output of a console.log. My function to test is: export const log = logMsg => console.log(logMsg); My test is : it('console.log the text "hello"', () => { console.log =…
Hello-World
  • 9,277
  • 23
  • 88
  • 154