Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Jest and enzyme?

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?
like image 335
Eslam Tahoon Avatar asked Mar 06 '17 02:03

Eslam Tahoon


People also ask

Is Enzyme the same as Jest?

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.

Which is better Jest or Enzyme?

Many people choose to use Jest and Enzyme together to test their React web applications. They use Jest as a test runner and assertion library, then use Enzyme to build the tests for their UI. This results in slimmer, cleaner testing code that's also easier to debug when a test breaks.

Can I use Jest without Enzyme?

Both Jest and Enzyme are specifically designed to test React applications, Jest can be used with any other Javascript app but Enzyme only works with React. Jest can be used without Enzyme to render components and test with snapshots, Enzyme simply adds additional functionality.


2 Answers

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.

like image 62
Kirti Chaturvedi Avatar answered Oct 09 '22 03:10

Kirti Chaturvedi


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. :)

like image 30
Patrick Finnigan Avatar answered Oct 09 '22 03:10

Patrick Finnigan