What is the difference between Jest and enzyme?
Asked Answered
V

3

49

I'm new to Unit testing. I want to test React project. As I started with React documentation which refers to enzyme as test utility which is kind of ambiguous to me.

What is the difference between Jest and enzyme?

  • Is enzyme assertion library or task runner?
  • Can I use karma with Jasmine?
  • What is the best way to test a react-redux project?
Vassell answered 6/3, 2017 at 2:26 Comment(1)
Jest is a test framework that has a runner and assertions. Enzyme is a test util library for manipulating and asserting React components, it works with Jest or Karma or Mocha or other test frameworks. Karma and Jasmine would be an alternative to Jest.Informant
W
36

Jest is a framework which includes a task runner, assertion library, and mocking support. This means it can execute different unit test cases, write its result in console or log files, create mocks, or verify all the assertions. In short, it will execute the test.

Enzyme, on other hand, is a library that provides a simple interface for writing unit tests. For this purpose, it wraps packages such as React TestUtils, JSDOM and CheerIO. React TestUtils has methods to render a React component into a document and to simulate an event. JSDOM is a JavaScript implementation of the DOM (Document object model). The DOM represents the tree structure of UI components. CheerIO implements a subset of jQuery core and is used to query the DOM.

Enzyme is not a test runner. It doesn't have its own assertion library. It just provides a collection of APIs for unit testing. That's why it could be integrated with Jest or any other task runner.

Yes, you can use karma with jasmine.

Welles answered 12/4, 2018 at 5:56 Comment(0)
U
8

Enzyme and Jest both have different responsibilities "Enzyme" is not a test runner meaning when u enter command npm test or npm run test it is jest responsibility that collects all the files ending with .test.js and run each test case and shows pass and fail results in your console like below

enter image description here

Enzyme provides you function to catch dom element and perform some action below are some of its function

shallow, mount, find, children, etc...

you can also use React Testing Library(another popular testing library) for such function to access dom element

EXTRA: developer often confuse among

  • jest
  • react-testing-library
  • Enzyme

Here Enzyme and react-testing-library are two similar things and alternatives to each other means you can use

  • enzyme with jest or
  • react-testing-library with jest
  • you can also use all three i.e react-testing-library+Enzyme with jest
  • but you can not use Enzyme and react-testing-library without jest or any other test runner eg: Mocha

where jest(testing-framework) will collect all .test.js files execute all the test cases and put the output in console with detail like how many pass and fail and react-testing-library or enzyme(both are testing library) will help you to perform event and accessing dom element

Uneventful answered 7/11, 2021 at 14:22 Comment(0)
J
4

Aaron's comment answers your first question.

Enzyme provides unit testing utility functions for React components, such as allowing shallow rendering. The Enzyme docs say you can use it with any test runner or assertion library - see http://airbnb.io/enzyme/.

Yes, you can use karma with jasmine.

As far as the best way to test a react-redux project, that's a separate question and I suggest you search Stack Overflow. :)

Jew answered 27/6, 2017 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.