This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
forked from scotthogan/grunt-mocha-phantom-istanbul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
271 lines (233 loc) · 6.87 KB
/
Gruntfile.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Example Gruntfile for Mocha setup
*/
'use strict';
module.exports = function(grunt) {
var port = 8981;
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/**/*.js', ],
options: {
jshintrc: '.jshintrc'
}
},
watch: {
// If you want to watch files and run tests automatically on change
test: {
files: [
'example/js/**/*.js',
'example/test/spec/**/*.js',
'phantomjs/*',
'tasks/*',
'Gruntfile.js'
],
tasks: 'test'
}
},
mocha: {
// runs all html files (except test2.html) in the test dir
// In this example, there's only one, but you can add as many as
// you want. You can split them up into different groups here
// ex: admin: [ 'test/admin.html' ]
all: ['example/test/**/!(test2|testBail|testPage|testCoverage).html'],
// Runs 'test/test2.html' with specified mocha options.
// This variant auto-includes 'bridge.js' so you do not have
// to include it in your HTML spec file. Instead, you must add an
// environment check before you run `mocha.run` in your HTML.
test2: {
// Test files
src: ['example/test/test2.html'],
options: {
// mocha options
mocha: {
ignoreLeaks: false,
grep: 'food'
},
reporter: 'Spec',
// Indicates whether 'mocha.run()' should be executed in
// 'bridge.js'
run: true,
timeout: 10000
}
},
// Runs the same as test2 but with URL's
testUrls: {
options: {
// mocha options
mocha: {
ignoreLeaks: false,
grep: 'food'
},
reporter: 'Nyan',
// URLs passed through as options
urls: ['http://localhost:' + port + '/example/test/test2.html'],
// Indicates whether 'mocha.run()' should be executed in
// 'bridge.js'
run: true
}
},
// Test using a custom reporter
testReporter: {
src: ['example/test/test.html', 'example/test/test2.html'],
options: {
mocha: {
ignoreLeaks: false,
grep: 'food'
},
reporter: './example/test/reporter/simple',
run: true
}
},
// Test log option
testLog: {
src: ['example/test/test.html'],
options: {
mocha: {
ignoreLeaks: false,
grep: 'food'
},
log: true
}
},
testDest1: {
// Test files
src: ['example/test/test2.html'],
dest: 'example/test/results/spec.out',
options: {
reporter: 'Spec',
run: true
}
},
// Same as above, but with URLS + Xunit
testDest2: {
options: {
reporter: 'XUnit',
// URLs passed through as options
urls: ['http://localhost:' + port + '/example/test/test2.html'],
run: true
},
dest: 'example/test/results/xunit.out'
},
// Test a failing test with bail: true
testBail: {
src: ['example/test/testBail.html'],
// Bail option
options: {
run: true,
bail: true
}
},
// This test should never run
neverTest: {
src: ['example/test/test.html'],
// Bail option
options: {
run: true
}
},
// Test page options
testPage: {
src: ['example/test/testPage.html'],
options: {
page: {
settings: {
userAgent: 'grunt-mocha-agent'
}
}
}
},
// Test Istanbul integration.
testCoverage: {
src: ['example/test/testCoverage.html'],
options: {
run: true,
coverage: {
htmlReport: 'example/test/results/coverage.out/html',
coberturaReport: 'example/test/results/coverage.out/cobertura',
lcovReport: 'example/test/results/coverage.out/lcov',
cloverReport: 'example/test/results/coverage.out/clover',
jsonReport: 'example/test/results/coverage.out/json'
}
}
}
},
connect: {
testUrls: {
options: {
port: port,
base: '.'
}
},
testDest: {
options: {
port: port + 1,
base: '.'
}
}
}
});
grunt.registerTask('verifyDestResults', function () {
var expected = ['spec', 'xunit'];
expected.forEach(function (reporter) {
var output = 'example/test/results/' + reporter + '.out';
// simply check if the file is non-empty since verifying if the output is
// correct based on the spec is kind of hard due to changing test running
// times and different ways to report this time in reporters.
if (!grunt.file.read(output, 'utf8'))
grunt.fatal('Empty reporter output: ' + reporter);
// Clean-up
grunt.file.delete(output);
grunt.log.ok('Reporter output non-empty for %s', reporter);
});
// Check for Coverage Reports.
var expectedCoverage = [
'cobertura/cobertura-coverage.xml',
'lcov/lcov.info',
'clover/clover.xml',
'json/coverage.json',
'html/index.html'
];
expectedCoverage.forEach(function (reporter) {
var output = 'example/test/results/coverage.out/' + reporter;
if (!grunt.file.read(output, 'utf8')) {
grunt.fatal('Empty reporter output: ' + reporter);
}
grunt.log.ok('Reporter output non-empty for %s', reporter);
});
// Clean-up.
grunt.file.delete('example/test/results/coverage.out');
});
// IMPORTANT: Actually load this plugin's task(s).
// To use grunt-mocha, replace with grunt.loadNpmTasks('grunt-mocha')
grunt.loadTasks('tasks');
// grunt.loadNpmTasks('grunt-mocha');
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.task.registerTask('testUrls', ['connect:testUrls', 'mocha:testUrls']);
grunt.task.registerTask('testLog', ['mocha:testLog']);
grunt.task.registerTask('testReporter', ['mocha:testReporter']);
grunt.task.registerTask('testDest', [
'mocha:testDest1',
'connect:testDest',
'mocha:testDest2',
'mocha:testCoverage',
'verifyDestResults'
]);
grunt.task.registerTask('testPage', ['mocha:testPage']);
// WARNING: Running this test will cause grunt to fail after mocha:testBail
grunt.task.registerTask('testBail', ['mocha:testBail', 'mocha:neverTest']);
grunt.task.registerTask('test', [
'mocha:all',
'testUrls',
'testLog',
'testReporter',
'testDest',
'testPage',
'testBail',
]);
// By default, lint and run all tests.
grunt.task.registerTask('default', ['jshint', 'test']);
};