UNPKG

1.91 kBJavaScriptView Raw
1import tester from './lib/test';
2import tap from './lib/tap';
3import {combine, stream} from './lib/combinators';
4
5let flatten = true;
6const tests = [];
7const test = tester(t => tests.push(t));
8
9// Provide a root context for BSD style test suite
10const subTest = (test('Root', () => {})).test;
11test.test = (description, spec) => {
12 flatten = false; // Turn reporter into BSD style
13 return subTest(description, spec);
14};
15
16const start = async ({reporter = tap()} = {}) => {
17 let count = 0;
18 let failure = 0;
19 reporter({type: 'version', data: 13});
20
21 // Remove the irrelevant root title
22 await tests[0].next();
23
24 let outputStream = stream(combine(...tests));
25 outputStream = flatten ? outputStream
26 .filter(({type}) => type !== 'testAssert')
27 .map(item => Object.assign(item, {offset: 0})) :
28 outputStream;
29
30 const filterOutAtRootLevel = ['plan', 'time'];
31 outputStream = outputStream
32 .filter(item => item.offset > 0 || !filterOutAtRootLevel.includes(item.type))
33 .map(item => {
34 if (item.offset > 0 || (item.type !== 'assert' && item.type !== 'testAssert')) {
35 return item;
36 }
37
38 count++;
39 item.data.id = count;
40 failure += item.data.pass ? 0 : 1;
41 return item;
42 });
43
44 // One day with for await loops ... :) !
45 while (true) {
46 const {done, value} = await outputStream.next();
47
48 if (done === true) {
49 break;
50 }
51
52 reporter(value);
53
54 if (value.type === 'bailout') {
55 throw value.data; // Rethrow but with Nodejs we keep getting the deprecation warning (unhandled promise) and the process exists with 0 exit code...
56 }
57 }
58
59 reporter({type: 'plan', data: {start: 1, end: count}});
60 reporter({type: 'comment', data: failure > 0 ? `failed ${failure} of ${count} tests` : 'ok'});
61};
62
63// Auto bootstrap following async env vs sync env (browser vs node)
64if (typeof window === 'undefined') {
65 setTimeout(start, 0);
66} else {
67 window.addEventListener('load', start);
68}
69
70export default test;