stdio.js
3.99 KB
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
var expect = require('expect.js');
var spawnChild = require('../util/spawn');
describe('Spooky provides a stdio transport', function () {
var context = {};
var hooks = require('../util/hooks');
beforeEach(hooks.before(context));
it('crashes if passed invalid JSON', function (done) {
var child = spawnChild();
// wait until ready
child.stdout.once('line', function (line) {
expect(line).to.contain('ready');
// expect child to exit non-zero
var exited = false;
child.on('exit', function (code) {
expect(code).not.to.be(0);
exited = true;
if (emitted) {
done();
}
});
// expect child to emit an error event
var emitted = false;
child.stdout.once('line', function (line) {
var message = JSON.parse(line);
expect(message.params[0]).to.be('error');
emitted = true;
if (exited) {
done();
}
});
child.stdin.write('not valid JSON\n');
});
});
it('allows chained suites', function (done) {
var spooky = context.spooky;
var steps = [];
spooky.on('test.step', function (step) {
steps.push(step);
});
spooky.on('test.done', function () {
expect(steps).to.eql([1, 2]);
done();
});
spooky.start();
spooky.then(function () {
this.emit('test.step', 1);
});
spooky.run(function () {
this.start();
this.then(function () {
this.emit('test.step', 2);
});
this.run(function () {
this.emit('test.done');
});
});
});
it('allows restarts after completion', function (done) {
var out = [];
function onConsole(line) {
if (line === 'Fourth') {
expect(out).to.contain('First');
expect(out).to.contain('Second');
expect(out).to.contain('Third');
context.spooky.removeListener('console', onConsole);
done();
return;
}
out.push(line);
}
context.spooky.on('console', onConsole);
context.spooky.start();
context.spooky.then(function () {
this.echo('First');
});
context.spooky.then(function () {
this.echo('Second');
});
context.spooky.run(function() {
this.emit('test.done');
});
context.spooky.on('test.done', function() {
context.spooky.start();
context.spooky.then(function () {
this.echo('Third');
});
context.spooky.then(function () {
this.echo('Fourth');
});
context.spooky.run();
});
});
it('allows restarts queued before completion', function (done) {
var out = [];
function onConsole(line) {
if (line === 'Fourth') {
expect(out).to.contain('First');
expect(out).to.contain('Second');
expect(out).to.contain('Third');
context.spooky.removeListener('console', onConsole);
done();
return;
}
out.push(line);
}
context.spooky.on('console', onConsole);
context.spooky.start();
context.spooky.then(function () {
this.echo('First');
});
context.spooky.then(function () {
this.echo('Second');
});
context.spooky.run(function() {});
context.spooky.start();
context.spooky.then(function () {
this.echo('Third');
});
context.spooky.then(function () {
this.echo('Fourth');
});
context.spooky.run();
});
});