1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var Base = require('./base');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | exports = module.exports = JSONReporter;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function JSONReporter (runner) {
|
22 | Base.call(this, runner);
|
23 |
|
24 | var self = this;
|
25 | var tests = [];
|
26 | var pending = [];
|
27 | var failures = [];
|
28 | var passes = [];
|
29 |
|
30 | runner.on('test end', function (test) {
|
31 | tests.push(test);
|
32 | });
|
33 |
|
34 | runner.on('pass', function (test) {
|
35 | passes.push(test);
|
36 | });
|
37 |
|
38 | runner.on('fail', function (test) {
|
39 | failures.push(test);
|
40 | });
|
41 |
|
42 | runner.on('pending', function (test) {
|
43 | pending.push(test);
|
44 | });
|
45 |
|
46 | runner.on('end', function () {
|
47 | var obj = {
|
48 | stats: self.stats,
|
49 | tests: tests.map(clean),
|
50 | pending: pending.map(clean),
|
51 | failures: failures.map(clean),
|
52 | passes: passes.map(clean)
|
53 | };
|
54 |
|
55 | runner.testResults = obj;
|
56 |
|
57 | process.stdout.write(JSON.stringify(obj, null, 2));
|
58 | });
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function clean (test) {
|
70 | return {
|
71 | title: test.title,
|
72 | fullTitle: test.fullTitle(),
|
73 | duration: test.duration,
|
74 | currentRetry: test.currentRetry(),
|
75 | err: errorJSON(test.err || {})
|
76 | };
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function errorJSON (err) {
|
87 | var res = {};
|
88 | Object.getOwnPropertyNames(err).forEach(function (key) {
|
89 | res[key] = err[key];
|
90 | }, err);
|
91 | return res;
|
92 | }
|