-
Notifications
You must be signed in to change notification settings - Fork 67
/
handle.test.ts
105 lines (93 loc) · 2.72 KB
/
handle.test.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
import {expect, fancy} from 'fancy-test'
import * as fs from 'fs-extra'
import * as path from 'path'
import * as process from 'process'
import {CLIError, config, ExitError} from '../../src/errors'
import {handle} from '../../src/errors/handle'
import {exit as exitErrorThrower} from '../../src/errors'
const errlog = path.join(__dirname, '../tmp/mytest/error.log')
const x = process.platform === 'win32' ? '»' : '›'
const originalExit = process.exit
const originalExitCode = process.exitCode
describe('handle', () => {
beforeEach(() => {
(process as any).exitCode = undefined;
(process as any).exit = (code: any) => {
(process as any).exitCode = code
}
})
afterEach(() => {
(process as any).exit = originalExit;
(process as any).exitCode = originalExitCode
})
// fancy
// .stderr()
// .finally(() => delete process.exitCode)
// .it('displays an error from root handle module', ctx => {
// handle(new Error('x'))
// expect(ctx.stderr).to.contain('Error: x')
// expect(process.exitCode).to.equal(1)
// })
// fancy
// .stderr()
// .finally(() => delete process.exitCode)
// .it('shows an unhandled error', ctx => {
// handle(new Error('x'))
// expect(ctx.stderr).to.contain('Error: x')
// expect(process.exitCode).to.equal(1)
// })
// fancy
// .stderr()
// .finally(() => delete process.exitCode)
// .it('handles a badly formed error object', () => {
// handle({status: 400} as any)
// expect(process.exitCode).to.equal(1)
// })
// fancy
// .stderr()
// .finally(() => delete process.exitCode)
// .it('shows a cli error', ctx => {
// handle(new CLIError('x'))
// expect(ctx.stderr).to.equal(` ${x} Error: x\n`)
// expect(process.exitCode).to.equal(2)
// })
fancy
.stdout()
.stderr()
.it('hides an exit error', ctx => {
handle(new ExitError(0))
expect(ctx.stdout).to.equal('')
expect(ctx.stderr).to.equal('')
expect(process.exitCode).to.equal(0)
})
fancy
.stderr()
.do(() => {
config.errlog = errlog
})
.finally(() => {
config.errlog = undefined
})
.it('logs when errlog is set', async ctx => {
handle(new CLIError('uh oh!'))
expect(ctx.stderr).to.equal(` ${x} Error: uh oh!\n`)
await config.errorLogger!.flush()
expect(fs.readFileSync(errlog, 'utf8')).to.contain('Error: uh oh!')
expect(process.exitCode).to.equal(2)
})
describe('exit', () => {
fancy
.stderr()
.stdout()
.it('exits without displaying anything', ctx => {
try {
exitErrorThrower(9000)
} catch (error: any) {
handle(error)
}
expect(ctx.stdout).to.equal('')
expect(ctx.stderr).to.equal('')
expect(process.exitCode).to.be.equal(9000)
})
})
})