Migrating from Mocha to Jest

Running our test suite with Mocha took 12+ minutes. In CI with our beefy build machines we’re now able to run the entire Jest suite in 4…

Gary Borton
7 min readintermediate
--
View Original

Overview

The article discusses Airbnb's migration from Mocha to Jest for testing, highlighting significant performance improvements and reduced test flakiness. It emphasizes the minimal changes required for tests and the benefits of Jest's architecture in enhancing developer experience.

What You'll Learn

1

How to migrate a test suite from Mocha to Jest with minimal changes

2

Why Jest's parallelization improves test performance

3

How to reduce test flakiness in large test suites

4

When to leverage Jest's built-in coverage reporting

Prerequisites & Requirements

  • Familiarity with JavaScript testing frameworks
  • Basic understanding of Jest and Mocha(optional)

Key Questions Answered

What performance improvements were observed after migrating to Jest?
After migrating to Jest, the test suite that previously took over 12 minutes with Mocha now runs in just 4 minutes and 30 seconds on CI. Locally, the time reduced from approximately 45 minutes to 14.5 minutes, showcasing significant performance gains.
How did Jest help reduce test flakiness?
By running tests in isolation across multiple processes, Jest eliminated dependencies between tests, which previously caused flakiness. This change reduced the flake rate from around 12% to approximately 1%, significantly improving the reliability of the test suite.
What changes were required in the test files during the migration?
The migration to Jest required minimal changes to the existing test files at Airbnb. Most tests remained unchanged due to the similarity between the Jest and Mocha APIs, allowing for a straightforward transition with only minor adjustments.
What architectural benefits does Jest provide over Mocha?
Jest automatically handles test parallelization and coverage reporting, which previously required custom logic in Mocha. This simplification allowed Airbnb to streamline their testing architecture and reduce the overhead associated with managing test execution.

Key Statistics & Figures

Test suite runtime in CI
4 minutes 30 seconds
This is the time taken to run the entire Jest suite after migration, compared to over 12 minutes with Mocha.
Local test suite runtime
14.5 minutes
The time taken to run the test suite locally decreased from approximately 45 minutes with Mocha.
Flake rate reduction
from ~12% to ~1%
The flake rate was significantly reduced after migrating to Jest, improving test reliability.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Key Actionable Insights

1
Consider migrating to Jest if your current test suite is slow and flaky.
Jest's parallelization and isolation of tests can significantly enhance performance and reliability, as demonstrated by Airbnb's experience.
2
Standardize your test practices across teams to minimize migration challenges.
By ensuring consistent test styles and practices, teams can reduce the effort required during migrations and improve collaboration.
3
Utilize Jest's built-in features to manage code coverage effectively.
Jest simplifies coverage reporting, eliminating the need for complex setups, which can save time and reduce errors in your testing workflow.

Common Pitfalls

1
Relying on a global setup file can lead to performance issues in Jest.
Because Jest runs each test file in a clean environment, importing a global setup file repeatedly can slow down test execution. It's essential to minimize such imports to improve performance.