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
265
votes
5 answers

How can I clear the Jest cache?

Jest is picking up an old version of a package and thus my tests fail unless I use --no-cache. I can even delete the package folder from folder node_modules and Jest is happy to run the tests (almost all are passing). So how do I clear the Jest…
Manuel
  • 10,869
  • 14
  • 55
  • 86
253
votes
6 answers

How can I test for object keys and values equality using Jest?

I have a mapModule where I import components and export them: import ComponentName from '../components/ComponentName'; export default { name: ComponentName, }; How can I test that mapModule has the correct exported keys, values and that they are…
fasenberg
  • 2,735
  • 3
  • 10
  • 16
247
votes
30 answers

How do I deal with localStorage in jest tests?

I keep getting "localStorage is not defined" in Jest tests which makes sense but what are my options? Hitting brick walls.
Chiedo
  • 7,288
  • 3
  • 26
  • 22
239
votes
6 answers

How to reset Jest mock functions calls count before every test

I'm trying to use it for testing if a function was called or not. I noticed the mock.calls.length is not resetting for every test but accumulating. How can I make it 0 before every test? I don't want my next tests depends on the results of the…
Alex Efremov
  • 6,304
  • 4
  • 15
  • 27
238
votes
12 answers

Better way to disable console inside unit tests

I wonder if there is a better way to disable console errors inside a specific Jest test (i.e. restore the original console before/after each test). Here is my current approach: describe("Some description", () => { let consoleSpy; beforeEach(()…
Apidcloud
  • 3,558
  • 2
  • 21
  • 23
233
votes
8 answers

How to change mock implementation on a per single test basis?

I'd like to change the implementation of a mocked dependency on a per single test basis by extending the default mock's behaviour and reverting it back to the original implementation when the next test executes. More briefly, this is what I'm trying…
Andrea Carraro
  • 9,731
  • 5
  • 33
  • 57
229
votes
17 answers

How can I mock the JavaScript 'window' object using Jest?

I need to test a function which opens a new tab in the browser openStatementsReport(contactIds) { window.open(`a_url_${contactIds}`); } I would like to mock window's open function, so I can verify the correct URL is passed in to the open…
danny
  • 3,046
  • 3
  • 23
  • 28
213
votes
7 answers

How can I get the arguments called in jest mock function?

How can I get the arguments called in jest mock function? I want to inspect the object that is passed as argument.
Bruno Quaresma
  • 9,457
  • 7
  • 32
  • 50
209
votes
15 answers

Mock dependency in Jest with TypeScript

When testing a module that has a dependency in a different file and assigning that module to be a jest.mock, TypeScript gives an error that the method mockReturnThisOnce (or any other jest.mock method) does not exist on the dependency, this is…
Philip Chmalts
  • 2,268
  • 2
  • 12
  • 11
206
votes
33 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
  • 7,322
  • 6
  • 36
  • 54
202
votes
4 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
  • 7,197
  • 9
  • 31
  • 46
201
votes
8 answers

How to mock imported named function in Jest when module is unmocked

I have the following module I'm trying to test in Jest: // myModule.js export function otherFn() { console.log('do something'); } export function testFn() { otherFn(); // do other things } As shown above, it exports some named functions…
Jon Rubins
  • 4,323
  • 9
  • 32
  • 51
196
votes
11 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
  • 13,662
  • 34
  • 99
  • 183
191
votes
4 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
  • 2,249
  • 3
  • 17
  • 18
189
votes
6 answers

Mock only one function from module but leave rest with original functionality

I only want to mock a single function (named export) from a module but leave the rest of the module functions intact. Using jest.mock('package-name') makes all exported functions mocks, which I don't want. I tried spreading the named exports back…
spencer.sm
  • 19,173
  • 10
  • 77
  • 88