0.202003.1 • Published 5 years ago

@hp4k1h5/t v0.202003.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

t(tests)

minimal assert based testing framework

output

instructions

t can be imported into your own test running script, or run as a command-line script @t which recursively crawls all directories containing 'test' in their name, starting from the cwd.

as import

npm i @hp4k1h5/t

const { t } = require('@hp4k1h5/t')
// t(tests, only, skip)
t() // recursively tests all files in all dirs with 'test' in their name
t([], 'unit') // recursively tests whose name matches `/unit/`
t([], null, 'integration') // recursively tests all tests whose name does not match `/integration/`

every file ending in .js inside *test* must export a default Array of test Objects. test Objects may contain the following keys:

keyname   expected value

- name      String
  - objects with only name a key will be treated as decorator/info text
- fn        function
  - a block of tests
- before    function
  - values returned by before on the callback parameter in `after`
  - before: () => {val}
- after     function
  - after: b => console.log(b.val)
// example.js
// run `node example.js`
const { t } = require('@hp4k1h5/t')
const assert = require('assert')

const tests = [
  { name: 'test name' },
  {
    before: async () => {
      const interval = setInterval(() => 1, 1000)
      return { interval }
    },
  },
  {
    name: 'test passes ?',
    fn: () => assert.ok(true, 'not ok'),
  },
  {
    name: `test fails ?,
    fn: () => assert.ok(false),
  },
  {
    after: b => {
      clearInteveral(b.interval)
    },
  },
]
t(tests)

as cli script

requirements:

  • @babel/babel-node
  • @babel/babel-core
  1. npm i -g @babel/babel-node @babel/babel-core

  2. link cd @hp4k1h5/t && npm link

  3. run @t from a project directory containing files like the example below

it will look for all files, like the test file example below , that export a default Array of javascript Objects with name and fn key(s)

command line arguments

flag -O :only - takes a space separated list of js regex strings

@t -O 'just tests that match these words'

@t -O 'test.* like? .*this$'

flag -s :skip - takes a space separated list of js regex strings

@t -s 'long_.+ .*int.*'

only

only

skip

skip

test file example

import assert from 'assert'

import { t, testAll } from '../lib'

export default [
  {
    name: `test_t.js\n\t- this test guarantees basic package functionality\n\t- please see README for more information `,
  },

  {
    name: 'test ok ?',
    fn: async () => {
      assert.ok('ok', 'not ok')
    },
  },

  {
    name: 'test fails ?',
    fn: async () => {
      const f = () => assert.ok(false)
      assert.throws(f)
    },
  },

  {
    name: `  *** imports ***`,
  },

  {
    name: `t exists in lib?`,
    fn: async () => {
      await assert.doesNotReject(import('../lib'))
      assert.deepStrictEqual(await import('../lib'), { t, testAll })
    },
  },

  {
    name: `lib and src export same things ?\n\t **test may not pass while developing**\n\t run 'npm run prepublish' \n\t before double checking that your code hasn't introduced breaking changes`,
    fn: async () => {
      assert.equal(
        await import('../lib').toString(),
        await import('../src').toString(),
      )
    },
  },

  {
    name: 't prints to tty ? __recursive',
    fn: async () => {
      // capture stdout
      let output = ''
      const origWrite = process.stdout.write.bind(process.stdout)
      process.stdout.write = (chunk, encoding, callback) => {
        if (typeof chunk === 'string') {
          output += chunk
        }
      }

      // run t
      await t([], '', '__recursive')

      // reset original stdout.write
      process.stdout.write = origWrite

      // t prints()
      assert.ok(output.length)
    },
  },
]
0.202003.0

5 years ago

0.202003.1

5 years ago

0.20200309.0

5 years ago

0.20200307.0

5 years ago

0.20200307.1

5 years ago

0.20200306.0

5 years ago

0.20200305.6

5 years ago

0.20200305.4

5 years ago

0.20200305.5

5 years ago

0.20200305.3

5 years ago

0.20200305.2

5 years ago

2020.3.5

5 years ago

0.20200305.1

5 years ago

2020.4.1

5 years ago