1.1.1 • Published 1 year ago

test-a-bit v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

test-a-bit

Zero-dependency light weight testing & benchmarking tool for node-js.

BEWARE!!!

This package is not ready for production, but still can be fun to use.

Installation

npm i test-a-bit --save-dev

runner

Call runner(<test_scripts>, <options>)

Each test script running in a separate process. You can specify the timeout for each script.

/* run_test.js */

import { runner } from 'test-a-bit'

runner([
      { script: './tests/success.js' },
      { script: './tests/fail.js' },
      { script: './tests/timeout.js', timeout: 200 },
      { script: './tests/random.js', timeout: 200, silent: false },
    ],
    {
      silent: false, // log or not process output by default
      timeout: 1000, // default timeout 
    }
).then(results => console.log(results, 'bye'))

<tests_list>

List of objects with script and timeout.

[silent=false]

Set to true to suppress console output from console.log inside the tests.

results

Runner returns Promise with results map like this:

{
  '12ebd1-9daf62-594cd5': {
    uid: '12ebd1-9daf62-594cd5',
    name: 'sample fail test',
    note: 'oh, no!',
    timeout: 200,
    script: './tests/random.js',
    silent: false,
    result: 'fail',
    delta: 0.09140002727508545,
    delta_precision: 'milli',
    delta_precision_sym: 'ms',
    exit_code: 1
  }
}

If you are lazy enough - just use auto_runner to automatically run all scripts in the specific folder.

auto_runner('./tests/').then(results => console.log('bye'))

Actual Test

Each test is a separated file that calls the execute method once with the test function. To indicate test result - run the success or fail function.

/* tests/random-test.js */

import { execute } from 'test-a-bit'

execute('sample fail test', (success, fail, is_runner) => {
  const rnd = Math.random()

  if (!is_runner) console.log(`oh wow! rolled: rnd`)

  success > 0.5
      ? success('got lucky!')
      : fail('oh, no!')
})

Possible output:

[fail]     sample fail test >> oh, no! / Δ=0.07ms

execute(<name>, <callback>)

<name> - Is a string - will be passed to the output.

<callback> - Callback Function with your test inside.

Callback Arguments

Callback function has 3 arguments: result functions success & fail, and is_runner flag.

fail([note]) & success([note])

Call this to mark test passed or failed respectively.

note - string, will be passed to the output.

is_runner

Is this test was runner by a runner (along with other tests). Might be handy to decide if you need to spam the console.

That's it. Have a fun! :3