forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_spec.js
172 lines (123 loc) · 4.68 KB
/
errors_spec.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require('../spec_helper')
const style = require('ansi-styles')
const chalk = require('chalk')
const errors = require(`${root}lib/errors`)
const logger = require(`${root}lib/logger`)
const snapshot = require('snap-shot-it')
describe('lib/errors', () => {
beforeEach(() => {
return sinon.spy(console, 'log')
})
context('.log', () => {
it('uses red by default', () => {
const err = errors.get('NOT_LOGGED_IN')
const ret = errors.log(err)
expect(ret).to.be.undefined
const {
red,
} = style
expect(console.log).to.be.calledWithMatch(red.open)
expect(console.log).to.be.calledWithMatch(red.close)
})
it('can change the color', () => {
const err = errors.get('NOT_LOGGED_IN')
const ret = errors.log(err, 'yellow')
expect(ret).to.be.undefined
const {
yellow,
} = style
expect(console.log).to.be.calledWithMatch(yellow.open)
expect(console.log).to.be.calledWithMatch(yellow.close)
})
it('logs err.message', () => {
const err = errors.get('NO_PROJECT_ID', 'foo/bar/baz')
const ret = errors.log(err)
expect(ret).to.be.undefined
expect(console.log).to.be.calledWithMatch('foo/bar/baz')
})
it('logs err.details', () => {
const err = errors.get('PLUGINS_FUNCTION_ERROR', 'foo/bar/baz', 'details huh')
const ret = errors.log(err)
expect(ret).to.be.undefined
expect(console.log).to.be.calledWithMatch('foo/bar/baz')
expect(console.log).to.be.calledWithMatch('\n', 'details huh')
})
it('logs err.stack in development', () => {
process.env.CYPRESS_INTERNAL_ENV = 'development'
const err = new Error('foo')
const ret = errors.log(err)
expect(ret).to.eq(err)
expect(console.log).to.be.calledWith(chalk.red(err.stack))
})
})
context('.logException', () => {
it('calls logger.createException with unknown error', () => {
sinon.stub(logger, 'createException').resolves()
sinon.stub(process.env, 'CYPRESS_INTERNAL_ENV').value('production')
const err = new Error('foo')
return errors.logException(err)
.then(() => {
expect(console.log).to.be.calledWith(chalk.red(err.stack))
expect(logger.createException).to.be.calledWith(err)
})
})
it('does not call logger.createException when known error', () => {
sinon.stub(logger, 'createException').resolves()
sinon.stub(process.env, 'CYPRESS_INTERNAL_ENV').value('production')
const err = errors.get('NOT_LOGGED_IN')
return errors.logException(err)
.then(() => {
expect(console.log).not.to.be.calledWith(err.stack)
expect(logger.createException).not.to.be.called
})
})
it('does not call logger.createException when not in production env', () => {
sinon.stub(logger, 'createException').resolves()
sinon.stub(process.env, 'CYPRESS_INTERNAL_ENV').value('development')
const err = new Error('foo')
return errors.logException(err)
.then(() => {
expect(console.log).not.to.be.calledWith(err.stack)
expect(logger.createException).not.to.be.called
})
})
it('swallows creating exception errors', () => {
sinon.stub(logger, 'createException').rejects(new Error('foo'))
sinon.stub(process.env, 'CYPRESS_INTERNAL_ENV').value('production')
const err = errors.get('NOT_LOGGED_IN')
return errors.logException(err)
.then((ret) => {
expect(ret).to.be.undefined
})
})
})
context('.clone', () => {
it('converts err.message from ansi to html with span classes when html true', () => {
const err = new Error(`foo${chalk.blue('bar')}${chalk.yellow('baz')}`)
const obj = errors.clone(err, { html: true })
expect(obj.message).to.eq('foo<span class="ansi-blue-fg">bar</span><span class="ansi-yellow-fg">baz</span>')
})
it('does not convert err.message from ansi to html when no html option', () => {
const err = new Error(`foo${chalk.blue('bar')}${chalk.yellow('baz')}`)
const obj = errors.clone(err)
expect(obj.message).to.eq('foo\u001b[34mbar\u001b[39m\u001b[33mbaz\u001b[39m')
})
})
context('.displayFlags', () => {
it('returns string formatted from selected keys', () => {
const options = {
tags: 'nightly,staging',
name: 'my tests',
unused: 'some other value',
}
// we are only interested in showig tags and name values
// and prepending them with custom prefixes
const mapping = {
tags: '--tag',
name: '--name',
}
const text = errors.displayFlags(options, mapping)
return snapshot('tags and name only', text)
})
})
})