Japa Lean Node.js Test Runner

4.0.0 · active · verified Tue Apr 21

Japa is a lean and fast Node.js test runner designed for both testing applications and for building custom test runners. The current stable version is v10.4.0, with minor and patch releases occurring frequently—typically several times a quarter—and major versions released periodically as breaking changes are introduced. A key differentiator is its minimal core, offering faster boot times compared to alternatives like Mocha or Ava, primarily because it does not ship with its own CLI; tests are executed directly as standard Node.js scripts. Japa supports ES6 async/await syntax, ES modules, test groups with lifecycle hooks, regression tests, and offers an extensible assertion system often utilized via plugins. Its design philosophy emphasizes simplicity and provides the foundational components for highly customized testing environments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Japa tests, organizes them into groups with lifecycle hooks, configures the `@japa/assert` plugin for assertions, and shows how to programmatically run tests.

import test, { Runner } from 'japa'
import { assert } from '@japa/assert' // Japa typically uses plugins for assertions

// Configure Japa to discover test files and use the assertion plugin
test.configure({
  files: ['**/*.spec.ts'], // Adjust glob pattern to your test file naming
  plugins: [assert()]
})

// A simple function to be tested
function add(a: number, b: number): number {
  return a + b
}

// Basic test demonstrating the main `test` function
test('should correctly add two positive numbers', ({ assert }) => {
  assert.equal(add(2, 3), 5)
  assert.notEqual(add(1, 1), 3)
})

// Test group with lifecycle hooks
test.group('Complex Operations', (group) => {
  let result: number // State for the group

  group.beforeEach(() => {
    // Executed before each test in this group
    result = 0
    console.log('  [Group Hook] Resetting result to 0')
  })

  group.afterEach(() => {
    // Executed after each test in this group
    console.log(`  [Group Hook] Current result after test: ${result}`)
  })

  test('should initialize result to 0', ({ assert }) => {
    assert.equal(result, 0)
  })

  test('should allow adding values incrementally', ({ assert }) => {
    result = add(result, 10)
    assert.equal(result, 10)
    result = add(result, 5)
    assert.equal(result, 15)
  })
}).timeout(5000) // Example of a group-level timeout in milliseconds

// Manually run the tests. This is optional if using a global runner setup.
const runner = new Runner()
runner.run()
  .then(() => console.log('Tests completed.'))
  .catch((error) => console.error('Tests failed:', error))

view raw JSON →