forked from croc-code/grunt-croc-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
185 lines (167 loc) · 4.89 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
/*
* grunt-croc-qunit
*
* Copyright (c) 2013 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tasks/**/*.js',
],
options: {
jshintrc: '.jshintrc'
}
},
// Create a local web server for testing http:// URIs.
connect: {
root_server: {
options: {
port: 9000,
base: '.',
},
},
test_server: {
options: {
port: 9001,
base: 'test',
},
}
},
// Unit tests.
qunit: {
options: {
phantomPath: require('phantomjs').path
},
all_tests: ['test/*{1,2}.html'],
individual_tests: {
files: [
{src: 'test/*1.html'},
{src: 'test/*{1,2}.html'},
]
},
urls: {
options: {
urls: [
'http://localhost:9000/test/qunit1.html',
'http://localhost:9001/qunit2.html',
]
},
},
urls_and_files: {
options: {
urls: '<%= qunit.urls.options.urls %>',
},
src: 'test/*{1,2}.html',
},
},
'test-phantom': {
basic: {
options: {
url: 'test/fixtures/basic.html',
phantomPath: require('phantomjs').path,
expected: [1, 2, 3, 4, 5, 6],
test: function test(a, b, c) {
if (!test.actual) { test.actual = []; }
test.actual.push(a, b, c);
}
}
},
inject: {
options: {
url: 'test/fixtures/inject.html',
phantomPath: require('phantomjs').path,
inject: require('path').resolve('test/fixtures/inject.js'),
expected: 'injected',
test: function test(msg) {
test.actual = msg;
}
}
}
}
});
// The most basic of tests. Not even remotely comprehensive.
grunt.registerMultiTask('test-phantom', function() {
var options = this.options();
var phantomjs = require('./tasks/phantomjs').init(grunt);
// Do something.
phantomjs.on('test', options.test);
phantomjs.on('done', phantomjs.halt);
// Built-in error handlers.
phantomjs.on('fail.load', function(url) {
phantomjs.halt();
grunt.verbose.write('Running PhantomJS...').or.write('...');
grunt.log.error();
grunt.warn('PhantomJS unable to load "' + url + '" URI.');
});
phantomjs.on('fail.timeout', function() {
phantomjs.halt();
grunt.log.writeln();
grunt.warn('PhantomJS timed out.');
});
// This task is async.
var done = this.async();
// Spawn phantomjs
phantomjs.spawn(options.url, {
// Additional PhantomJS options.
options: options,
// Complete the task when done.
done: function(err) {
if (err) { done(err); return; }
var assert = require('assert');
var difflet = require('difflet')({indent: 2, comment: true});
try {
assert.deepEqual(options.test.actual, options.expected, 'Actual should match expected.');
grunt.log.writeln('Test passed.');
done();
} catch (err) {
grunt.log.subhead('Assertion Failure');
console.log(difflet.compare(err.expected, err.actual));
done(err);
}
}
});
});
// Build a mapping of url success counters.
var successes = {};
var currentUrl;
grunt.event.on('qunit.spawn', function(url) {
currentUrl = url;
if (!successes[currentUrl]) { successes[currentUrl] = 0; }
});
grunt.event.on('qunit.done', function(failed, passed) {
if (failed === 0 && passed === 2) { successes[currentUrl]++; }
});
grunt.registerTask('really-test', 'Test to see if qunit task actually worked.', function() {
var assert = require('assert');
var difflet = require('difflet')({indent: 2, comment: true});
var actual = successes;
var expected = {
'test/qunit1.html': 3,
'test/qunit2.html': 3,
'http://localhost:9000/test/qunit1.html': 2,
'http://localhost:9001/qunit2.html': 2
};
try {
assert.deepEqual(actual, expected, 'Actual should match expected.');
} catch (err) {
grunt.log.subhead('Actual should match expected.');
console.log(difflet.compare(expected, actual));
throw new Error(err.message);
}
});
// Actually load this plugin's task(s).
grunt.loadTasks('tasks');
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-connect');
//grunt.loadNpmTasks('grunt-contrib-internal');
// Whenever the "test" task is run, run some basic tests.
grunt.registerTask('test-task', ['connect', 'qunit', 'really-test']);
// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test-phantom', 'test-task']);
};