Skip to content

Latest commit

 

History

History
138 lines (100 loc) · 2.92 KB

README.md

File metadata and controls

138 lines (100 loc) · 2.92 KB

Node.js CI install size


xv

xv is a blazingly fast test runner that works differently

Features

  • Simple - zero-config, no API to learn, out of the box ESM/CJS support
  • TypeScript - no complex setup, xv works with TypeScript + ESM
  • Blazingly fast - with almost zero abstractions, xv is one of the fastest test runners
  • Different - simply export test functions, doesn't hide console logs, raw errors, ...
  • Lightweight - 2kB for the runner code and no dependencies
  • Unix philosophy™ - do one thing well, xv is only a test runner

Used by lowdb (local JSON database), steno (fast file writer) and other awesome projects.

Install

npm install xv --save-dev

Usage

Create a test file and use Node's built-in assert module:

// src/add.test.js
import assert from 'node:assert/strict'
import add from './add.js'

// This is plain Node code, there's no xv API
export function testAdd() {
  assert.equal(add(1, 2), 3)
}

Edit package.json:

{
  "scripts": {
    "test": "xv src"
  }
}

Run all test files:

npm test

Run a single test file:

npx xv src/add.test.js 

Convention

When provided with a directory, xv will look for files named:

  • *.test.js
  • test.js
  • *.test.ts
  • test.ts

And run exported functions sequentially.

TypeScript

With TypeScript + ts-node

npm install ts-node --save-dev
{
  "scripts": {
    "test": "xv --loader=ts-node/esm src"
  }
}

With TypeScript only

Compile your .ts files using tsc and run xv on compiled .js files.

For example, assuming your compiled files are in lib/, edit package.json to run xv after tsc:

{
  "scripts": {
-   "test": "xv src"
+   "test": "tsc && xv lib"
  }
}

If you're publishing to npm, edit package.json to exclude compiled test files:

{
  "files": [
    "lib",
+   "!lib/**/*.test.js",
+   "!lib/**/test.js"
  ]
}

Common JS

xv can also test CJS code.

// src/add.test.js
const assert = require('assert').strict;
const add = require('./add')

// This is plain Node code, there's no xv API
exports.testAdd = function() {
  assert.equal(add(1, 2), 3)
}

Watch mode

xv doesn't integrate a watch mode. If the feature is needed, it's recommended to use tools like watchexec or chokidar-cli to re-run xv when there are changes.