@magic/test
simple yet powerful unit testing library
Last updated 18 days ago by jascha .
AGPL-3.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @magic/test 
SYNC missed versions from official npm registry.

@magic/test

declaratively test your ecmascript module files

no transpiling of either your codebase nor the tests.

incredibly fast.

html docs

NPM version Linux Build Status Windows Build Status Coverage Status Greenkeeper badge Known Vulnerabilities

getting started

be in a nodejs project.

npm i --save-dev @magic/test

mkdir test

create test/index.js

import yourTest from '../path/to/your/file.js'

export default [
  { fn: () => true, expect: true, info: 'true is true' },
  // note that the function will be called automagically
  { fn: yourTest, expect: true, info: 'hope this will work ;)'}
]
npm run scripts

edit package.json:

{
  "scripts": {
    "test": "t -p", // quick test, only failing tests log
    "coverage": "t", // get full test output and coverage reports
  }
}

repeated for easy copy pasting (without comments):
  "scripts": {
    "test": "t -p",
    "coverage": "t",
  }

run the test:

  npm test

example output: (failing tests will print, passing tests are silent)

###  Testing package: @magic/test

Ran 2 tests. Passed 2/2 100%

run coverage reports and get full test report including from passing tests:

  npm run coverage
data/fs driven test suite creation:
  • expectations for optimal test messages:
  • src and test directories have the same structure and files.
  • tests one src file per test file.
  • tests one function per suite
  • tests one feature per test
Filesystem based naming

the following directory structure:

./test/
  ./suite1.js
  ./suite2.js

has the same result as exporting the following from ./test/index.js

Data driven naming
import suite1 from './suite1'
import suite2 from './suite2'

export default {
  suite1,
  suite2,
}
Important

if test/index.js exists, no other files will be loaded.

if test/lib/index.js exists, no other files from that subdirectory will be loaded.

single test, literal value, function or promise
export default { fn: true, expect: true, info: 'expect true to be true' }

// expect: true is the default and can be omitted
export default { fn: true, info: 'expect true to be true' }

// if fn is a function expect is the returned value of the function
export default { fn: () => false, expect: false, info: 'expect true to be true' }

// if expect is a function the return value of the test get passed to it
export default { fn: false, expect: t => t === false, info: 'expect true to be true' }

// if fn is a promise the resolved value will be returned
export default { fn: new Promise(r => r(true)), expect: true, info: 'expect true to be true' }

// if expects is a promise it will resolve before being compared to the fn return value
export default { fn: true, expect: new Promise(r => r(true)), info: 'expect true to be true' }

// callback functions can be tested easily too:
import { promise } from '@magic/test'
const fnWithCallback = (err, arg, cb) => cb(err, arg)
export default { fn: promise(fnWithCallback(null, 'arg', (e, a) => a)), expect: 'arg' }
testing types

types can be compared using @magic/types

@magic/types is a full featured and thoroughly tested type library without dependencies.

it is exported from this library for convenience.

import { is } from '@magic/test'
export default [
  { fn: () => 'string',
    expect: is.string,
    info: 'test if a function returns a string'
  },
  {
    fn: () => 'string',
    expect: is.length.equal(6),
    info: 'test length of returned value'
  },
  // !!! Testing for deep equality. simple.
  {
    fn: () => [1, 2, 3],
    expect: is.deep.equal([1, 2, 3]),
    info: 'deep compare arrays/objects for equality',
  },
  {
    fn: () => { key: 1 },
    expect: is.deep.different({ value: 1 }),
    info: 'deep compare arrays/objects for difference',
  },
]
caveat:

if you want to test if a function is a function, you need to wrap the function

import { is } from '@magic/test'
const fnToTest = () => {}
export default {
  fn: () => fnToTest,
  expect: is.function,
  info: 'function is a function',
}
multiple tests

multiple tests can be created by exporting an array of single test objects.

export default {
  multipleTests: [
    { fn: () => true, expect: true, info: 'expect true to be true' },
    { fn: () => false, expect: false, info: 'expect false to be false' },
  ]
}
promises
import { promise, is } from '@magic/test'

export default [
  // kinda clumsy, but works. until you try handling errors.
  {
    fn: new Promise(cb => setTimeOut(() => cb(true), 2000)),
    expect: true,
    info: 'handle promises',
  },
  // better!
  {
    fn: promise(cb => setTimeOut(() => cb(null, true), 200)),
    expect: true,
    info: 'handle promises in a nicer way',
  },
  {
    fn: promise(cb => setTimeOut(() => cb(new Error('error')), 200)),
    expect: is.error,
    info: 'handle promise errors in a nice way',
  },
]
callback functions
import { promise, is } from '@magic/test'

const fnWithCallback = (err, arg, cb) => cb(err, arg)

export default [
  {
    fn: promise(cb => fnWithCallback(null, true, cb)),
    expect: true
    info: 'handle callback functions as promises',
  },
  {
    fn: promise(cb => fnWithCallback(new Error('oops'), true, cb)),
    expect: is.error,
    info: 'handle callback function error as promise',
  },
]
run functions before and/or after individual test
const after = () => {
  global.testing = 'Test has finished, cleanup.'
}

const before = () => {
  global.testing = false

  // if a function gets returned,
  // this function will be executed once the test finished.
  return after
}

export default [
  {
    fn: () => { global.testing = 'changed in test' },
    // if before returns a function, it will execute after the test.
    before,
    after,
    expect: () => global.testing === 'changed in test',
  },
run functions before and/or after a suite of tests
const afterAll = () => {
  // Test has finished, cleanup.'
  global.testing = undefined
}

const beforeAll = () => {
  global.testing = false

  // if a function gets returned,
  // this function will be executed once the test suite finished.
  return afterAll
}

export default [
  {
    fn: () => { global.testing = 'changed in test' },
    // if beforeAll returns a function, it will execute after the test suite.
    beforeAll,
    // this is optional and can be omitted if beforeall returns a function.
    // in this example, afterAll will trigger twice.
    afterAll,
    expect: () => global.testing === 'changed in test',
  },

Utility Belt

@magic/test exports some utility functions that make working with complex test workflows simpler.

curry

Currying can be used to split the arguments of a function into multiple nested functions. This helps if you have a function with complicated arguments that you just want to quickly shim.

import { curry } from '@magic/test'

const compare = (a, b) => a === b
const curried = curry(compare)
const shimmed = curried('shimmed_value')

export default {
  fn: shimmed('shimmed_value'),
  expect: true,
  info: 'expect will be called with a and b and a will equal b',
}
vals

exports some javascript types. more to come. will sometime in the future be the base of a fuzzer.

promises

Helper function to wrap nodejs callback functions and promises with ease. Handles the try/catch steps internally and returns a resolved or rejected promise.

import { promise, is } from '@magic/test'

export default [
  {
    fn: promise(cb => setTimeOut(() => cb(null, true), 200)),
    expect: true,
    info: 'handle promises in a nice way',
  },
  {
    fn: promise(cb => setTimeOut(() => cb(new Error('error')), 200)),
    expect: is.error,
    info: 'handle promise errors in a nice way',
  },
]
css

exports @magic/css which allows parsing and stringification of css-in-js objects.

tryCatch

allows to catch and test functions without bubbling the errors up into the runtime

import { is, tryCatch } from '@magic/test'
const throwing = () => throw new Error('oops')
const healthy = () => true

export default [
  {
    fn: tryCatch(throwing()),
    expect: is.error,
    info: 'function throws an error',
  },
  {
    fn: tryCatch(healthy()),
    expect: true,
    info: 'function does not throw'
  },
]

Usage

js api:

// test/index.js

import run from '@magic/test'

const tests = {
  lib: [
    { fn: () => true, expect: true, info: 'Expect true to be true' }
  ],
}

run(tests)

cli

package.json (recommended):

Add the magic/test bin scripts to package.json

{
  "scripts": {
    "test": "t -p",
    "coverage": "t",
  },
  "devDependencies": {
    "@magic/test": "github:magic/test"
  }
}

then use the npm run scripts

  npm test
  npm run coverage
Globally (not recommended):

you can of course install this library globally, but the recommendation is to add the dependency and scripts to the package.json file.

this both explains to everyone that your app has this dependencies and keeps your bash free of clutter

  npm i -g magic/test

  // run tests in production mode
  t -p

  // run tests in verbose mode
  t

This library tests itself, have a look at the tests

Checkout @magic/types and the other magic libraries for more test examples.

Changelog

0.1.0

use esmodules instead of commonjs.

0.1.1

rework of bin scripts and update dependencies to esmodules

0.1.2

cli now works on windows again (actually, this version is broken on all platforms.)

0.1.3

cli now works everywhere

0.1.4

npm run scripts of @magic/test itself can be run on windows.

0.1.5

use ecmascript version of @magic/deep

0.1.6

  • update this readme and html docs.
  • tests should always process.exit(1) if they errored.

0.1.7

  • readded calls npm run script
  • updated c8

0.1.8

update @magic/cli

0.1.9

  • test/beforeAll.mjs gets loaded separately if it exists and executed before all tests
  • test/afterAll.mjs gets loaded separately if it exists and executed after all tests
  • if the function exported from test/beforeAll.mjs returns another function, this returned function will also be executed after all tests
  • export hyperapp beta 18

0.1.10

node 12.4.0 does not use --experimental-json-modules flag. removed it in 12.4+.

0.1.11

  • update prettier, coveralls
  • add and export @magic/css to test css validity

0.1.12

update dependencies

0.1.13

windows support is back

0.1.14

windows support now supports index.mjs files that provide test structure

0.1.15

update dependencies

0.1.16

update @magic/cli for node 13 support.

0.1.17

add node 13 json support for coverage reports.

0.1.18

  • update dependencies
  • require node 12.13.0

0.1.19

update dependencies

0.1.20

update broken dependencies

0.1.21

update @magic/cli to allow default args

0.1.22

update dependencies

0.1.23

update @magic dependencies to use npm packages instead of github

0.1.24

  • update @magic/css
  • update c8

0.1.25

  • currying now throws errors instead of returning them.
  • update @magic/css
  • update @magic/types which now uses @magic/deep for is.deep.eq and is.deep.diff

0.1.26

remove commonjs support. node 13+ required. awesome.

0.1.27

remove prettier from deps

0.1.28

  • package: engineStrict: true
  • update cli: missing @magic/cases dependency

0.1.29

help text can show up when --help is used

0.1.30

export @magic/fs

0.1.31

update dependencies

0.1.32

  • tests now work on windows \o/
  • uncaught errors will cause tests to fail with process.exit(1)

0.1.33

update exported dependencies

0.1.34

fix: c8 needs "report" command now

0.1.35

  • fix: c8 errored if coverage dir did not exist
  • update dependencies

0.1.36

c8: --exclude, --include and --all get applied correctly.

0.1.37

fix: arguments for both node and c8 tests work. broken in 0.1.36

0.1.38

update dependencies, minimist sec issue.

0.1.39

update coveralls, fix minimist issue above.

0.1.40

update dependencies

0.1.41

update dependencies

0.1.42

update dependencies

0.1.43

update dependencies

0.1.44

update dependencies

0.1.45

security fix: update dependencies, yargs-parser.

0.1.46

update @magic/css

0.1.47

update c8, yargs-parser

0.1.48

bump required node version to 14.2.0 update dependencies

0.1.49 - unreleased

...

Current Tags

  • 0.1.48                                ...           latest (18 days ago)

62 Versions

  • 0.1.48                                ...           18 days ago
  • 0.1.47                                ...           18 days ago
  • 0.1.46                                ...           22 days ago
  • 0.1.45                                ...           24 days ago
  • 0.1.44                                ...           a month ago
  • 0.1.43                                ...           a month ago
  • 0.1.42                                ...           a month ago
  • 0.1.41                                ...           2 months ago
  • 0.1.40                                ...           2 months ago
  • 0.1.39                                ...           2 months ago
  • 0.1.38                                ...           2 months ago
  • 0.1.37                                ...           3 months ago
  • 0.1.36                                ...           3 months ago
  • 0.1.35                                ...           3 months ago
  • 0.1.34                                ...           3 months ago
  • 0.1.33                                ...           4 months ago
  • 0.1.32                                ...           4 months ago
  • 0.1.31                                ...           4 months ago
  • 0.1.30                                ...           4 months ago
  • 0.1.29                                ...           5 months ago
  • 0.1.28                                ...           5 months ago
  • 0.1.27                                ...           5 months ago
  • 0.1.26                                ...           5 months ago
  • 0.1.25                                ...           5 months ago
  • 0.1.24                                ...           5 months ago
  • 0.1.23                                ...           6 months ago
  • 0.1.22                                ...           6 months ago
  • 0.1.21                                ...           6 months ago
  • 0.1.20                                ...           6 months ago
  • 0.1.19                                ...           6 months ago
  • 0.1.18                                ...           6 months ago
  • 0.1.17                                ...           6 months ago
  • 0.1.16                                ...           6 months ago
  • 0.1.15                                ...           6 months ago
  • 0.1.14                                ...           9 months ago
  • 0.1.13                                ...           9 months ago
  • 0.1.12                                ...           10 months ago
  • 0.1.11                                ...           a year ago
  • 0.1.10                                ...           a year ago
  • 0.1.9                                ...           a year ago
  • 0.1.8                                ...           a year ago
  • 0.1.7                                ...           a year ago
  • 0.1.6                                ...           a year ago
  • 0.1.5                                ...           a year ago
  • 0.1.4                                ...           a year ago
  • 0.1.3                                ...           a year ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago
  • 0.1.0-cjs                                ...           a year ago
  • 0.0.12                                ...           a year ago
  • 0.0.11                                ...           a year ago
  • 0.0.10                                ...           a year ago
  • 0.0.9                                ...           a year ago
  • 0.0.8                                ...           a year ago
  • 0.0.7                                ...           a year ago
  • 0.0.6                                ...           a year ago
  • 0.0.5                                ...           a year ago
  • 0.0.4                                ...           a year ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (2)
Downloads
Today 0
This Week 0
This Month 328
Last Day 0
Last Week 62
Last Month 276
Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |