-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
176 lines (139 loc) · 4.56 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
168
169
170
171
172
173
174
175
176
import { diffWords, diffJson } from 'diff';
import chalk from 'chalk';
import duplexer from 'duplexer';
import figures from 'figures';
import through2 from 'through2';
import Parser from 'tap-parser';
import prettyMs from 'pretty-ms';
import jsondiffpatch from 'jsondiffpatch';
const INDENT = ' ';
const FIG_TICK = figures.tick;
const FIG_CROSS = figures.cross;
const createReporter = () => {
const output = through2();
const p = new Parser();
const stream = duplexer(p, output);
const startedAt = Date.now();
const println = (input = '', indentLevel = 0) => {
let indent = '';
for (let i = 0; i < indentLevel; ++i) {
indent += INDENT;
}
input.split('\n').forEach(line => {
output.push(`${indent}${line}`);
output.push('\n');
});
};
const handleTest = name => {
println();
println(chalk.blue(name), 1);
};
const handleAssertSuccess = assert => {
const name = assert.name;
println(`${chalk.green(FIG_TICK)} ${chalk.dim(name)}`, 2)
};
const toString = (arg) => Object.prototype.toString.call(arg).slice(8, -1).toLowerCase()
const JSONize = (str) => {
return str
// wrap keys without quote with valid double quote
.replace(/([\$\w]+)\s*:/g, (_, $1) => '"'+$1+'":')
// replacing single quote wrapped ones to double quote
.replace(/'([^']+)'/g, (_, $1) => '"' + $1 + '"')
}
const handleAssertFailure = assert => {
const name = assert.name;
const writeDiff = ({ value, added, removed }) => {
let style = chalk.white;
if (added) style = chalk.green.inverse;
if (removed) style = chalk.red.inverse;
// only highlight values and not spaces before
return value.replace(/(^\s*)(.*)/g, (m, one, two) => one + style(two))
};
let {
at,
actual,
expected
} = assert.diag
let expected_type = toString(expected)
if (expected_type !== 'array' ) {
try {
// the assert event only returns strings which is broken so this
// handles converting strings into objects
if (expected.indexOf('{') > -1) {
actual = JSON.stringify(JSON.parse(JSONize(actual)), null, 2)
expected = JSON.stringify(JSON.parse(JSONize(expected)), null, 2)
}
} catch (e) {
try {
actual = JSON.stringify(eval(`(${actual})`), null, 2)
expected = JSON.stringify(eval(`(${expected})`), null, 2)
} catch (e) {
// do nothing because it wasn't a valid json object
}
}
expected_type = toString(expected)
}
println(`${chalk.red(FIG_CROSS)} ${chalk.red(name)} at ${chalk.magenta(at)}`, 2);
if (expected_type === 'object') {
const delta = jsondiffpatch.diff(actual[failed_test_number], expected[failed_test_number])
const output = jsondiffpatch.formatters.console.format(delta)
println(output, 4)
} else if (expected_type === 'array') {
const compared = diffJson(actual, expected)
.map(writeDiff)
.join('');
println(compared, 4);
} else if (expected === 'undefined' && actual === 'undefined') {
;
} else if (expected_type === 'string') {
const compared = diffWords(actual, expected)
.map(writeDiff)
.join('');
println(compared, 4);
} else {
println(
chalk.red.inverse(actual) + chalk.green.inverse(expected),
4
);
}
};
const handleComplete = result => {
const finishedAt = Date.now();
println();
println(
chalk.green(`passed: ${result.pass} `) +
chalk.red(`failed: ${result.fail || 0} `) +
chalk.white(`of ${result.count} tests `) +
chalk.dim(`(${prettyMs(finishedAt - startedAt)})`)
);
println();
if (result.ok) {
println(chalk.green(`All of ${result.count} tests passed!`));
} else {
println(chalk.red(`${result.fail || 0} of ${result.count} tests failed.`));
stream.isFailed = true;
}
println();
};
p.on('comment', (comment) => {
const trimmed = comment.replace('# ', '').trim();
if (/^tests\s+[0-9]+$/.test(trimmed)) return;
if (/^pass\s+[0-9]+$/.test(trimmed)) return;
if (/^fail\s+[0-9]+$/.test(trimmed)) return;
if (/^ok$/.test(trimmed)) return;
handleTest(trimmed);
});
p.on('assert', (assert) => {
if (assert.ok) return handleAssertSuccess(assert);
handleAssertFailure(assert);
});
p.on('complete', handleComplete);
p.on('child', (child) => {
;
});
p.on('extra', extra => {
println(chalk.yellow(`${extra}`.replace(/\n$/, '')), 4);
});
return stream;
};
export default createReporter;