-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
107 lines (89 loc) · 2.27 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*!
* gruu <https://github.com/tunnckoCore/gruu>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
const dush = require('dush')
const utils = require('./lib/utils')
const assert = require('assert')
const getName = require('get-fn-name')
let reporter = require('./lib/reporter')
module.exports = function gruu (options) {
const { on, off, once, emit } = dush()
const app = {
on,
off,
once,
emit,
tests: [],
stats: {
pass: 0,
fail: 0,
count: 0,
anonymous: 0
},
options: Object.assign({
showStack: false,
serial: false,
settle: true,
args: assert
}, options),
use (plugin) {
return plugin(app) || app
},
add (title, fn) {
if (typeof title === 'function') {
fn = title
title = null
}
if (typeof fn !== 'function') {
throw new TypeError('.add: expect `fn` to be a function')
}
title = typeof title === 'string' ? title : getName(fn)
if (title === null) {
app.stats.anonymous++
}
app.stats.count++
const test = {
title,
index: app.tests.length + 1,
fn: utils.try(fn, app.options.args)
}
app.tests.push(test)
return app
},
run (options) {
app.options = Object.assign({}, app.options, options)
reporter = typeof app.options.reporter === 'function'
? app.options.reporter
: reporter
app.use(reporter)
app.emit('header', app)
function mapper (test) {
app.emit('beforeEach', app, test)
const promise = test.fn()
const notify = (result) => {
test = Object.assign({}, test, result)
app.emit('afterEach', app, test)
if (test.isRejected) {
app.stats.fail++
app.emit('fail', app, test)
} else {
app.stats.pass++
app.emit('pass', app, test)
}
if (test.isRejected && app.options.settle === false) {
throw test.reason
} else {
return test
}
}
return utils.reflect(promise, notify)
}
return utils.map(app, mapper)
}
}
return app
}