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
63
votes
5 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
  • 6,298
  • 4
  • 32
  • 53
63
votes
3 answers

How to mock React component methods with jest and enzyme

I have a react component(this is simplified in order to demonstrate the issue): class MyComponent extends Component { handleNameInput = (value) => { this.searchDish(value); }; searchDish = (value) => { //Do something …
Miha Šušteršič
  • 8,483
  • 17
  • 69
  • 133
62
votes
3 answers

Jest spyOn function called

I'm trying to write a simple test for a simple React component, and I want to use Jest to confirm that a function has been called when I simulate a click with enzyme. According to the Jest docs, I should be able to use spyOn to do this:…
Max Millington
  • 3,842
  • 4
  • 20
  • 32
61
votes
3 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
  • 7,538
  • 4
  • 26
  • 41
61
votes
6 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
  • 9,206
  • 4
  • 43
  • 74
59
votes
8 answers

How to resolve "Cannot use import statement outside a module" in jest

I have a React application (not using Create React App) built using TypeScript, Jest, Webpack, and Babel. When trying to run "yarn jest", I get the following error: I have tried removing all packages and re-adding them. It does not resolve this. …
Logan Shoemaker
  • 787
  • 1
  • 5
  • 10
59
votes
5 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,154
  • 2
  • 10
  • 20
59
votes
5 answers

How to mock dependencies for unit tests with ES6 Modules

I'm trying to fiddle with Ecmascript 6 modules using webpack + traceur to transpile to ES5 CommonJS, but I'm having trouble successfully unit testing them. I tried using Jest + traceur preprocessor, but the automocking and dependency names seem to…
Evan Layman
  • 3,575
  • 9
  • 26
  • 47
58
votes
1 answer

How can I test part of object using Jest?

I would like to test that time is parsed correctly and I am only interested in checking some of the properties and not the entire object. In this case hour and minutes. I tried using expect(object).toContain(value) but as you can see in the snippet…
mancristiana
  • 1,014
  • 1
  • 11
  • 22
58
votes
8 answers

`regeneratorRuntime` is not defined when running Jest test

The title pretty much explains what I'm facing. I'm trying to test a React component that has some state, and I attempt to provide my store to the component in order to get what it needs. When I run the test of the component using Jest, I get the…
rafafan2010
  • 1,059
  • 1
  • 11
  • 21
57
votes
4 answers

Jest setup "SyntaxError: Unexpected token export"

I'm implementing tests into an existing project that currently has no tests. My tests are failing to compile node_modules/ imports. /Users/me/myproject/node_modules/lodash-es/lodash.js:10 export { default as add } from…
Cory Robinson
  • 3,050
  • 4
  • 23
  • 43
56
votes
10 answers

command not found: jest

I have a test file like so: (I am using create-react-app) import React from 'react'; import ReactDOM from 'react-dom'; import App from './components/Calculator'; import { getAction, getResult } from './actions/' import {shallow} from…
Alessandro
  • 4,405
  • 15
  • 55
  • 108
56
votes
5 answers

Jest Enzyme test a React component that returns null in render method

I have a component that returns null in render under certain conditions: render() { if (this.props.isHidden) { return null; } return
test
; } I want to check if the component is null when isHidden is true with jest and…
klugjo
  • 13,748
  • 4
  • 39
  • 59
56
votes
12 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
  • 596
  • 1
  • 4
  • 8
55
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
  • 4,579
  • 9
  • 28
  • 66