-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
83 lines (67 loc) · 2.11 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
var tapOut = require('tap-out')
var runParallel = require('run-parallel')
var execSpawn = require('execspawn')
var spawn = require('child_process').spawn
function TapWebpackPlugin (options) {
this.options = options || {}
}
TapWebpackPlugin.prototype.apply = function (compiler) {
compiler.plugin('after-emit', run.bind(null, this.options))
}
function run (options, compilation, callback) {
var entry = compilation.chunks.filter((c) => c.hasRuntime())
var files = entry.map((c) => c.files[0])
var assets = files.map((f) => compilation.assets[f])
var source = assets.map((a) => a.source()).join('\n')
var proc = spawn(process.execPath, { stdio: ['pipe', 'pipe', 'inherit'] })
proc.stdin.end(source, 'utf8')
return runParallel([
options.reporter ? report : parse,
exit
], callback)
function report (callback) {
var reporter = execSpawn(options.reporter,
{ stdio: ['pipe', 'inherit', 'inherit'] })
proc.stdout.pipe(reporter.stdin)
reporter.on('exit', exited)
function exited (code) {
if (code !== 0) addError('test reporter non-zero exit code')
return callback()
}
}
function parse (callback) {
proc.stdout.pipe(tapOut(parsed))
function parsed (err, results) {
if (err) {
addError('could not parse TAP output')
} else if (results.fail.length > 0) {
results.fail.map(getError).forEach(addError)
}
return callback()
function getError (f) {
return getMessage(results.tests[f.test - 1], f)
}
}
}
function exit (callback) {
proc.on('exit', exited)
function exited (code) {
if (code !== 0) addError('tests failed')
return callback()
}
}
function addError (message) {
compilation.errors.push(new Error(message))
}
}
function getMessage (test, fail) {
var name = test ? test.name + ' - ' + fail.name : fail.name
var message = 'failed test: ' + name
var error = fail.error
if (error && error.expected && error.actual) {
message += '\n Expected: ' + error.expected +
'\n Actual: ' + error.actual
}
return message
}
module.exports = TapWebpackPlugin