DEV Community

maxdevjs
maxdevjs

Posted on

What do you use to test JavaScript code (if)?

There are many alternatives, sometimes is not a free choice (work, consolidated projects, etc).

But what do you prefer to use and how do you choose it?

Personal and work examples are welcome.

Top comments (7)

Collapse
 
evanplaice profile image
Evan Plaice • Edited

Tape.js for util libs

It's super easy to setup and use. Doesn't require plugins so it can be run w/o install via npx.

Jest for DOM interactions

Same reasons I use Tape except whrn I need to test some fake DOM interactions.

testing-library for framework code

Lightweight, doesn't depend on framework-specific implementation details. Just add a test-id and use it to check on an element's state.

Cypress.io for E2E/Integration*

B/C Selenium is a non-deterministic piece of trash. Puppeteer would probably work as well.

Collapse
 
gypsydave5 profile image
David Wickes • Edited

Tape.js for util libs

Yes, yes and 1000 times yes. By far the most sensible option 90% of the time for unit testing some JS. Small surface area (barely any dependencies), runs quickly, sensible set of default assertions built in. Very much in the style of the Go testing library - you can extend it yourself by writing your own functions.

If you're looking for clean, easy to read tests that don't install half of npm to your computer, this is my go to lib.

Collapse
 
evanplaice profile image
Evan Plaice • Edited

Keep in mind, these choices are opinionated. IMO, the best testing framework is the one you notice the least.

Wherever possible, I stay away from FWs that are overloaded with a ton of convenience features or support trendy-but-not-useful approaches (ex BDD).

For example Mocha/Chai is usually the most popular for unit tests. I'm not a fan.

Protractor (Angular) and Enzyme (React) work perfectly well for framework-specific testing. But, I like that testing-library works directly on the DOM.

I like Tape b/c it doesn't use a plugin model for formatting the output. Instead, it outputs TAP (Test Again Protocol).

TAP is really basic and not too readable but since it's a standard there are plenty of tools built to consume it. For example piping tap into tap-spec will give you nice looking colorized test results.

Where TAP really makes a difference is CI/CD testing. On CI/CD speed trumps readability.

IMO, every testing lib should support TAP. If not as the default, then as an option.

Why the strong dislike of Selenium? I've used it before. Work that should've taken a week took almost a month b/c tests would randomly fail. To compensate, the tests became riddled with wait statements and other hacks.

Non-deterministic testing is a nightmare scenario for a Dev. It's really aggravating, time consuming, and makes you look bad even if you're doubled-down on effort trying to make it work.

Selenium is the anti-thesis of "the best testing FW is the one you notice least" in every way.

Collapse
 
maxdevjs profile image
maxdevjs

Here you addressed a good amount of my doubts and curiosities. Greatly appreciated :)

Collapse
 
maxdevjs profile image
maxdevjs

Wow, these are helpful suggestions. I am reading a few comparison/opinionated articles, but takes me a while to wrap my mind around this topic :)

Collapse
 
ben profile image
Ben Halpern

Lots of good discussion here, specifically around end-to-end testing....

At DEV we also use Jest for testing. It wasn't an overly controversial choice as it was just fairly popular, but other folks on the team might be able to speak more to that.

Collapse
 
maxdevjs profile image
maxdevjs

Yes, I did a search before to post. Actually lots :)