UNPKG

4.66 kBMarkdownView Raw
1# m.test
2
3[![travis](https://img.shields.io/travis/ivoputzer/m.test.svg?style=flat-square)](https://travis-ci.org/ivoputzer/m.test) [![npm-package-quality](http://npm.packagequality.com/shield/m.test.svg?style=flat-square&colorB=44CC11)](http://packagequality.com/#?package=m.test) [![standard-js](https://img.shields.io/badge/coding%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/) [![node-api](https://img.shields.io/badge/node-6%2B-blue.svg?style=flat-square)](https://nodejs.org/docs/v6.0.0/api) [![npm-version](https://img.shields.io/npm/v/m.test.svg?style=flat-square&colorB=007EC6)](https://www.npmjs.com/package/m.test) [![npm-license](https://img.shields.io/npm/l/m.test.svg?style=flat-square&colorB=007EC6)](https://spdx.org/licenses/MIT)
4
5**[m(icro)](https://github.com/ivoputzer/m.cro#readme)[test](https://github.com/ivoputzer/m.test)** is a lightweight test runner for [node.js](https://nodejs.org/) written in es6+ (~4kb).
6
7#### install
8
9install [m.test](https://github.com/ivoputzer/m.test) directly from [npm](https://www.npmjs.com) to project's [devDependencies](https://docs.npmjs.com/files/package.json#devdependencies).
10
11```sh
12npm install --save-dev m.test
13```
14
15#### usage
16
17test files are run by simply passing them to [node](https://nodejs.org). for a given `test/index.js` run the following command to execute the suite:
18
19```sh
20node test
21```
22
23run the following one to enable [node's debugger](https://nodejs.org/api/debugger.html):
24
25```sh
26node debug test
27```
28
29#### cli
30
31more utilities to run your suites are available through the cli. if no files are given they will be looked up from `./test` recursively.
32
33```sh
34m.test [options] [files]
35```
36
37when executing suites through the cli `m.test` will be assigned to `global.test` by design. the following line can be omitted:
38
39```javascript
40const {test} = require('m.test')
41```
42
43further instructions can be accessed via `--help` flag and [man-pages](https://github.com/ivoputzer/m.test/tree/master/man) by executing either `m.test --help` or `man m.test` within your shell.
44
45---
46
47#### basic usage
48
49```javascript
50const {ok} = require('assert')
51
52test('it just works!', function () {
53 ok(true)
54})
55```
56
57#### async usage
58
59```javascript
60const {ok} = require('assert')
61
62test('it works async too!', function (done) {
63 setTimeout(function () {
64 ok(true)
65 done()
66 }, 0)
67})
68
69test('done takes a error argument!', function (done) {
70 setTimeout(function (err = null) {
71 done(err)
72 }, 0)
73})
74```
75
76#### context usage
77
78```javascript
79test('can be used as a context', function () {
80 test('works!', function (done) {
81 done(null)
82 })
83 test('works!', function (done) {
84 done(null)
85 })
86})
87```
88
89#### alias usage
90
91```javascript
92const {test: context, test: describe, test: it} = require('m.test')
93
94context('given some context', function () {
95 describe('your subject', function () {
96 it('just works!', (done) => done(null))
97 })
98})
99```
100
101#### beforeEach afterEach usage
102
103```javascript
104test('description', function (done) {
105 done(null)
106})
107beforeEach(done => setup(done))
108afterEach(done => teardown(done))
109```
110
111it is important to call `beforeEach` and `afterEach` wrap functions after `test` functions themselves. when using wraps within nested suites consider their contextual binding.
112
113```javascript
114test('description 1', function () {
115 test('description 1.1', Function.prototype)
116 test('description 1.2', Function.prototype)
117 beforeEach(done => setup(done))
118 afterEach(done => teardown(done))
119})
120test('description 2', function () {
121 test('description 2.1', Function.prototype)
122 test('description 2.2', Function.prototype)
123})
124```
125_(in the example above hooks would be called for `1.1` e `1.2`)_
126
127---
128
129#### skip modifier
130
131```javascript
132test.skip('description', function () {
133 // this function will never be called
134})
135```
136
137the [skip](#skip-modifier) modifier comes with an optional `doSkip=true` parameter that enables/disables the skip behavior according to the expression:
138
139```javascript
140test.skip('description', function () {
141 // this test will be skipped when NODE_ENV=CI
142}, /CI/gi.test(process.env.NODE_ENV))
143```
144
145#### timeout modifier
146
147```javascript
148test.timeout('description', function () {
149 // this test will fail if it exceeds 200ms of execution
150}, 200)
151```
152
153the [timeout](#timeout-modifier) modifier comes with an optional `doTimeout=true` parameter that enables/disables the timeout behavior according to the expression:
154
155```javascript
156test.timeout('description', function () {
157 // this test will have a timeout when NODE_ENV=CI
158}, 200, /CI/g.test(process.env.NODE_ENV))
159```
160[view more](https://github.com/ivoputzer/m.test/tree/master/test)