1 | var defined = require('defined');
|
2 | var createDefaultStream = require('./lib/default_stream');
|
3 | var Test = require('./lib/test');
|
4 | var createResult = require('./lib/results');
|
5 | var through = require('through');
|
6 |
|
7 | var canEmitExit = typeof process !== 'undefined' && process
|
8 | && typeof process.on === 'function' && process.browser !== true
|
9 | ;
|
10 | var canExit = typeof process !== 'undefined' && process
|
11 | && typeof process.exit === 'function'
|
12 | ;
|
13 |
|
14 | exports = module.exports = (function () {
|
15 | var harness;
|
16 | var lazyLoad = function () {
|
17 | return getHarness().apply(this, arguments);
|
18 | };
|
19 |
|
20 | lazyLoad.only = function () {
|
21 | return getHarness().only.apply(this, arguments);
|
22 | };
|
23 |
|
24 | lazyLoad.createStream = function (opts) {
|
25 | if (!opts) opts = {};
|
26 | if (!harness) {
|
27 | var output = through();
|
28 | getHarness({ stream: output, objectMode: opts.objectMode });
|
29 | return output;
|
30 | }
|
31 | return harness.createStream(opts);
|
32 | };
|
33 |
|
34 | lazyLoad.onFinish = function () {
|
35 | return getHarness().onFinish.apply(this, arguments);
|
36 | };
|
37 |
|
38 | lazyLoad.onFailure = function () {
|
39 | return getHarness().onFailure.apply(this, arguments);
|
40 | };
|
41 |
|
42 | lazyLoad.getHarness = getHarness;
|
43 |
|
44 | return lazyLoad;
|
45 |
|
46 | function getHarness(opts) {
|
47 | if (!opts) opts = {};
|
48 | opts.autoclose = !canEmitExit;
|
49 | if (!harness) harness = createExitHarness(opts);
|
50 | return harness;
|
51 | }
|
52 | })();
|
53 |
|
54 | function createExitHarness(conf) {
|
55 | if (!conf) conf = {};
|
56 | var harness = createHarness({
|
57 | autoclose: defined(conf.autoclose, false)
|
58 | });
|
59 |
|
60 | var stream = harness.createStream({ objectMode: conf.objectMode });
|
61 | var es = stream.pipe(conf.stream || createDefaultStream());
|
62 | if (canEmitExit) {
|
63 | es.on('error', function (err) { harness._exitCode = 1; });
|
64 | }
|
65 |
|
66 | var ended = false;
|
67 | stream.on('end', function () { ended = true; });
|
68 |
|
69 | if (conf.exit === false) return harness;
|
70 | if (!canEmitExit || !canExit) return harness;
|
71 |
|
72 | process.on('exit', function (code) {
|
73 |
|
74 | if (code !== 0) {
|
75 | return;
|
76 | }
|
77 |
|
78 | if (!ended) {
|
79 | var only = harness._results._only;
|
80 | for (var i = 0; i < harness._tests.length; i++) {
|
81 | var t = harness._tests[i];
|
82 | if (only && t !== only) continue;
|
83 | t._exit();
|
84 | }
|
85 | }
|
86 | harness.close();
|
87 | process.exit(code || harness._exitCode);
|
88 | });
|
89 |
|
90 | return harness;
|
91 | }
|
92 |
|
93 | exports.createHarness = createHarness;
|
94 | exports.Test = Test;
|
95 | exports.test = exports;
|
96 | exports.test.skip = Test.skip;
|
97 |
|
98 | function createHarness(conf_) {
|
99 | if (!conf_) conf_ = {};
|
100 | var results = createResult();
|
101 | if (conf_.autoclose !== false) {
|
102 | results.once('done', function () { results.close(); });
|
103 | }
|
104 |
|
105 | var test = function (name, conf, cb) {
|
106 | var t = new Test(name, conf, cb);
|
107 | test._tests.push(t);
|
108 |
|
109 | (function inspectCode(st) {
|
110 | st.on('test', function sub(st_) {
|
111 | inspectCode(st_);
|
112 | });
|
113 | st.on('result', function (r) {
|
114 | if (!r.todo && !r.ok && typeof r !== 'string') test._exitCode = 1;
|
115 | });
|
116 | })(t);
|
117 |
|
118 | results.push(t);
|
119 | return t;
|
120 | };
|
121 | test._results = results;
|
122 |
|
123 | test._tests = [];
|
124 |
|
125 | test.createStream = function (opts) {
|
126 | return results.createStream(opts);
|
127 | };
|
128 |
|
129 | test.onFinish = function (cb) {
|
130 | results.on('done', cb);
|
131 | };
|
132 |
|
133 | test.onFailure = function (cb) {
|
134 | results.on('fail', cb);
|
135 | };
|
136 |
|
137 | var only = false;
|
138 | test.only = function () {
|
139 | if (only) throw new Error('there can only be one only test');
|
140 | only = true;
|
141 | var t = test.apply(null, arguments);
|
142 | results.only(t);
|
143 | return t;
|
144 | };
|
145 | test._exitCode = 0;
|
146 |
|
147 | test.close = function () { results.close(); };
|
148 |
|
149 | return test;
|
150 | }
|