/
timeout.test.js
69 lines (65 loc) · 2.19 KB
/
timeout.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
67
68
69
import { test } from 'node:test'
import { once } from 'node:events'
import { pathToFileURL } from 'node:url'
import { fork } from 'node:child_process'
import { tspl } from '@matteo.collina/tspl'
import { join } from 'desm'
const borp = join(import.meta.url, '..', 'borp.js')
const clock = join(import.meta.url, '..', 'test-utils', 'clock.js')
const forkOpts = {
cwd: join(import.meta.url, '..', 'fixtures', 'long'),
env: { NODE_OPTIONS: `--import=${pathToFileURL(clock)}` },
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
}
test('times out after 30s by default', async (t) => {
const { ok, equal } = tspl(t, { plan: 4 })
const borpProcess = fork(borp, forkOpts)
let stdout = ''
borpProcess.stdout.on('data', (data) => {
stdout += data
if (data.includes('test:waiting')) {
borpProcess.send(['tick', 30e3])
borpProcess.send(['uninstall'])
}
})
const [code] = await once(borpProcess, 'exit')
equal(code, 1)
ok(stdout.includes('test timed out after 30000ms'))
ok(stdout.includes('tests 1'))
ok(stdout.includes('cancelled 1'))
})
test('does not timeout when setting --no-timeout', async (t) => {
const { ok, equal } = tspl(t, { plan: 4 })
const borpProcess = fork(borp, ['--no-timeout'], forkOpts)
borpProcess.stderr.pipe(process.stderr)
let stdout = ''
borpProcess.stdout.on('data', (data) => {
stdout += data
if (data.includes('test:waiting')) {
borpProcess.send(['tick', 30e3])
borpProcess.send(['uninstall'])
}
})
const [code] = await once(borpProcess, 'exit')
equal(code, 0)
ok(stdout.includes('✔ this will take a long time'))
ok(stdout.includes('tests 1'))
ok(stdout.includes('pass 1'))
})
test('timeout is configurable', async (t) => {
const { ok, equal } = tspl(t, { plan: 4 })
const borpProcess = fork(borp, ['--timeout', '10000'], forkOpts)
let stdout = ''
borpProcess.stdout.on('data', (data) => {
stdout += data
if (data.includes('test:waiting')) {
borpProcess.send(['tick', 10e3])
borpProcess.send(['uninstall'])
}
})
const [code] = await once(borpProcess, 'exit')
equal(code, 1)
ok(stdout.includes('test timed out after 10000ms'))
ok(stdout.includes('tests 1'))
ok(stdout.includes('cancelled 1'))
})