6
6
*/
7
7
8
8
import { afterEach , beforeEach , describe , it } from 'mocha' ;
9
- import { default as chai , expect } from 'chai' ;
9
+ import chai , { expect } from 'chai' ;
10
10
import { formatWarning } from '../assertValidName' ;
11
+ import dedent from '../../jsutils/dedent' ;
11
12
12
13
/* eslint-disable no-console */
13
14
14
- /**
15
- * Helper for dedenting indented template literals. This helps us to
16
- * keep the tests pretty.
17
- */
18
- function dedent ( string ) {
19
- // Get lines, discarding empty leading and trailing lines.
20
- const lines = string . replace ( / ^ [ \t ] * \n | \n [ \t ] * $ / g, '' ) . split ( '\n' ) ;
21
-
22
- // Find smallest indent.
23
- const indent = lines . reduce ( ( currentMinimum , line ) => {
24
- const whitespace = line . match ( / ^ + / ) ;
25
- return Math . min ( whitespace ? whitespace [ 0 ] . length : 0 , currentMinimum ) ;
26
- } , Infinity ) ;
27
-
28
- // Remove indent from each line.
29
- return lines . map ( line => line . slice ( indent ) ) . join ( '\n' ) ;
30
- }
31
-
32
15
/**
33
16
* Convenience method for creating an Error object with a defined stack.
34
17
*/
@@ -101,27 +84,25 @@ describe('assertValidName()', () => {
101
84
102
85
describe ( 'formatWarning()' , ( ) => {
103
86
it ( 'formats given a Chrome-style stack property' , ( ) => {
104
- const chromeStack = dedent ( `
87
+ const chromeStack = dedent `
105
88
Error: foo
106
89
at z (<anonymous>:1:21)
107
90
at y (<anonymous>:1:15)
108
91
at x (<anonymous>:1:15)
109
- at <anonymous>:1:6
110
- ` ) ;
92
+ at <anonymous>:1:6` ;
111
93
const error = createErrorObject ( 'foo' , chromeStack ) ;
112
94
expect ( formatWarning ( error ) ) . to . equal (
113
- dedent ( `
95
+ dedent `
114
96
foo
115
97
at z (<anonymous>:1:21)
116
98
at y (<anonymous>:1:15)
117
99
at x (<anonymous>:1:15)
118
- at <anonymous>:1:6
119
- ` ) ,
100
+ at <anonymous>:1:6` ,
120
101
) ;
121
102
} ) ;
122
103
123
104
it ( 'formats given a Node-style stack property' , ( ) => {
124
- const nodeStack = dedent ( `
105
+ const nodeStack = dedent `
125
106
Error: foo
126
107
at z (repl:1:29)
127
108
at y (repl:1:23)
@@ -132,11 +113,10 @@ describe('formatWarning()', () => {
132
113
at bound (domain.js:280:14)
133
114
at REPLServer.runBound [as eval] (domain.js:293:12)
134
115
at REPLServer.onLine (repl.js:537:10)
135
- at emitOne (events.js:101:20)
136
- ` ) ;
116
+ at emitOne (events.js:101:20)` ;
137
117
const error = createErrorObject ( 'foo' , nodeStack ) ;
138
118
expect ( formatWarning ( error ) ) . to . equal (
139
- dedent ( `
119
+ dedent `
140
120
foo
141
121
at z (repl:1:29)
142
122
at y (repl:1:23)
@@ -147,44 +127,40 @@ describe('formatWarning()', () => {
147
127
at bound (domain.js:280:14)
148
128
at REPLServer.runBound [as eval] (domain.js:293:12)
149
129
at REPLServer.onLine (repl.js:537:10)
150
- at emitOne (events.js:101:20)
151
- ` ) ,
130
+ at emitOne (events.js:101:20)` ,
152
131
) ;
153
132
} ) ;
154
133
155
134
it ( 'formats given a Firefox-style stack property' , ( ) => {
156
- const firefoxStack = dedent ( `
135
+ const firefoxStack = dedent `
157
136
z@debugger eval code:1:20
158
137
y@debugger eval code:1:14
159
138
x@debugger eval code:1:14
160
- @debugger eval code:1:5
161
- ` ) ;
139
+ @debugger eval code:1:5` ;
162
140
const error = createErrorObject ( 'foo' , firefoxStack ) ;
163
141
expect ( formatWarning ( error ) ) . to . equal (
164
- dedent ( `
142
+ dedent `
165
143
foo
166
144
z@debugger eval code:1:20
167
145
y@debugger eval code:1:14
168
146
x@debugger eval code:1:14
169
- @debugger eval code:1:5
170
- ` ) ,
147
+ @debugger eval code:1:5` ,
171
148
) ;
172
149
} ) ;
173
150
174
151
it ( 'formats given a Safari-style stack property' , ( ) => {
175
- const safariStack = dedent ( `
152
+ const safariStack = dedent `
176
153
z
177
154
y
178
155
x
179
156
global code
180
157
evaluateWithScopeExtension@[native code]
181
158
_evaluateOn
182
159
_evaluateAndWrap
183
- evaluate
184
- ` ) ;
160
+ evaluate` ;
185
161
const error = createErrorObject ( 'foo' , safariStack ) ;
186
162
expect ( formatWarning ( error ) ) . to . equal (
187
- dedent ( `
163
+ dedent `
188
164
foo
189
165
z
190
166
y
@@ -193,8 +169,7 @@ describe('formatWarning()', () => {
193
169
evaluateWithScopeExtension@[native code]
194
170
_evaluateOn
195
171
_evaluateAndWrap
196
- evaluate
197
- ` ) ,
172
+ evaluate` ,
198
173
) ;
199
174
} ) ;
200
175
0 commit comments