UNPKG

1.89 kBJavaScriptView Raw
1'use strict';
2
3const yaml = require('js-yaml');
4const extend = require('util')._extend;
5const EventEmitter = require('events').EventEmitter;
6const TapParser = require('tap-parser');
7const log = require('npmlog');
8
9module.exports = class TapConsumer extends EventEmitter {
10 constructor() {
11 super();
12
13 this.stream = new TapParser();
14 this.stream.on('assert', this.onTapAssert.bind(this));
15 this.stream.on('extra', this.onTapExtra.bind(this));
16
17 this.stream.on('complete', this.onTapEnd.bind(this));
18 this.stream.on('bailout', this.onTapError.bind(this));
19 }
20
21 onTapAssert(data) {
22 log.info(data);
23 if (data.skip) {
24 return;
25 }
26
27 if (data.id === undefined) {
28 return;
29 }
30
31 let test = {
32 passed: 0,
33 failed: 0,
34 total: 1,
35 id: data.id,
36 name: data.name ? data.name.trim() : '',
37 items: []
38 };
39
40 if (!data.ok) {
41 let stack;
42 if (data.diag) {
43 stack = data.diag.stack || data.diag.at;
44 }
45 if (stack) {
46 stack = yaml.dump(stack);
47 }
48 data = extend(data, data.diag);
49
50 this.latestItem = extend(data, {
51 passed: false,
52 stack: stack
53 });
54 test.items.push(this.latestItem);
55 test.failed++;
56 } else {
57 test.passed++;
58 }
59 this.emit('test-result', test);
60 }
61
62 onTapExtra(extra) {
63 if (!this.latestItem) {
64 return;
65 }
66
67 if (this.latestItem.stack) {
68 this.latestItem.stack += extra;
69 } else {
70 this.latestItem.stack = extra;
71 }
72 }
73
74 onTapError(reason) {
75 let test = {
76 failed: 1,
77 name: 'bailout',
78 items: [],
79 error: {
80 message: reason
81 }
82 };
83
84 this.stream.removeAllListeners();
85 this.emit('test-result', test);
86 this.emit('all-test-results');
87 }
88
89 onTapEnd() {
90 this.stream.removeAllListeners();
91 this.emit('all-test-results');
92 }
93};