DEV Community

Cover image for Choosing JS test framework in 2023
Artur
Artur

Posted on

Choosing JS test framework in 2023

In my previous post I went through the process of bootstrapping the typescript project, choosing runtime, and thinking of important stuff.
I wrote a simple filter function to work with API responses in my newly created 2023 typescript project 😉 . My thoughts were:

To run the project, generate some data, and test the function live.
What if it doesn't work the first time(very likely)?
I thought about how many iterations it would take to fix it and how much time I would lose by manually running live tests every time. And then I remembered: unit tests are fast. Sure upfront cost of choosing a framework, and bootstrapping it will be slower than testing it manually a couple of times, but I will have it set up for later and will ensure that the function works after code changes, and in general cultivates better habits for myself(those are important).

So which framework for unit tests?

Choices are:

  • Jasmine
  • Jest
  • Mocha
  • AVA
  • Tape
  • builtin bun test runner(compatible with jest)

Jasmine and Jest

are IMHO test frameworks for angular and react.
Comes with the battery included, meaning almost no setup required. Both depend on globals, which, in my book, is bad if you can't refrain from sharing state between tests.

Mocha

is good, the most popular, very old(12+ years), and mature framework.
Very modular, depends on globals. Will have to install something for assertions.

Why AVA

in their own words:

  • Minimal and fast
  • Simple test syntax
  • Runs tests concurrently
  • Enforces writing atomic tests
  • No implicit globals
  • Includes TypeScript definitions
  • Magic assert
  • Isolated environment for each test file
  • Promise support
  • Async function support
  • Observable support
  • Enhanced assertion messages
  • Automatic parallel test runs in CI
  • TAP reporter

This list got me slightly giddy. 20k start on github, not bad. Also, The only one that supports Ukraine openly. 👍

Tape

Minimal, opinionated, no globals, no setup functions... honestly I am already biased towards AVA. I just can't stop thinking of AVA.

Builtin bun test runner

Bun ships with a fast, built-in, Jest-compatible test runner. Tests are executed with the Bun runtime. Jest, but it's bun, and not ts-node. Enough said.

Experiments

Trying AVA 💔

Yeah, so I couldn't make AVA use bun, and using ts-node is slow(3 sec for a single test, are you serious?), which goes against the reason why I chose bun in the first place. So sorry AVA 😢, I wanted this to work out badly, but life is life.

Going with bun test ❤️

Since it's compatible with jest, we can always fall back to jest. Fast, don't have anything else to add.

Conclusion

Wanted to go with AVA badly, even if based on subjective opinion.

But oh well, jest compatible bun test runner it is :) So basically jest it is. Just because it's easier, faster, and would work out of the box.

Next post coming soon...

Subscribe, react, and share, if you liked this.
Also, If you have an opinion, I would love to hear it down in the comments.

👋

Top comments (0)