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
91
votes
6 answers

Are tests inside one file run in parallel in Jest?

Jest states in docs: "Jest virtualizes JavaScript environments and runs tests in parallel across worker processes." But what about multiple tests inside one file, do they run in parallel or this statement applies just to the test files? Can I assume…
Jarda
  • 1,188
  • 1
  • 7
  • 12
90
votes
7 answers

Jest URL.createObjectURL is not a function

I'm developping a reactJs application. I'm using jest to test my application. I want to test a function that download a blob. But unfortunately I receve this error: URL.createObjectURL is not a function my test function: describe('download', () =>…
Melchia
  • 22,578
  • 22
  • 103
  • 117
89
votes
3 answers

What is the difference between jest.fn() and jest.spyOn() methods in jest?

I am writing the Unit test cases for my react project and using jest and enzyme for writing test cases. I have read the jest documentation https://jestjs.io/docs/en/jest-object.html#jestspyonobject-methodname which explains about jest.spyOn()…
Pradhumn Sharma
  • 1,663
  • 2
  • 10
  • 19
88
votes
3 answers

Jest mock the same function twice with different arguments

I'm new to JEST and I'm currently testing a Javascript component that makes an API call in its onComponentDidMount. Depending on the return data of the ajax call (api call) my component either display a table or a simple text. My JEST test are…
Simon Leyendecker
  • 883
  • 1
  • 6
  • 6
87
votes
5 answers

How to assert data type with Jest

I'm using Jest to test my Node application. Is it possible for me to expect/assert a value to be a Date Object? expect(typeof result).toEqual(typeof Date()) Was my attempt, but naturally returns [Object]. And so this would pass too {}. Thanks!
Jack.c
  • 1,035
  • 1
  • 11
  • 14
87
votes
7 answers

Specify jest test files directory

I wanted only to test files located in a specific directory only. How do I specify that I want tests to run to files only on a specific directory and ignore others? I have installed jest via npm "jest": "^23.6.0", and specified my test command in…
Geoff
  • 6,277
  • 23
  • 87
  • 197
87
votes
4 answers

jest typescript property mock does not exist on type

When using jest.fn() to add a mock you can usually access the .mock property to access details such as calls, something similar to this: test('not working', () => { const foo = new Foo(); foo.addListener = jest.fn(); foo.func(); // will…
ext
  • 2,593
  • 4
  • 32
  • 45
86
votes
10 answers

Jest: Timer and Promise don't work well. (setTimeout and async function)

Any ideas on this code jest.useFakeTimers() it('simpleTimer', async () => { async function simpleTimer(callback) { await callback() // LINE-A without await here, test works as expected. setTimeout(() => { simpleTimer(callback) …
GutenYe
  • 3,227
  • 2
  • 25
  • 21
86
votes
11 answers

Importing images breaks jest test

In React components importing assets (ex, import logo from "../../../assets/img/logo.png) gives such error ({"Object.":function(module,exports,require,__dirname,__filename,global,jest){�PNG SyntaxError: Invalid or unexpected token …
Ani Alaverdyan
  • 1,527
  • 3
  • 14
  • 20
84
votes
9 answers

Mocking `document` in jest

I'm trying to write tests for my web components projects in jest. I already use babel with es2015 preset. I'm facing an issue while loading the js file. I have followed a piece of code where document object has a currentScript object. But in test…
thecodejack
  • 12,689
  • 10
  • 44
  • 59
83
votes
4 answers

How to determine if JEST is running the code or not?

I am creating a JS test on my react-native project. I'm specifically using firebase for react native, in which I would like to replace firebase instance with a mockfirebase instance if JS is running the code of my class. For example I have class…
Jojo Narte
  • 2,767
  • 2
  • 30
  • 52
83
votes
2 answers

Using jest in my react app, describe is not defined

I am new to jest and trying to figure out some basic stuff in my following code import * as actions from './IncrementalSearchActions'; describe('Incremental Search Actions', () => { it('Should create an incremental search action') }); The…
tmp dev
  • 8,043
  • 16
  • 53
  • 108
82
votes
11 answers

Intellisense for Jest not working in VS code

Edit: Run npm install @types/jest --save-dev To fix Just trying to type it() and the auto suggestion is isTag I've tried adding a jsconfig.json { "compilerOptions": { "target": "es6" }, "exclude": [ "node_modules", "assets" …
Barrard
  • 1,783
  • 1
  • 18
  • 25
82
votes
3 answers

Jest mock react context

I need some help understanding how one can test an application using React Context. Here's my sample setup. context.js import React from 'react' export const AppContext = React.createContext() App.js import React from 'react' import MyComponent…
artooras
  • 6,315
  • 9
  • 45
  • 78
80
votes
4 answers

Jest react testing: Check state after delay

I'm really confused trying to create test with the help of Jest documentation https://facebook.github.io/jest/docs/timer-mocks.html#content I'm trying to check a state when container mounts and then few seconds later, after I have manually set…
Jack M.
  • 1,831
  • 5
  • 24
  • 36