Quiz

What are some popular JavaScript testing frameworks?

Topics
JavaScriptTesting

TL;DR

Choose by test layer and runtime rather than popularity alone. Jest and Vitest are batteries-included choices for unit and integration tests; Mocha is a configurable runner often paired with separate assertion and mocking libraries; Jasmine is an all-in-one BDD framework. Playwright and Cypress automate real browsers for end-to-end and component tests. Node.js also ships a built-in node:test runner. DOM-testing libraries such as Testing Library complement a runner rather than replacing it.


Popular JavaScript testing frameworks

Jest

Jest is an OpenJS Foundation project originally developed at Facebook. It is a general JavaScript testing framework and remains common in React applications.

  • It comes with a built-in test runner, assertion library, and mocking support
  • It has a zero-configuration setup, making it easy to get started
  • It provides features like snapshot testing and code coverage out of the box

Example:

test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});

Mocha

Mocha is a flexible testing framework that can be used with various assertion libraries like Chai. It is known for its extensive configuration options and support for asynchronous testing.

  • It provides a simple and flexible API for writing tests
  • It supports both BDD (Behavior-Driven Development) and TDD (Test-Driven Development) styles
  • It can be easily integrated with other libraries and tools

Example:

const assert = require('assert');
describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.strictEqual([1, 2, 3].indexOf(4), -1);
});
});
});

Jasmine

Jasmine is a behavior-driven development framework that requires no additional libraries. It is known for its simplicity and ease of use.

  • It provides a clean and readable syntax for writing tests
  • It includes built-in assertion and mocking libraries
  • It supports asynchronous testing

Example:

describe('A suite', function () {
it('contains a spec with an expectation', function () {
expect(true).toBe(true);
});
});

Cypress

Cypress is an end-to-end testing framework that provides a great developer experience. It is known for its fast and reliable tests.

  • It provides a simple and intuitive API for writing tests
  • It includes built-in support for assertions, mocking, and stubbing
  • It provides real-time reloading and debugging capabilities

Example:

describe('My First Test', function () {
it('Visits the Kitchen Sink', function () {
cy.visit('https://example.cypress.io');
cy.contains('type').click();
cy.url().should('include', '/commands/actions');
});
});

Vitest

Vitest is a testing framework built on top of Vite, designed for fast unit testing with a Jest-compatible API. It is commonly used in projects already using Vite as their build tool.

  • It offers a Jest-compatible API, making migration from Jest straightforward
  • It provides fast test runs through Vite's transform pipeline and native ES modules support
  • It includes built-in support for TypeScript, watch mode, and code coverage

Example:

import { expect, test } from 'vitest';
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});

Playwright

Playwright is a browser automation and end-to-end testing framework with Chromium, Firefox, and WebKit support. It is a strong option when cross-browser behavior, multiple pages, downloads, permissions, or network control matter.

import { expect, test } from '@playwright/test';
test('shows the page title', async ({ page }) => {
await page.goto('https://example.com');
await expect(page).toHaveTitle(/Example Domain/);
});

Further reading

Exercises

Check your understanding
Beta
Check your understanding Exercise 1 of 2
Check your understanding Exercise 1 of 2

Which tool selection best matches a project that wants fast Vite-integrated unit tests and real Chromium/Firefox/WebKit end-to-end flows?