forked from asciidisco/grunt-lib-phantomjs-istanbul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
179 lines (169 loc) · 4.99 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
'use strict';
module.exports = function (grunt) {
// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'src/*.js',
'test/*.js'
],
options: {
jshintrc: '.jshintrc'
}
},
test: {
basic: {
options: {
url: 'test/fixtures/basic.html',
startProducer: true,
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',
expected: 'injected',
test: function test(msg) {
test.actual = msg;
},
puppeteer: {
inject: require('path').resolve('test/fixtures/inject.js')
}
}
},
headers: {
options: {
url: 'http://localhost:8075',
startProducer: true,
server: './test/fixtures/headers_server.js',
expected: 'custom_header_567',
test: function test(msg) {
test.actual = msg;
},
puppeteer: {
page: {
customHeaders: {
'X-CUSTOM': 'custom_header_567'
}
}
}
}
},
viewportSize: {
options: {
url: 'test/fixtures/viewportSize.html',
startProducer: true,
expected: [1366, 800],
test: function test(a, b) {
if (!test.actual) {
test.actual = [];
}
test.actual.push(a, b);
},
puppeteer: {
page: {
viewportSize: {
width: 1366,
height: 800
}
}
}
}
}
}
});
// The most basic of tests. Not even remotely comprehensive.
grunt.registerMultiTask('test', 'A test, of sorts.', function () {
/*
Sequence of Events:
1. spawn thread of producer
2. spawn thread of consumer, this handles the async call.
3. consumer connects to producer
4. consumer sends path to qunit html file to producer
5. producer navigates to the page and evaluates it, sending log and utlimately the successs status
6. producer notifies the consumer when it is finished, consumer disconnects.
7. consumer kills itself resolving it's async call
8. producer kills itself when there are no consumers left
9. resolve grunt job with the success status determining success of failure.
*/
/*
grunt
task
producer (or producer monitor with internal queue with hashes for each consumer thread)
consumer threads (if using monitor, there are many consumer threads)
*/
var options = this.options();
options.url = require('fs').realpathSync(options.url);
var done = this.async();
options.resolve = function (isSuccessful, output) {
console.log('Test Finished');
//TODO add some output testing here?
done(isSuccessful);
};
var PuppetMaster = require('./src/puppetmaster');
var masterOfPuppets = new PuppetMaster(options);
//TODO -> this needs to be cleaned up
masterOfPuppets.listenOn({
'test': options.test,
'debug': function (msg) {
grunt.log.writeln('debug:' + msg);
},
'console.log': function (response) {
grunt.log.writeln(response);
},
'fail.load': function (url) {
grunt.verbose.write('Running Puppeteer...').or.write('...');
grunt.log.error();
grunt.warn('Puppeteer unable to load "' + url + '" URI.');
},
'fail.timeout': function () {
grunt.log.writeln();
grunt.warn('Puppeteer timed out.');
},
'error': function (error) {
var code = error.code;
var syscall = error.syscall;
// Ignore time out issue unless thrown from emitter.
if (!(code === 'ENOENT' || code === 'ECONNREFUSED') && syscall !== 'connect') {
grunt.fail.warn(error);
}
},
'done': function (res) {
const {
error
} = res;
//clean up and etc..
if (error) {
done(error);
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 (error) {
grunt.log.subhead('Assertion Failure');
console.log(difflet.compare(error.expected, error.actual));
done(error);
}
},
});
masterOfPuppets.start();
});
// The jshint plugin is used for linting.
grunt.loadNpmTasks('grunt-contrib-jshint');
// By default, lint library.
grunt.registerTask('default', ['jshint', 'test']);
};