DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Reasons to Love Jest: The Test Framework

Reasons to Love Jest: The Test Framework

If you're looking for a new framework to test your JavaScript code, read on to see the benefits that Jest brings to the table.

Liran Tal user avatar by
Liran Tal
CORE ·
Apr. 23, 19 · Tutorial
Like (2)
Save
Tweet
Share
6.68K Views

Join the DZone community and get the full member experience.

Join For Free

We had Tape, Mocha, Ava, and now Jest. Let’s see what this is all about!

I enjoy writing tests, but Jest takes it to a whole new level. It’s like I get up in the morning and ask myself:

What new app am I going to build today? I need something to write some Jest tests for...

Jest Testing Framework

I love Jest for many reasons and this is the first of several posts where I’ll share why I really like it. So let’s get started.

Zero Configuration

Jest follows the #0CJS practices for Zero Configuration, where even though it is extendible with many configuration variables, it just works out of the box and you don’t need to configure anything special.

Think Webpack 4, Parcel, Create React App — this is a practice that many projects have been following recently so it shouldn’t be news and Jest embraces it whole heartedly.

What #0CJS boils down to:

  • One dependency — just install Jest.
  • Mocking is built in; no need to install test double libraries like Proxyquire, Sinon, Testdouble, or others.
  • Assertions are built in; no need to install Chai, Should.js, or others. Jest ships with a good basis of what it refers to as Matchers to assert expectations.
  • Code coverage? Built-in.

Fast Like Ava, Sane Like the Rest

Oh Ava, my love!

I jumped from Mocha to Ava.js a few years back and it has been an interesting ride. With Ava, it’s not about the tooling but rather the philosophy and test practices it promotes — no shared state between any tests.

This is, however, not an easy task to follow.

Let’s see a crude example . Say your app behaves differently depending on an environment variable like NODE_ENV (not a far fetched example) and you want to test it in different conditions:

JavaScript code

See the pitfall?

With Ava, the two test cases are executed concurrently, which will lead to a situation where, by the time the first test case enters the service promise, the second test case already ran and changed the global variable’s value to production.

Some of these pitfalls are not uncommon across test code—global variables, singletons, which are the basis for Node.js module system, or integration tests where managing state between test cases is part of a flow.

All of these are bad test patterns and should be avoided as Ava encourages, but we often find ourselves in these situations.

Extensible Framework

There’s a great talk about Jest as a Platform by Rogelio Guzman from ReactiveConf 2017 which I highly recommend you to watch.

But without diving into the internals of the Jest project and how the platform is built, I'd just like to give a quick overview.  

Jest’s matchers (assertions, such as expect().toBe(1)) are easily extendible and help make your code more readable and concise without requiring you to use any of the language constructs.

For example, while Jest matchers give you out of the box things like:

  • .toBeTruthy()
  • .toHaveBeenCalled()
  • .toBeGreaterThan(number)

With the jest-extended package installed you also get the following matchers:

Arrays:

  • .toBeArrayOfSize()
  • .toBeArray()
  • .toIncludeAllMembers([members])

Numbers or Objects

  • .toBeEven()
  • .toContainKey(key)

More ecosystem extensions at jest-extended

Codemods

Imagine you have an existing code base written in one test framework and you want to move to another framework. How would you do it?

Codemods are programs that help you automate work for transforming your code base, and can largely be categorized in the following levels of maturity:

  • Search and replace.
  • Apply regular expressions for smarter search and replace.
  • Apply Abstract Syntax Tree (AST) transformations from one language syntax to another.

Codemods aren't really specific to Jest but they make the job easy when you need to migrate existing projects.

I wrote an article that documented this process on a small project if you're about to venture into the same kind of process.

And of course the jest-codemods repoistory is a great resource that will get you going.

--

I'd be happy to hear about why you love Jest. There's a second part that I'll share soon  —  "Reasons to Love Jest: Developer Experience."

Stay tuned!

Testing Framework

Published at DZone with permission of Liran Tal. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Practical Example of Using CSS Layer
  • Keep Your Application Secrets Secret
  • 10 Most Popular Frameworks for Building RESTful APIs
  • A First Look at Neon

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: