-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (162 loc) · 5.35 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
177
178
179
const {DataStream} = require('scramjet');
const test = require("tape");
const tTest = (t) => {
return Object.assign(t, {
expect: (count) => {
t.expectCount = count;
},
done: () => {
if (t.expectCount > 0 && t.assertCount !== t.expectCount) {
t.fail(`Expected ${t.expectCount} assertions, but ${t.assertCount} were run.`);
}
t.end();
},
equals: t.equal
});
};
const _path = require('path');
const reporter = ({tests, name}) => {
const ok = !tests.find(({ok}) => !ok);
console.error(ok ? "✓" : "✗", name);
tests.forEach(
(args) => {
const {ok, operator, actual, expected, name, error} = args;
console.error(' ', ok ? "✓" : "✗", `${operator}(${name})`);
if (error) {
console.error(' !', error.message);
}
if (!ok && actual) {
console.error(' => actual:', actual, 'expected:', expected);
}
}
);
return {name, ok};
};
const flattenTests = ({tests, conf: {testOnly, ...conf} = {}, prefix = ''}) => {
return {
name: prefix,
tests: Object.keys(tests)
.reduce((acc, name) => {
if (typeof tests[name] === "function" && (!testOnly || name.startsWith("test"))) {
const test = tests[name];
acc.push({
name: `${prefix}/${name}`,
conf,
async exec(t) {
try {
await test(tTest(t));
} catch(e) {
t.fail(e.stack.replace(/\n /g, '\n '));
t.done();
}
}
});
return acc;
} else if (typeof tests[name] === "object") {
return acc.concat(flattenTests({tests: tests[name], conf, prefix: prefix + '/' + name}).tests);
}
return acc;
}, [])
};
};
const runTests = ({name, tests, testTimeout = 5000}) => {
const harness = test.createHarness();
let current = null;
const acc = new DataStream;
let to;
harness.createStream({objectMode: true})
.pipe(new DataStream)
.each(async (chunk) => {
switch (chunk.type) {
case "test":
current = Object.assign({}, chunk, {
tests: []
});
clearTimeout(to);
to = setTimeout(() => acc.raise(Object.assign(
new Error("Test timeouted or exited without ending")),
{chunk}
), testTimeout)
break;
case "assert":
if (!current) {
const err = new Error('Test assertions run after the test has completed');
err.assertion = chunk;
throw err;
}
current.tests.push(chunk);
break;
case "end": // eslint-disable-next-line
const last = current;
current = null;
clearTimeout(to);
return acc.whenWrote(last);
}
})
.on("end", () => {
acc.end()
})
.resume()
;
DataStream.fromArray(tests)
.map(async ({name, conf, exec}) => harness(name, conf, exec))
.catch(e => console.error("Error!", e && e.stack));
return acc
.map(reporter)
.toArray()
.then((result) => ({
name,
result,
ok: !result.find(({ok}) => !ok)
}));
};
/**
* Returns a transform stream that should be fed with file objects and runs tests.
*
* @param {Object} conf configuration for tape
* @returns {DataStream} stream of test results.
*/
module.exports = (stream, conf) => {
// cleanup environment before running tests.
delete require.cache["scramjet"];
delete require.cache["scramjet-core"];
const safeRequire = (path) => {
try {
return require(path);
} catch(e) {
return {
test_require: () => {
throw e;
}
};
}
};
return DataStream.from(stream)
.map(({path}) => ({
prefix: _path.basename(path).replace(/\.js$/, ''),
conf,
tests: safeRequire(path)
}))
.map(flattenTests)
.map(runTests)
.tap()
.until(
({name, ok}) => {
if (!ok) {
throw new Error(`✗ Unit test errors occurred in ${name}`);
}
return false;
}
);
};
/**
* Runs test on any {Readable} stream of file.
*
* @param {Readable} stream stream of file entries
* @param {Object} conf
*/
module.exports.from = async (stream, conf) => DataStream.from(stream)
.use(module.exports, conf)
.run();
module.exports.flattenTests = flattenTests;
module.exports.runTests = runTests;