-
Notifications
You must be signed in to change notification settings - Fork 13
/
gulpfile.js
169 lines (154 loc) · 4.57 KB
/
gulpfile.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
/**
* This is the gulp script to run jasmine tests with coverage analysis using istanbul.
* For more information, please see also:
* @see {@link http://jasmine.github.io/2.0/introduction.html}
* @see {@link https://www.npmjs.com/package/istanbul}
* @see {@link https://www.npmjs.com/package/gulp}
* @see {@link https://www.npmjs.com/package/gulp-istanbul}
* @see {@link https://www.npmjs.com/package/gulp-jasmine}
* @see {@link https://www.npmjs.com/package/jasmine-reporters}
* @see {@link https://www.npmjs.com/package/glob}
**/
var path = require('path')
var gulp = require('gulp')
var jasmine = require('gulp-jasmine')
var istanbul = require('gulp-istanbul')
var reporters = require('jasmine-reporters')
// required to replace absolute paths in results with relative ones
var replace = require('gulp-replace')
// configuration of the test coverage, uses glob syntax
// @see {@link https://www.npmjs.com/package/glob}
// the following files are included in the coverage analysis
// include all javascript files and exclude myExclude.js
// replace or remove files depending on what is to be excluded or included in addition
var includedScripts = ['**/*.js', '!myExclude.js']
// the following files are part of the framework and are to be excluded from the test coverage
var defaultExclusion = [
'!**/*spec.js',
'!rungulp.js',
'!gulpfile.js',
'!**/node_modules/**',
'!appcontroller.*/**',
'!vendor/**'
]
// test results folder for test view history
// assign each test run a unique timestamp, coverage and test results
// are associated via timestamp, so the test results folder fills up with files
// of form:
// 123456_report.xml
// 123456_coverage.json
// 456789_report.xml
// 456789_coverage.json
// ...
var testResultsDir = path.join(__dirname, '.testresults')
var timestamp = Date.now()
var testResultFile = timestamp + '_report.xml'
var coverageResultFile = timestamp + '_coverage.json'
/**
* Instrument the test/productive code
*/
gulp.task('instrumentation', function () {
return (
gulp
.src(includedScripts.concat(defaultExclusion), { allowEmpty: true })
// Covering files
.pipe(
istanbul({
includeUntested: true // instrument all files
})
)
// Force `require` to return covered files
.pipe(istanbul.hookRequire())
)
})
/**
* Execute tests with coverage information, requires instrumentation
* before tests are executed
*/
gulp.task(
'jasmine-istanbul',
gulp.series('instrumentation', function () {
// run all tests ending with "spec", skip all tests that are part of the node_modules
return gulp
.src(['**/*spec.js', '!**/node_modules/**'], { allowEmpty: true })
.pipe(
jasmine({
errorOnFail: false,
// use the standard junit xml reporter
reporter: new reporters.JUnitXmlReporter({
savePath: testResultsDir,
filePrefix: testResultFile,
consolidateAll: true
})
})
)
.pipe(
istanbul.writeReports({
// generage json report for the coverage
reporters: ['json'],
reportOpts: {
json: {
dir: testResultsDir,
file: coverageResultFile
}
}
})
)
})
)
/**
* Execute tests without coverage information
*/
gulp.task('jasmine', function () {
// run all tests ending with "spec", skip all tests that are part of the node_modules
return gulp
.src(['**/*spec.js', '!**/node_modules/**'], { allowEmpty: true })
.pipe(
jasmine({
errorOnFail: false,
// use the standard junit xml reporter
reporter: new reporters.JUnitXmlReporter({
savePath: testResultsDir,
filePrefix: testResultFile,
consolidateAll: true
})
})
)
})
/**
* Remove absolute path portions so test view can open the referenced files
*/
function postProcessing () {
return gulp
.src(
[
path.join(testResultsDir, testResultFile),
path.join(testResultsDir, coverageResultFile)
],
{
dot: true,
allowEmpty: true
}
)
.pipe(replace(__dirname, ''))
.pipe(gulp.dest(testResultsDir))
}
// run tests with coverage analysis
gulp.task(
'test-coverage',
gulp.series('jasmine-istanbul', function () {
return postProcessing()
})
)
// only run the tests, skip test coverage analysis
gulp.task(
'test',
gulp.series('jasmine', function () {
return postProcessing()
})
)
// the default task is to run tests with coverage analysis
gulp.task(
'default',
gulp.series('test-coverage', function () {})
)