forked from SchoolOfFreelancing/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_spec.coffee
More file actions
132 lines (93 loc) · 3.87 KB
/
errors_spec.coffee
File metadata and controls
132 lines (93 loc) · 3.87 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
require("../spec_helper")
style = require("ansi-styles")
chalk = require("chalk")
errors = require("#{root}lib/errors")
logger = require("#{root}lib/logger")
snapshot = require("snap-shot-it")
describe "lib/errors", ->
beforeEach ->
sinon.spy(console, "log")
context ".log", ->
it "uses red by default", ->
err = errors.get("NOT_LOGGED_IN")
ret = errors.log(err)
expect(ret).to.be.undefined
red = style.red
expect(console.log).to.be.calledWithMatch(red.open)
expect(console.log).to.be.calledWithMatch(red.close)
it "can change the color", ->
err = errors.get("NOT_LOGGED_IN")
ret = errors.log(err, "yellow")
expect(ret).to.be.undefined
yellow = style.yellow
expect(console.log).to.be.calledWithMatch(yellow.open)
expect(console.log).to.be.calledWithMatch(yellow.close)
it "logs err.message", ->
err = errors.get("NO_PROJECT_ID", "foo/bar/baz")
ret = errors.log(err)
expect(ret).to.be.undefined
expect(console.log).to.be.calledWithMatch("foo/bar/baz")
it "logs err.details", ->
err = errors.get("PLUGINS_FUNCTION_ERROR", "foo/bar/baz", "details huh")
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_ENV = "development"
err = new Error("foo")
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_ENV").value("production")
err = new Error("foo")
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_ENV").value("production")
err = errors.get("NOT_LOGGED_IN")
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_ENV").value("development")
err = new Error("foo")
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_ENV").value("production")
err = errors.get("NOT_LOGGED_IN")
errors.logException(err)
.then (ret) ->
expect(ret).to.be.undefined
context ".clone", ->
it "converts err.message from ansi to html with span classes", ->
err = new Error("foo" + chalk.blue("bar") + chalk.yellow("baz"))
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>')
context ".displayFlags", ->
it "returns string formatted from selected keys", ->
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
mapping = {
tags: "--tag",
name: "--name"
}
text = errors.displayFlags(options, mapping)
snapshot("tags and name only", text)