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
9
votes
4 answers

How to test that the renderItem function returns a ?

I'm building my app with React Native and I do my unit tests with Jest and Enzyme. How can I test my 's renderItem() function? It returns a from the React-Native-Elements library. Let me give you the example code: import {…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
9
votes
1 answer

How to test a function is called in react jest?

I have my button inside a component, which calls a method deleteData on click. How do I test the deleteData method is called on the button click in jest?
9
votes
1 answer

Global functions in TypeScript for jest testing

I'm using create-react-app-typescript and want to create a function that will be available in every test file similar to jest's globals. Is it possible to write a function in the src/setupTest.ts file that will be available in every test file? I…
ssylviageo
  • 195
  • 1
  • 8
9
votes
2 answers

Mocking a function jest but jest calling original function

I have a function that returns true or false, lets call it myFunc myFunc (){ if(something){return true} else{return false} } that's what it does for sake of arg I then call it somewhere else if(myFunc()){ }else{ } when I log it out, it…
donut
  • 343
  • 2
  • 7
  • 15
9
votes
1 answer

How to properly close mongoose connection in jest test

I have the following error after running my tests with --detectOpenHandles parameter Jest has detected the following 1 open handle potentially keeping Jest from exiting: ● PROMISE 18 | 19 | mongoose.Promise = global.Promise; > 20 |…
Yuri Taratorkin
  • 607
  • 6
  • 19
9
votes
6 answers

VSCode debugger not working in Jest tests

I'm struggling to get the Visual Studio Code debugger working in with Jest tests. Here is my launch.json: { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Jest All", "program":…
Carl Rippon
  • 4,553
  • 8
  • 49
  • 64
9
votes
6 answers

How to solve TypeError: environment.teardown is not a function

I can't test an application which was created with create-react-app. All guides says that test is working by default, but when I try "yarn test", it requires install 'jest-cli' and after installation gives an error: TypeError: environment.teardown…
user8736389
9
votes
1 answer

How to time travel to end of animation using React Jest

I'm trying to test a component which change state after a TweenMax animation. Everything's fine on browser, but I cannot understand how to write a test for it. The problem is Jest doesn't wait for the animation to complete, therefore state doesn't…
a.barbieri
  • 2,398
  • 3
  • 30
  • 58
9
votes
1 answer

Jest - mock function in 'child_process' package

I'm writing unit tests and which to mock the 'exec' method in package 'child_process'. __mocks__/child_process.js const child_process = jest.genMockFromModule('child_process'); child_process.exec = jest.fn() module.exports = child_process; This is…
David Faizulaev
  • 4,651
  • 21
  • 74
  • 124
9
votes
1 answer

How to write Jest unit test for a Vue form component which uses a Vuex store?

I have a login form. When I fill out the login form with data and the login button is clicked: form data (username, password) is sent to the server and a response is returned If the form data is invalid, a message is displayed by the…
sakhunzai
  • 13,900
  • 23
  • 98
  • 159
9
votes
7 answers

How test a React Loadable component

I have this component: import React from 'react'; const UploadAsync = Loadable({ loader: () => import('components/Upload'), loading: () => }); const Component = () => { return } export default…
Albert Olivé Corbella
  • 4,061
  • 7
  • 48
  • 66
9
votes
2 answers

Specify window.location for each test file for Jest

I am upgrading to Jest 22 and I got some problem about mocking window.location. In past, this method is work fine but not work after upgraded. Object.defineProperty(window.location, 'href', { writable: true, value:…
Natsathorn
  • 1,530
  • 1
  • 12
  • 26
9
votes
1 answer

Mocking a function call inside a function in Jest

I have a function getBookingStateObject that calls another function getBookingStateButtons. In turn getBookingStateButtons calls two other functions linkButtons and sendEventButtons. I'm trying to write tests for the above scenario. I have the…
Sooraj
  • 9,717
  • 9
  • 64
  • 99
9
votes
4 answers

Jest + Enzyme: test Redux-form

My application has a lot of redux-form. I am using Jest and Enzyme for unit testing. However, I fail to test the redux-form. My component is a login form like: import { login } from './actions'; export class LoginForm extends React.Component
Joy
  • 9,430
  • 11
  • 44
  • 95
9
votes
1 answer

Jest Manual Mocks with React and Typescript: mock an ES6 class dependency

I am using React with Typescript and in a unit test I have to mock a module which is included inside the main file to which I am writing unit tests for (a container component). The file I am trying to test imports this module: import…
quirimmo
  • 9,800
  • 3
  • 30
  • 45