Testing code is a mandatory operation with any major corporation or project, and every developer these days should be able and willing to write tests, from unit tests to integration tests to end-to-end tests.
And guess what: Testing code can even be fun! Well, sort of … if you have the right tools at your hands.
The following collection presents you with some of the most popular testing libraries for JavaScript available. Maybe you're already using some of them, but if not, it's definitely time to get started.
Cypress
Cypress is one of the best end-to-end testing solutions on the market right now. It can be used for anything that runs in a browser, and many developers just love testing with it, even if they don't like testing. The best thing is the user interface that pops up in your browser, making it really easy and enjoyable to test your code e2e.
Key features:
- Made specifically for developers and QA engineers
- Open-source project
- New architecture without Selenium
- Time-travel feature
- Great debuggability
- Real-time reloads
- Automatic waiting
Mocha
Mocha is one of the most popular frameworks when it comes to JavaScript testing. It's an independent open-source project and the most depended upon module on npm (which means many, many other packages depend on it).
Mocha is simple, flexible, and works both for Node.js and browsers. It takes away the hassle of testing asynchronous code.
Key features:
- Runs in the browser
- Simple async support
- Test-coverage reporting
- String-diff support
- Works with any assertion library
- Config-file support
Jasmine
Jasmine is another great testing framework. Just like Mocha, it was built for browsers and Node.js. Jasmine uses the concept of behaviour-driven development (BDD) and doesn't rely on any specific JavaScript framework.
Key features:
- Low overhead (no external dependencies in the core)
- Testing-ready out of the box
- Supports both browsers and Node.js
- Good documentation
Jest
Jest was created by Facebook (yeah, the same people who created React …) and is widely used for testing JavaScript code. The library has 30,000 stars on GitHub, and the repo is maintained actively. Jest is a comprehensive testing solution that usually works out of the box with most projects that use JavaScript.
Key features:
- Zero config needed on most projects
- Snapshot testing
- Isolated tests running on own processes
- Great API that's well documented
- Code-coverage reports integrated
- Easy mocking using the integrated custom resolver
Protractor
Protractor offers end-to-end testing for Angular applications. It's a Node.js program built on top of WebDriverJs that runs tests against applications in a real browser imitating real users.
Key features:
- Native events and browser-specific drivers
- Supports Angular-specific locator strategies
- Adds automatic wait and sleep steps
TestCafe
TestCafe is an end-to-end testing tool built with Node.js. It runs on Windows, macOS, and Linux and supports mobile, desktop, remote, and even cloud browsers. It's an open-source project and can be customized via plugins for reporting, integrating other tools, launching tests from IDEs, and more.
Key features:
- Tests can be written in JavaScript or TypeScript
- No manual timeouts — TestCafe waits for page reloads
- Code changes are immediately reflected
- TestCafe can identify JavaScript errors
- TestCafe Studio — a complete testing IDE available
Chai
Chai is a great BDD/test-driven development (TDD) assertion library for browsers and Node.js that can easily be paired with testing frameworks such as Mocha (which is a very common pair, by the way). Chai isn't a testing framework on its own but adds assertions like Node's built-in assert
to a testing setup.
Key features:
- Adds a lot of assertions
- Can be customized via plugins
- Works well with testing frameworks
- Open-source project
QUnit
QUnit is a powerful JavaScript testing framework for writing unit tests. It's used by jQuery, for example, but works with most JavaScript code on the client or server-side. A good use case for QUnit is regression testing, where tests are written when bugs appear in order to fix them afterwards and to keep an eye on the code that causes them.
Key features:
- Node.js support
- Regression testing
- Unit testing
- Around since jQuery but runs standalone
Conclusion
This list is far from complete. There definitely are other great tools, libraries, and frameworks around that can replace one of the examples from above.
Assertions libraries like Chai, for example, are often integrated into solutions like Cypress or Jest so you won't need a standalone solution anymore. Yet, Chai is still used in many projects, so it won't hurt to know a bit about it.