@@ -22,13 +22,8 @@ describe('config karma shared', () => {
22
22
} ) ;
23
23
24
24
mock . reRequire ( '../config/karma/shared.karma.conf' ) ( {
25
- set : ( config ) => {
26
- const collector = {
27
- getFinalCoverage : ( ) => ( { } )
28
- } ;
25
+ set : ( ) => {
29
26
expect ( called ) . toEqual ( true ) ;
30
- expect ( typeof config . coverageReporter . _onWriteReport ) . toEqual ( 'function' ) ;
31
- expect ( config . coverageReporter . _onWriteReport ( collector ) ) . toBeDefined ( ) ;
32
27
done ( ) ;
33
28
}
34
29
} ) ;
@@ -59,33 +54,6 @@ describe('config karma shared', () => {
59
54
} ) ;
60
55
} ) ;
61
56
62
- function checkCodeCoverage ( configValue , threshold ) {
63
-
64
- mock ( '../config/sky-pages/sky-pages.config.js' , {
65
- getSkyPagesConfig : ( ) => ( {
66
- skyux : {
67
- codeCoverageThreshold : configValue
68
- }
69
- } )
70
- } ) ;
71
-
72
- mock ( testConfigFilename , {
73
- getWebpackConfig : ( ) => { }
74
- } ) ;
75
-
76
- mock . reRequire ( '../config/karma/shared.karma.conf' ) ( {
77
- set : ( config ) => {
78
- expect ( config . coverageReporter . check ) . toEqual ( {
79
- global : {
80
- statements : threshold ,
81
- branches : threshold ,
82
- functions : threshold ,
83
- lines : threshold
84
- }
85
- } ) ;
86
- }
87
- } ) ;
88
- }
89
57
90
58
it ( 'should not add the check property when codeCoverageThreshold is not defined' , ( ) => {
91
59
mock ( '../config/sky-pages/sky-pages.config.js' , {
@@ -105,18 +73,6 @@ describe('config karma shared', () => {
105
73
} ) ;
106
74
} ) ;
107
75
108
- it ( 'should handle codeCoverageThreshold set to "none"' , ( ) => {
109
- checkCodeCoverage ( 'none' , 0 ) ;
110
- } ) ;
111
-
112
- it ( 'should handle codeCoverageThreshold set to "standard"' , ( ) => {
113
- checkCodeCoverage ( 'standard' , 80 ) ;
114
- } ) ;
115
-
116
- it ( 'should handle codeCoverageThreshold set to "strict"' , ( ) => {
117
- checkCodeCoverage ( 'strict' , 100 ) ;
118
- } ) ;
119
-
120
76
it ( 'should pass the logColor flag to the config' , ( ) => {
121
77
mock ( '@blackbaud/skyux-logger' , { logColor : false } ) ;
122
78
mock . reRequire ( '../config/karma/shared.karma.conf' ) ( {
@@ -153,4 +109,168 @@ describe('config karma shared', () => {
153
109
} ) ;
154
110
} ) ;
155
111
112
+ describe ( 'code coverage' , ( ) => {
113
+ let errorSpy ;
114
+ let exitSpy ;
115
+ let infoSpy ;
116
+
117
+ const coverageProps = [
118
+ 'statements' ,
119
+ 'branches' ,
120
+ 'lines' ,
121
+ 'functions'
122
+ ] ;
123
+
124
+ beforeEach ( ( ) => {
125
+ mock ( testConfigFilename , {
126
+ getWebpackConfig : ( ) => { }
127
+ } ) ;
128
+
129
+ errorSpy = jasmine . createSpy ( 'error' ) ;
130
+ infoSpy = jasmine . createSpy ( 'info' ) ;
131
+
132
+ exitSpy = spyOn ( process , 'exit' ) ;
133
+
134
+ mock ( '@blackbaud/skyux-logger' , {
135
+ error : errorSpy ,
136
+ info : infoSpy
137
+ } ) ;
138
+
139
+ mock ( 'remap-istanbul' , {
140
+ remap : ( ) => {
141
+ return {
142
+ fileCoverageFor : ( ) => { } ,
143
+ files : ( ) => [
144
+ 'test.js'
145
+ ]
146
+ } ;
147
+ }
148
+ } ) ;
149
+ } ) ;
150
+
151
+ function createMergeSummaryObjectSpy ( testPct ) {
152
+ return jasmine . createSpy ( 'mergeSummaryObjects' ) . and . callFake ( ( ) => {
153
+ const summary = { } ;
154
+
155
+ coverageProps . forEach ( ( key ) => {
156
+ summary [ key ] = {
157
+ pct : testPct
158
+ } ;
159
+ } ) ;
160
+
161
+ return summary ;
162
+ } ) ;
163
+ }
164
+
165
+ function mockIstanbul ( mergeSummaryObjects ) {
166
+ mock ( 'istanbul' , {
167
+ utils : {
168
+ summarizeFileCoverage : ( ) => { } ,
169
+ mergeSummaryObjects
170
+ }
171
+ } ) ;
172
+ }
173
+
174
+ function mockConfig ( codeCoverageThreshold ) {
175
+ mock ( '../config/sky-pages/sky-pages.config.js' , {
176
+ getSkyPagesConfig : ( ) => ( {
177
+ skyux : {
178
+ codeCoverageThreshold
179
+ }
180
+ } )
181
+ } ) ;
182
+ }
183
+
184
+ function resetSpies ( ) {
185
+ errorSpy . calls . reset ( ) ;
186
+ infoSpy . calls . reset ( ) ;
187
+ exitSpy . calls . reset ( ) ;
188
+ }
189
+
190
+ function checkCodeCoverage ( thresholdName , threshold , testPct , shouldPass ) {
191
+ const mergeSummaryObjectsSpy = createMergeSummaryObjectSpy ( testPct ) ;
192
+
193
+ mockIstanbul ( mergeSummaryObjectsSpy ) ;
194
+ mockConfig ( thresholdName ) ;
195
+
196
+ resetSpies ( ) ;
197
+
198
+ const browsers = [ 'Chrome' , 'Firefox' ] ;
199
+ const reporters = [
200
+ { type : 'json' } ,
201
+ { type : 'html' }
202
+ ] ;
203
+
204
+ mock . reRequire ( '../config/karma/shared.karma.conf' ) ( {
205
+ browsers : browsers ,
206
+ set : ( config ) => {
207
+ config . coverageReporter . reporters = reporters ;
208
+
209
+ const fakeCollector = {
210
+ getFinalCoverage : ( ) => {
211
+ return {
212
+ files : ( ) => [ ]
213
+ } ;
214
+ }
215
+ } ;
216
+
217
+ // Simulate multiple reporters/browsers the same way that karma-coverage does.
218
+ reporters . forEach ( ( ) => {
219
+ browsers . forEach ( ( ) => {
220
+ config . coverageReporter . _onWriteReport ( fakeCollector ) ;
221
+ } ) ;
222
+ } ) ;
223
+
224
+ // Code coverage should be evaluated once per browser unless the threshold is 0,
225
+ // in which case it should not be called at all.
226
+ expect ( mergeSummaryObjectsSpy ) . toHaveBeenCalledTimes (
227
+ threshold === 0 ? 0 : browsers . length
228
+ ) ;
229
+
230
+ // Verify the tests pass or fail based on the coverage percentage.
231
+ const doneSpy = jasmine . createSpy ( 'done' ) ;
232
+
233
+ config . coverageReporter . _onExit ( doneSpy ) ;
234
+
235
+ if ( shouldPass ) {
236
+ expect ( exitSpy ) . not . toHaveBeenCalled ( ) ;
237
+ expect ( errorSpy ) . not . toHaveBeenCalled ( ) ;
238
+ expect ( infoSpy ) . not . toHaveBeenCalledWith ( 'Karma has exited with 1.' ) ;
239
+ } else {
240
+ expect ( exitSpy ) . toHaveBeenCalledWith ( 1 ) ;
241
+
242
+ browsers . forEach ( ( browserName ) => {
243
+ coverageProps . forEach ( ( key ) => {
244
+ expect ( errorSpy ) . toHaveBeenCalledWith (
245
+ `Coverage in ${ browserName } for ${ key } (${ testPct } %) does not meet ` +
246
+ `global threshold (${ threshold } %)`
247
+ ) ;
248
+ } ) ;
249
+ } )
250
+
251
+ expect ( infoSpy ) . toHaveBeenCalledWith ( 'Karma has exited with 1.' ) ;
252
+ }
253
+
254
+ expect ( doneSpy ) . toHaveBeenCalled ( ) ;
255
+ }
256
+ } ) ;
257
+ }
258
+
259
+ it ( 'should handle codeCoverageThreshold set to "none"' , ( ) => {
260
+ checkCodeCoverage ( 'none' , 0 , 0 , true ) ;
261
+ checkCodeCoverage ( 'none' , 0 , 1 , true ) ;
262
+ } ) ;
263
+
264
+ it ( 'should handle codeCoverageThreshold set to "standard"' , ( ) => {
265
+ checkCodeCoverage ( 'standard' , 80 , 79 , false ) ;
266
+ checkCodeCoverage ( 'standard' , 80 , 80 , true ) ;
267
+ checkCodeCoverage ( 'standard' , 80 , 81 , true ) ;
268
+ } ) ;
269
+
270
+ it ( 'should handle codeCoverageThreshold set to "strict"' , ( ) => {
271
+ checkCodeCoverage ( 'strict' , 100 , 99 , false ) ;
272
+ checkCodeCoverage ( 'strict' , 100 , 100 , true ) ;
273
+ } ) ;
274
+ } ) ;
275
+
156
276
} ) ;
0 commit comments