-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (150 loc) · 4.43 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*!
* dush-tap-report <https://github.com/tunnckoCore/dush-tap-report>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var util = require('util')
var extend = require('extend-shallow')
var stackdata = require('stacktrace-metadata')
/**
* > A simple TAP report producing plugin for [dush][] or anything based on it.
* It returns a function that can be passed to dush's `.use` method. This plugin
* will also work for [minibase][] and [base][] mini frameworks for building robust apps.
*
* **Example**
*
* ```js
* const reporter = require('dush-tap-report')
* const dush = require('dush')
*
* const app = dush()
*
* // provide a fake stats object
* // so `finish` to work correctly
* app.use(reporter({
* stats: {
* count: 3,
* pass: 2,
* fail: 1
* }
* }))
*
* const item = {
* index: 1,
* title: 'some passing test'
* }
* const failing = {
* index: 2,
* title: 'failing test, sorry',
* reason: new Error('some sad error here')
* }
* const item2 = {
* index: 3,
* title: 'awesome test is okey'
* }
*
* app.emit('start', app)
* // => 'TAP version 13'
*
* app.emit('pass', app, item)
* // =>
* // # :) some passing test
* // ok 1 - some passing test
*
* app.emit('fail', app, failing)
* // =>
* // # :( failing test, sorry
* // not ok 2 - failing test, sorry
*
* app.emit('pass', app, item2)
* // =>
* // # :) awesome test is okey
* // ok 3 - awesome test is okey
*
* app.emit('finish', app)
* // =>
* // 1..3
* // # tests 3
* // # pass 2
* // # fail 1
* ```
*
* @param {Object} `options` optional options, merged with `app.options`,
* passed to [stacktrace-metadata][] and [find-callsite][]
* @param {Function} `options.writeLine` a logger function called on each line, default `console.log`
* @param {Boolean} `options.cleanStack` if `false` won't clean stack trace from node internals,
* [clean-stacktrace][]
* @param {Boolean} `options.shortStack` if `false` full stack traces, otherwise they are just four
* @param {Boolean} `options.showStack` if `false` the error.stack will be empty string
* @param {Boolean} `options.relativePaths` if `false` paths in stack traces will be absolute,
* [clean-stacktrace-relative-paths][]
* @param {Function} `options.mapper` called on each line of the stack with `(line, index)` signature
* @param {String} `options.cwd` current working directory, default `process.cwd()`
* @return {Function} a plugin function that should be
* passed to `.use` method of [minibase][], [base][] or [dush][]
* @api public
*/
module.exports = function tapReport (options) {
return function tapReport_ (app) {
app.options = extend({
writeLine: console.log
}, app.options, options)
var log = app.options.writeLine
app.once('start', function start (app) {
log('TAP version 13')
})
app.on('pass', function onPass (app, test) {
log('# :)', test.title)
log('ok', test.index, '-', test.title)
})
app.on('fail', function onFail (app, test) {
log('# :(', test.title)
log('not ok', test.index, '-', test.title)
var err = stackdata(test.reason, app.options)
log(' ---')
log(' name:', err.name)
if (hasOwn(err, 'message') && err.message.length) {
log(' message:', err.message)
}
if (hasOwn(err, 'expected')) {
log(' expected:', util.inspect(err.expected))
}
if (hasOwn(err, 'actual')) {
log(' actual:', util.inspect(err.actual))
}
/* istanbul ignore else */
if (hasOwn(err, 'at')) {
log(' at:', err.at)
} else {
app.options.showStack = true
}
if (app.options.showStack) {
log(' stack:')
err.stack.split('\n').slice(1).map(function (str) {
log(str)
})
}
log(' ...')
})
app.once('finish', function finish (app) {
var stats = app.stats || app.options.stats
log('')
log('1..' + stats.count)
log('# tests', stats.count)
log('# pass', stats.pass)
if (stats.fail) {
log('# fail', stats.fail)
log('')
} else {
log('')
log('# ok')
}
})
return app
}
}
function hasOwn (self, key) {
return Object.prototype.hasOwnProperty.call(self, key)
}