-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathunit.test.js
More file actions
150 lines (121 loc) · 4.43 KB
/
unit.test.js
File metadata and controls
150 lines (121 loc) · 4.43 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
149
150
'use strict'
const test = require('brittle')
const tmp = require('test-tmp')
const path = require('path')
const fs = require('fs')
const worker = require('../lib/worker')
const createShutdownHandler = require('../lib/shutdown')
const fixtureWorker = path.join(__dirname, 'fixtures', 'workers', 'dummy.js')
async function setupDir (t) {
const dir = await tmp(t)
fs.mkdirSync(path.join(dir, 'config'))
fs.mkdirSync(path.join(dir, 'workers'))
fs.copyFileSync(fixtureWorker, path.join(dir, 'workers', 'dummy.js'))
return dir
}
function writeConfig (dir, filename, content) {
const filePath = path.join(dir, 'config', filename)
const data = filename.endsWith('.js') ? content : JSON.stringify(content)
fs.writeFileSync(filePath, data)
}
function run (dir, env) {
return worker({ wtype: 'dummy', env, serviceRoot: dir })
}
test('JSON config resolution', async (t) => {
await t.test('loads base JSON config if env is not provided', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'common.json', { key: 'value', nested: { a: 1 } })
writeConfig(dir, 'production.common.json', { key: 'env-specific' })
const wrk = run(dir)
t.alike(wrk.conf, { key: 'value', nested: { a: 1 } })
})
await t.test('loads env-specific JSON config over base others', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'common.json', { key: 'base' })
writeConfig(dir, 'production.common.json', { key: 'prod-env-specific' })
writeConfig(dir, 'development.common.json', { key: 'dev-env-specific' })
const wrk = run(dir, 'production')
t.is(wrk.conf.key, 'prod-env-specific')
})
await t.test('falls back to base JSON if env-specific JSON is missing', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'common.json', { key: 'base' })
const wrk = run(dir, 'production')
t.is(wrk.conf.key, 'base')
})
})
test('JS config resolution', async (t) => {
await t.test('loads base JS config when no JSON exists', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'common.js', "module.exports = { key: 'from-js' }")
const wrk = run(dir)
t.is(wrk.conf.key, 'from-js')
})
await t.test('loads env-specific JS config when no JSON exists and env is provided', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'development.common.js', "module.exports = { key: 'dev-env-js' }")
writeConfig(dir, 'production.common.js', "module.exports = { key: 'prod-js' }")
const wrk = run(dir, 'development')
t.is(wrk.conf.key, 'dev-env-js')
})
await t.test('falls back to base JS if env-specific JS is missing and no JSON exists', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'common.js', "module.exports = { key: 'base-js' }")
const wrk = run(dir, 'production')
t.is(wrk.conf.key, 'base-js')
})
})
test('config priority order', async (t) => {
await t.test('JSON config takes priority over JS config', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'common.json', { source: 'json' })
writeConfig(dir, 'common.js', "module.exports = { source: 'js' }")
const wrk = run(dir)
t.is(wrk.conf.source, 'json')
})
await t.test('env-specific JSON has highest priority', async (t) => {
const dir = await setupDir(t)
writeConfig(dir, 'production.common.json', { source: 'env-json' })
writeConfig(dir, 'common.json', { source: 'base-json' })
writeConfig(dir, 'production.common.js', "module.exports = { source: 'env-js' }")
writeConfig(dir, 'common.js', "module.exports = { source: 'base-js' }")
const wrk = run(dir, 'production')
t.is(wrk.conf.source, 'env-json')
})
})
test('shutdown handler', async (t) => {
await t.test('exits immediately when worker is not active', (t) => {
let exitCode = null
let stopCalled = false
const hnd = {
active: 0,
stop: () => {
stopCalled = true
}
}
const shutdown = createShutdownHandler(hnd, code => {
exitCode = code
}, () => {})
shutdown()
t.is(stopCalled, false)
t.is(exitCode, 0)
})
await t.test('stops active worker once before exiting', (t) => {
let exitCode = null
let stopCalls = 0
const hnd = {
active: 1,
stop: cb => {
stopCalls++
cb()
}
}
const shutdown = createShutdownHandler(hnd, code => {
exitCode = code
}, () => {})
shutdown()
shutdown()
t.is(stopCalls, 1)
t.is(exitCode, 0)
})
})