-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-spawner.tests.ts
126 lines (99 loc) · 3.82 KB
/
process-spawner.tests.ts
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
import fs = require('fs')
import request = require('request')
import PATH = require('path')
import CHAI = require('chai')
const expect = CHAI.expect
import {ProcessSpawner} from './process-spawner'
describe('process-spawner', function() {
describe('constructor', function() {
it('should pass along env vars', function() {
process.env['SOME_ENV_VAR'] = 'some value'
let ps = new ProcessSpawner({
program: 'node',
args: ['some_program.js']
})
expect(ps.env['SOME_ENV_VAR']).to.equal('some value')
})
it('should override env vars from options.env', function() {
process.env['SOME_ENV_VAR'] = 'some value'
let ps = new ProcessSpawner({
program: 'node',
args: ['some_program.js'],
env: {SOME_ENV_VAR: 'a different value'}
})
expect(ps.options.env['SOME_ENV_VAR']).to.equal('a different value')
})
})
describe('start', function() {
it('should return a promise that resolves when the process is ready', function() {
let ps = new ProcessSpawner({
program: 'node',
args: ['test-finishes-immediately.js']
})
var ready_promise = ps.start()
return ready_promise
})
it('should run the process', function(done) {
let ps = new ProcessSpawner({
program: 'node',
args: ['test-simple-server-for-test.js']
})
var promises = ps.start()
promises.ready.then(() => {
request.get('http://localhost:7341', (error, response, body) => {
if (!error && response.statusCode == 200) {
expect(body).to.equal('Something from the server')
} else {
if (!error) {
error = new Error(`response.statusCode=${response.statusCode}`)
}
}
ps.stop().then(() => {
done(error)
})
})
})
})
describe('completion_promise', function() {
it('should resolve if the completion code is 0', function() {
let ps = new ProcessSpawner({
program: 'node',
args: ['test-finishes-immediately.js']
})
var promises = ps.start()
return promises.completed.then((exit_code) => {
expect(exit_code).to.equal(0)
})
})
it('should reject if the completion code is not 0', function() {
let ps = new ProcessSpawner({
program: 'node',
args: ['test-finishes-immediately.js', '2']
})
var promises = ps.start()
return promises.completed.then(
(exit_code) => {
throw new Error('expected rejection')
}, (error) => {
expect(error.exit_code).to.equal(2)
return
}
)
})
})
})
describe('stop', function() {
it('should return a promise that resolves when the process has stopped', function(done) {
let ps = new ProcessSpawner({
program: 'node',
args: ['test-simple-server-for-test.js']
})
var promises = ps.start()
promises.ready.then(() => {
ps.stop().then(() => {
done()
})
})
})
})
})