-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
66 lines (63 loc) · 2.23 KB
/
index.test.js
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
const { spawn, exec, emit } = require('child_process')
const wfb = require('.')
const {
ERROR_SERVICE_REQUIRED,
ERROR_SERVICE_MUST_BE_STRING,
ERROR_MATCHERS_MUST_NOT_BE_EMPTY,
} = require('./constants')
jest.mock('child_process')
describe('wait-for-blah', () => {
const SERVICE_NAME = 'myservice'
beforeEach(() => jest.clearAllMocks())
it('is a function', () => {
expect(typeof wfb).toBe('function')
})
it('requires a service', () => {
expect(wfb()).rejects.toThrow(ERROR_SERVICE_REQUIRED)
})
describe('runner', () => {
it('runs a service using docker-compose', async () => {
const service = wfb(SERVICE_NAME)
expect(exec).toHaveBeenCalledWith(
`docker-compose run ${SERVICE_NAME}`,
expect.any(Function),
)
await expect(service).resolves.toBe(undefined)
})
})
describe('waiter', () => {
it('blows up if service is not a string', () => {
expect(wfb({ config: 'object' }, 'matcher')).rejects.toThrow(
ERROR_SERVICE_MUST_BE_STRING,
)
})
it('blows up if matchers are empty', () => {
expect(wfb(SERVICE_NAME, [])).rejects.toThrow(
ERROR_MATCHERS_MUST_NOT_BE_EMPTY,
)
expect(wfb(SERVICE_NAME, ['', ''])).rejects.toThrow(
ERROR_MATCHERS_MUST_NOT_BE_EMPTY,
)
})
it('runs a service using docker-compose and string', async () => {
const service = wfb(SERVICE_NAME, 'matcher')
expect(spawn).toHaveBeenCalledWith('docker-compose', ['up', SERVICE_NAME])
emit('line with "matcher"')
await expect(service).resolves.toBeDefined()
})
it('runs a service using docker-compose and regexp', async () => {
const service = wfb(SERVICE_NAME, /lolcats\d+ever/)
expect(spawn).toHaveBeenCalledWith('docker-compose', ['up', SERVICE_NAME])
emit('line with "lolcats4ever"')
await expect(service).resolves.toBeDefined()
})
it('waits for a complex mix of matchers', async () => {
const service = wfb(SERVICE_NAME, ['simple', 'simon', /lolcats\d+ever/])
expect(spawn).toHaveBeenCalledWith('docker-compose', ['up', SERVICE_NAME])
emit('line with "lolcats4ever"')
emit('simple')
emit('simon')
await expect(service).resolves.toBeDefined()
})
})
})