forked from syarul/webpack-tape-run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (60 loc) · 1.95 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
const runParallel = require('run-parallel')
const execSpawn = require('execspawn')
const tapeRun = require('tape-run')
const str = require('string-to-stream')
class WebpackTapeRun {
constructor (options) {
this.options = options || {}
}
apply (compiler) {
compiler.hooks.emit.tapAsync('WebpackTapeRun', run.bind(null, this.options))
function run (options, compilation, callback) {
const source = []
compilation.chunks
.forEach((chunk) => {
if (chunk.hasRuntime()) {
const files = []
chunk.files.forEach((filename) => {
files.push(compilation.assets[filename].source())
})
source.push(files.join('\n'))
}
})
const stream = str(source.join('\n'))
.pipe(tapeRun(options.tapeRun))
return runParallel([
options.reporter ? report : _default,
exit
], callback)
function report (callback) {
const reporter = execSpawn(options.reporter,
{ stdio: ['pipe', 'inherit', 'inherit'] })
stream.pipe(reporter.stdin)
reporter.on('exit', exited)
function exited (code) {
if (code !== 0) addError('test reporter non-zero exit code')
return callback()
}
}
function _default (callback) {
stream.pipe(process.stdout)
stream.on('results', results)
function results (code) {
if (!code.ok) addError('test reporter non-zero exit code')
return callback()
}
}
function exit (callback) {
stream.on('results', results)
function results (code) {
if (!code.ok) addError('tests failed')
return callback()
}
}
function addError (message) {
compilation.errors.push(new Error(message))
}
}
}
}
module.exports = WebpackTapeRun