Giter Club home page Giter Club logo

xv's Introduction


xv

Node.js CI install size

A tiny (~80 lines of TypeScript) test runner focused on simplicity and speed

$ xv ./src
src/add.test.js: 0.103ms
src/sub.test.js: 0.064ms

Extracted from lowdb. Fastest test runner according to this benchmark.

Why

If you've used other test runners, you probably have spent a significant amount of time reading docs, configuring, maintaining and debugging them.

By being extremely simple, xv gets out of your way and lets you be productive faster. In fact, the whole project documentation fits in this page ;)

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 tests:

npm test                # run all test files in ./src
npx xv src/add.test.js  # run a single test file

Convention

By default, xv will look for files named: *.test.js, test.js, *.test.ts and test.ts

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": "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

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

exports.testAdd = function() {
  assert.equal(add(1, 2), 3)
}

Watch mode

xv doesn't have 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.

xv's People

Contributors

typicode avatar zetaraku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xv's Issues

Error handling in the pre-commit hook

Hello @typicode๐Ÿ‘‹

First of all, I appreciate your awesome work.

Let me get to the point, I have one problem that related to pre-commit hook.
That cannot stop commit when failed test at pre-commit.
This reason is because xv uses console.error for error output, I am assuming.

I would like to know a good solution to this problem, if any.

thank you.

ts-node `runTestFile` problem

Thanks for amazing library.

I'm testing v2, the runTestFile() uses pathToFileURL(file).toString()
which behave differently when running withts-node loader.

{
	"scripts": {
		"test": "xv --loader=ts-node/esm ./src"
	},
	"devDependencies": {
		"ts-node": "~10.9.0",
		"typescript": "~4.9.0",
		"xv": "~2.0.0"
	}
}
// ./src/test.ts
import assert from 'node:assert/strict';

export function foo(): void {
	assert.strictEqual(true, false)
}

I'm logging output of pathToFileURL(file).toString().

xv --loader=ts-node/esm ./src

Test wont run, if (typeof value === 'function') is skipped, there is only default export from pathToFileURL(file).toString().

src/test.ts
+ [Module: null prototype] { default: { foo: [Function: foo] } }

Compiled ts works.

tsc && xv ./lib
lib/test.js
+ [Module: null prototype] {
+  __esModule: true,
+  default: { foo: [Function: foo] },
+  foo: [Function: foo]
+ }
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

true !== false

About dynamic runtime exports in CommonJS

let testCases = [ /* ... */ ]

/* works */
exports.testAll = async () => {
  await Promise.all(testCases).map(async item => { /**/  })
}

/* runtime exports can't be recognized */
testCases.forEach(item => {
  exports[item.xxx] = async () => { /* ... */ }
})

I'm consider a PR but not sure which would be the best approach:

  1. ignore this
  2. add a nextTick and display an explicit warning
  3. add a nextTick and recognize that for the user

Is it possible to export run?

My scenario is like this: I need to run a mock server before the test starts. The port of this server will be returned randomly via a function, so I need to pass the port in as an environment variable when xv is run. This means I need to execute xv inside a file instead of the command line. So is it possible to export the run method to make this happen?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.