/javascript-testing-library

A highly opinionated testing library for JavaScript

Primary LanguageTypeScriptBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

javascript-testing-library

Build Status code style: prettier JavaScript Style Guide

A highly opinionated testing library for JavaScript

Install

npm install javascript-testing-library --save-dev

In your package.json

{
  "scripts": {
    "test": "javascript-testing-library 'lib/**/*.test.js'"
  }
}

Usage

// User.test.js
import { Suite } from 'javascript-testing-library'

import { User } from './User'

export const suite = Suite({
  name: 'User',
})
 
suite.addTest({
  name: 'can upsert a user with only a name',
  async test(t) {
    const user = User({
      name: 'Justin'
    })
    
    const actual = await user.upsert()

    t.equal({
      expected: {
        id: 1,
        name: 'Justin'
      },
      actual
    })
  },
  stubs: [
    {
      module: 'sequelize',
      method: 'upsert',
      returns(options) {
        return {
          id: 1,
          name: options.name,
        }
      }
    }
  ]
})

What it is

javascript-testing-library is meant to force good testing practices onto a JavaScript application.

Using this library will encourage you to write...

  • Fast tests, because all expensive input and output are disabled.
  • Focused tests, because only one assertion is allowed per-test.
  • Useful tests, because it does not allow you to assert against the results of typeof. You have to assert against an actual value.

Using this library will discourage you from writing...

  • Brittle tests, because it only allows you to stub external modules.
  • Flakey tests, because it disables unreliable inputs and outputs like HTTP requests and file system operations.

This library will not allow you to...

  • Skip a test.
  • Have more than one assertion per-test.
  • Use any assertion besides deep strict equality.
  • Mock or stub internal modules.
  • Make unmocked HTTP requests or file system operations.
  • Use a setup/teardown construct that shares state between tests.
  • Forget to restore a stubbed function, because they are automatically restored after each test.

Output

Error

Errors show a diff, including for objects an arrays. The stack trace will highlight the test file that the error happened in.

Success

Benchmark

Currently averaging ~1300 tests per second. Hoping to have this at ~3k+ tests per second.

4004 Tests Passed in 3.369 seconds

TODO

  • Pretty printed output
  • Automatically stub node http
  • Automatically stub node https
  • Automatically stub node http/2
  • Automatically stub node net
  • Automatically stub node dns
  • Automatically stub node tls
  • Automatically stub node child_process
  • Automatically stub node process
  • Basic CLI
  • Gracefully handle invalid test files
  • Throw an error if a stub is not used by a test
  • Don't allow a method to be stubbed twice in a test
  • Built-in watch mode
  • Some sort of multi-thread multi-process perf enhancement
  • Find and run related tests