Setting up and maintaining a test framework can sometimes be complex and time consuming. I've created xv
to be a low maintainance, easy to setup and learn test lib/CLI. Great for small and medium projects.
- Lightweight and fast
- No config, only one function
test()
, simple - Pure ESM package
- Used in lowdb and steno
Requires Node v14.13.1+
Install xv
:
npm install xv --save-dev
yarn add xv --dev
Create a test file src/add.test.js
and use Node's built-in assert
module.
If you need your tests to be compatible with Node 14 and 16:
import { strict as assert } from 'assert'
import { test } from 'xv'
await test('should add', () => {
assert.equal(1 + 2, 3)
})
If you're working with Node 16 only, you can simplify your code:
import { equal } from 'assert/strict'
import { test } from 'xv'
await test('should add', () => {
equal(1 + 2, 3)
})
In package.json
, edit test
script:
{
"scripts": {
"test": "xv 'src/**/*.test.js'"
}
}
npm test # Run all .test.js files in src/
node src/add.test.js # Run specific test file
That's all there is to know... for real :)
Note xv
is a pure ESM package, read this if you're getting ERR_REQUIRE_ESM
error.
For a limited time xv
is available to Sponsors only. Once the goal of 70 sponsors is reached (currently 55), I'll release it under MIT for everyone ๐
If you like this project and my work, please help me reach this goal by becoming a sponsor here. Thank you!
Note: if you're already sponsoring me via husky, feel free to use xv
in any type of project.
If you're using TypeScript, compile your .ts
and run xv
directly on compiled .js
files.
Assuming you have the following tsconfig.json
:
{
"compilerOptions": {
"outDir": "./lib",
"module": "ESNext"
}
}
Edit package.json
to exclude test files from being published andrun tsc
before xv
:
{
"files": [
"lib",
"!lib/**/*.test.*" // exclude test files
],
"scripts": {
"build": "rm -rf lib && tsc",
"test": "npm run build && xv 'lib/**/*.test.js'"
}
}