1
1
2
2
'use strict' ;
3
3
4
+ // Native dependencies
5
+ const fs = require ( 'fs' ) ,
6
+ path = require ( 'path' ) ;
7
+
8
+ // 3rd party dependencies
9
+ const istanbul = require ( 'istanbul' ) ,
10
+ Jasmine = require ( 'jasmine' ) ,
11
+ SpecReporter = require ( 'jasmine-spec-reporter' ) ,
12
+ deepmerge = require ( 'deepmerge' ) ;
13
+
4
14
module . exports = function jasmineNodeTask ( grunt ) {
5
15
6
- var istanbul = require ( 'istanbul' ) ,
7
- Jasmine = require ( 'jasmine' ) ,
8
- SpecReporter = require ( 'jasmine-spec-reporter' ) ,
9
- merge = require ( 'deepmerge' ) ,
10
- path = require ( 'path' ) ,
11
- fs = require ( 'fs' ) ;
16
+ const reports = [ ] ;
12
17
13
18
var reportingDir ,
14
19
coverageVar = '$$cov_' + new Date ( ) . getTime ( ) + '$$' ,
15
20
fileSrc = [ '**/*.js' ] ,
16
21
options ,
17
- done ,
18
-
19
- reports = [ ] ;
22
+ done ;
20
23
21
- var coverageCollect = function coverageCollect ( covPattern , collector ) {
24
+ const coverageCollect = function coverageCollect ( covPattern , collector ) {
22
25
23
26
// The pattern should be relative to the directory in which the reports are written
24
27
var coverageFiles = grunt . file . expand ( path . resolve ( reportingDir , covPattern ) ) ;
@@ -44,7 +47,7 @@ module.exports = function jasmineNodeTask(grunt) {
44
47
} ) ;
45
48
} ;
46
49
47
- var coverageThresholdCheck = function coverageThresholdCheck ( collector ) {
50
+ const coverageThresholdCheck = function coverageThresholdCheck ( collector ) {
48
51
49
52
// http://gotwarlost.github.io/istanbul/public/apidocs/classes/ObjectUtils.html
50
53
var objUtils = istanbul . utils ;
@@ -69,7 +72,7 @@ module.exports = function jasmineNodeTask(grunt) {
69
72
} ) ;
70
73
} ;
71
74
72
- var includeAllSources = function includeAllSources ( cov , opts ) {
75
+ const includeAllSources = function includeAllSources ( cov , opts ) {
73
76
if ( ! opts || ! opts . instrumenter || ! opts . transformer || ! opts . matchFn || ! cov ) {
74
77
grunt . log . error ( 'includeAllSources was set but coverage wasn\'t run.' ) ;
75
78
return ;
@@ -100,7 +103,7 @@ module.exports = function jasmineNodeTask(grunt) {
100
103
} ) ;
101
104
} ;
102
105
103
- var collectReports = function collectReports ( opts ) {
106
+ const collectReports = function collectReports ( opts ) {
104
107
var reportFile = path . resolve ( reportingDir , options . coverage . reportFile ) ,
105
108
collector = new istanbul . Collector ( ) , // http://gotwarlost.github.io/istanbul/public/apidocs/classes/Collector.html
106
109
cov = global [ coverageVar ] ;
@@ -115,7 +118,7 @@ module.exports = function jasmineNodeTask(grunt) {
115
118
116
119
grunt . verbose . writeln ( 'Writing coverage object [' + reportFile + ']' ) ;
117
120
118
- fs . writeFileSync ( reportFile , JSON . stringify ( cov ) , 'utf8' ) ;
121
+ fs . writeFileSync ( reportFile , JSON . stringify ( cov , null , ' ' ) , 'utf8' ) ;
119
122
120
123
if ( options . coverage . collect !== false ) {
121
124
options . coverage . collect . forEach ( function eachCollect ( covPattern ) {
@@ -135,19 +138,19 @@ module.exports = function jasmineNodeTask(grunt) {
135
138
coverageThresholdCheck ( collector ) ;
136
139
} ;
137
140
138
- var exitHandler = function exitHandler ( opts ) {
141
+ const exitHandler = function exitHandler ( opts ) {
139
142
if ( typeof global [ coverageVar ] !== 'object' || Object . keys ( global [ coverageVar ] ) . length === 0 ) {
140
143
grunt . log . error ( 'No coverage information was collected, exit without writing coverage information' ) ;
141
144
return ;
142
145
}
143
146
collectReports ( opts ) ;
144
147
} ;
145
148
146
- var istanbulMatcherRun = function istanbulMatcherRun ( matchFn ) {
149
+ const istanbulMatcherRun = function istanbulMatcherRun ( matchFn ) {
147
150
148
- var instrumenter = new istanbul . Instrumenter ( { coverageVariable : coverageVar } ) ;
149
- var transformer = instrumenter . instrumentSync . bind ( instrumenter ) ;
150
- var hookOpts = { verbose : options . isVerbose } ;
151
+ const instrumenter = new istanbul . Instrumenter ( { coverageVariable : coverageVar } ) ;
152
+ const transformer = instrumenter . instrumentSync . bind ( instrumenter ) ;
153
+ const hookOpts = { verbose : options . isVerbose } ;
151
154
152
155
istanbul . hook . hookRequire ( matchFn , transformer , hookOpts ) ;
153
156
@@ -167,7 +170,7 @@ module.exports = function jasmineNodeTask(grunt) {
167
170
} ;
168
171
169
172
170
- var runner = function runner ( opts ) {
173
+ const runner = function runner ( opts ) {
171
174
opts = opts || { } ;
172
175
173
176
if ( options . captureExceptions ) {
@@ -182,7 +185,7 @@ module.exports = function jasmineNodeTask(grunt) {
182
185
} ) ;
183
186
}
184
187
try {
185
- var jasmine = new Jasmine ( ) ;
188
+ const jasmine = new Jasmine ( ) ;
186
189
jasmine . loadConfig ( options . jasmine ) ;
187
190
jasmine . addReporter ( new SpecReporter ( options . jasmine . reporter ) ) ;
188
191
jasmine . onComplete ( function ( passed ) {
@@ -201,22 +204,22 @@ module.exports = function jasmineNodeTask(grunt) {
201
204
}
202
205
} ;
203
206
204
- var doCoverage = function doCoverage ( ) {
207
+ const doCoverage = function doCoverage ( ) {
205
208
206
209
// set up require hooks to instrument files as they are required
207
- var Report = istanbul . Report ;
210
+ const Report = istanbul . Report ;
208
211
209
212
grunt . file . mkdir ( reportingDir ) ; // ensure we fail early if we cannot do this
210
213
211
- var reportClassNames = options . coverage . report ;
214
+ const reportClassNames = options . coverage . report ;
212
215
reportClassNames . forEach ( function eachReport ( reportClassName ) {
213
216
reports . push ( Report . create ( reportClassName , {
214
217
dir : reportingDir ,
215
218
watermarks : options . coverage . watermarks
216
219
} ) ) ;
217
220
} ) ;
218
221
219
- var excludes = options . coverage . excludes || [ ] ;
222
+ const excludes = options . coverage . excludes || [ ] ;
220
223
excludes . push ( '**/node_modules/**' ) ;
221
224
222
225
// http://gotwarlost.github.io/istanbul/public/apidocs/classes/Istanbul.html#method_matcherFor
@@ -239,7 +242,7 @@ module.exports = function jasmineNodeTask(grunt) {
239
242
grunt . registerMultiTask ( 'jasmine_node' , 'Runs jasmine-node with Istanbul code coverage' , function registerGrunt ( ) {
240
243
241
244
// Default options. Once Grunt does recursive merge, use that, maybe 0.4.6
242
- options = merge ( {
245
+ options = deepmerge ( {
243
246
244
247
// Used only in this plugin, thus can be refactored out
245
248
projectRoot : process . cwd ( ) , // string
0 commit comments