-
Notifications
You must be signed in to change notification settings - Fork 112
/
mocha-phantomjs.coffee
398 lines (301 loc) · 14.3 KB
/
mocha-phantomjs.coffee
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
describe 'mocha-phantomjs', ->
expect = require('chai').expect
spawn = require('child_process').spawn
url = require('url')
fs = require('fs')
fileURL = (file) ->
fullPath = fs.realpathSync "#{process.cwd()}/test/#{file}.html"
fullPath = fullPath.replace /\\/g, '\/'
urlString = fullPath
urlString = url.format { protocol: 'file', hostname: '', pathname: fullPath } if process.platform isnt 'win32'
urlString
before ->
@runner = (done, args, callback) ->
stdout = ''
stderr = ''
spawnArgs = ["#{process.cwd()}/bin/mocha-phantomjs"].concat(args)
mochaPhantomJS = spawn 'node', spawnArgs
mochaPhantomJS.stdout.on 'data', (data) -> stdout = stdout.concat data.toString()
mochaPhantomJS.stderr.on 'data', (data) -> stderr = stderr.concat data.toString()
mochaPhantomJS.on 'exit', (code) ->
callback?(code, stdout, stderr)
done?()
it 'returns a failure code and shows usage when no args are given', (done) ->
@runner done, [], (code, stdout, stderr) ->
expect(code).to.equal 1
expect(stdout).to.match /Usage: mocha-phantomjs/
it 'returns a failure code and notifies of bad url when given one', (done) ->
@runner done, ['foo/bar.html'], (code, stdout, stderr) ->
expect(code).to.equal 1
expect(stdout).to.match /failed to load the page/i
expect(stdout).to.match /check the url/i
expect(stdout).to.match /foo\/bar.html/i
it 'returns a failure code and notifies of no such runner class', (done) ->
@runner done, ['-R', 'nonesuch', fileURL('passing')], (code, stdout, stderr) ->
expect(code).to.equal 1
expect(stdout).to.match /Unable to open file 'nonesuch'/
it 'returns a success code when a directory exists with the same name as a built-in runner', (done) ->
fs.mkdir 'spec'
@runner done, ['-R', 'spec', fileURL('passing')], (code, stdout, stderr) ->
fs.rmdir 'spec'
expect(code).to.equal 0
it 'returns a failure code when mocha can not be found on the page', (done) ->
@runner done, [fileURL('blank')], (code, stdout, stderr) ->
expect(code).to.equal 1
expect(stdout).to.match /Failed to find mocha on the page/
it 'returns a failure code when mocha fails to start for any reason', (done) ->
@runner done, [fileURL('bad')], (code, stdout, stderr) ->
expect(code).to.equal 1
expect(stdout).to.match /Failed to start mocha./
it 'returns a failure code when mocha is not started in a timely manner', (done) ->
@runner done, ['-t', 500, fileURL('timeout')], (code, stdout, stderr) ->
expect(code).to.equal 255
expect(stdout).to.match /Failed to start mocha: Init timeout/
it 'returns a failure code when there is a page error', (done) ->
@runner done, [fileURL('error')], (code, stdout, stderr) ->
expect(code).to.equal 1
expect(stdout).to.match /ReferenceError/
it 'does not fail when an iframe is used', (done) ->
@runner done, [fileURL('iframe')], (code, stdout, stderr) ->
expect(stdout).to.not.match /Failed to load the page\./m
expect(code).to.equal 0
it 'returns the mocha runner from run() and allows modification of it', (done) ->
@runner done, [fileURL('mocha-runner')], (code, stdout, stderr) ->
expect(stdout).to.not.match /Failed via an Event/m
expect(code).to.equal 1
passRegExp = (n) -> ///\u001b\[32m\s\s[✔✓]\u001b\[0m\u001b\[90m\spasses\s#{n}///
skipRegExp = (n) -> ///\u001b\[36m\s\s-\sskips\s#{n}\u001b\[0m///
failRegExp = (n) -> ///\u001b\[31m\s\s#{n}\)\sfails\s#{n}\u001b\[0m///
passComplete = (n) -> ///\u001b\[0m\n\n\n\u001b\[92m\s\s[✔✓]\u001b\[0m\u001b\[32m\s#{n}\stests\scomplete///
pendComplete = (n) -> ///\u001b\[36m\s+•\u001b\[0m\u001b\[36m\s#{n}\stests\spending///
failComplete = (x,y) -> ///\u001b\[31m\s\s#{x}\sfailing\u001b\[0m///
describe 'spec', ->
describe 'passing', ->
###
$ ./bin/mocha-phantomjs -R spec test/passing.html
$ mocha -r chai/chai.js -R spec --globals chai.expect test/lib/passing.js
###
before ->
@args = [fileURL('passing')]
it 'returns a passing code', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(code).to.equal 0
it 'writes all output in color', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /Tests Passing/
expect(stdout).to.match passRegExp(1)
expect(stdout).to.match passRegExp(2)
expect(stdout).to.match passRegExp(3)
expect(stdout).to.match skipRegExp(1)
expect(stdout).to.match skipRegExp(2)
expect(stdout).to.match skipRegExp(3)
describe 'failing', ->
###
$ ./bin/mocha-phantomjs -R spec test/failing.html
$ mocha -r chai/chai.js -R spec --globals chai.expect test/lib/failing.js
###
before ->
@args = [fileURL('failing')]
it 'returns a failing code equal to the number of mocha failures', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(code).to.equal 3
it 'writes all output in color', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /Tests Failing/
expect(stdout).to.match passRegExp(1)
expect(stdout).to.match passRegExp(2)
expect(stdout).to.match passRegExp(3)
expect(stdout).to.match failRegExp(1)
expect(stdout).to.match failRegExp(2)
expect(stdout).to.match failRegExp(3)
expect(stdout).to.match failComplete(3,6)
describe 'failing async', ->
###
$ ./bin/mocha-phantomjs -R spec test/failing-async.html
$ mocha -r chai/chai.js -R spec --globals chai.expect test/lib/failing-async.js
###
before ->
@args = [fileURL('failing-async')]
it 'returns a failing code equal to the number of mocha failures', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(code).to.equal 3
it 'writes all output in color', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /Tests Failing/
expect(stdout).to.match passRegExp(1)
expect(stdout).to.match passRegExp(2)
expect(stdout).to.match passRegExp(3)
expect(stdout).to.match failRegExp(1)
expect(stdout).to.match failRegExp(2)
expect(stdout).to.match failRegExp(3)
expect(stdout).to.match failComplete(3,6)
describe 'screenshot', ->
###
$ ./bin/mocha-phantomjs -R spec test/screenshot.html
$ mocha -r chai/chai.js -R spec --globals chai.expect test/lib/screenshot.js
###
before ->
@args = [fileURL('screenshot')]
it 'takes a screenshot into given file, suffixed with .png', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(code).to.equal 0
fileName = "screenshot.png"
expect(fs.existsSync(fileName)).to.equal(true)
fs.unlinkSync(fileName)
describe 'requirejs', ->
before ->
@args = [fileURL('requirejs')]
it 'returns a passing code', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(code).to.equal 0
it 'writes all output in color', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /Tests Passing/
expect(stdout).to.match passRegExp(1)
expect(stdout).to.match passRegExp(2)
expect(stdout).to.match passRegExp(3)
expect(stdout).to.match skipRegExp(1)
expect(stdout).to.match skipRegExp(2)
expect(stdout).to.match skipRegExp(3)
describe 'dot', ->
###
$ ./bin/mocha-phantomjs -R dot test/mixed.html
$ mocha -r chai/chai.js -R dot --globals chai.expect test/lib/mixed.js
###
before ->
@args = ['-R', 'dot', fileURL('mixed')]
it 'uses dot reporter', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /\u001b\[90m\․\u001b\[0m/ # grey
expect(stdout).to.match /\u001b\[36m\․\u001b\[0m/ # cyan
expect(stdout).to.match /\u001b\[31m\․\u001b\[0m/ # red
###
$ ./bin/mocha-phantomjs -R dot test/many.html
$ mocha -r chai/chai.js -R dot --globals chai.expect test/lib/many.js
###
before ->
@args = ['-R', 'dot', fileURL('many')]
it 'wraps lines correctly and has only one double space for the last dot', (done) ->
@runner done, @args, (code, stdout, stderr) ->
matches = stdout.match /\d\dm\․\u001b\[0m(\r\n\r\n|\n\n)/g
expect(matches.length).to.equal 1
describe 'tap', ->
###
$ ./bin/mocha-phantomjs -R tap test/mixed.html
$ mocha -r chai/chai.js -R tap --globals chai.expect test/lib/mixed.js
###
before ->
@args = ['-R', 'tap', fileURL('mixed')]
it 'basically works', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /Tests Mixed/
describe 'list', ->
###
$ ./bin/mocha-phantomjs -R list test/mixed.html
$ mocha -r chai/chai.js -R list --globals chai.expect test/lib/mixed.js
###
before ->
@args = ['-R', 'list', fileURL('mixed')]
it 'basically works', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /Tests Mixed/
describe 'doc', ->
###
$ ./bin/mocha-phantomjs -R doc test/mixed.html
$ mocha -r chai/chai.js -R doc --globals chai.expect test/lib/mixed.js
###
before ->
@args = ['-R', 'doc', fileURL('mixed')]
it 'basically works', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /<h1>Tests Mixed<\/h1>/
describe 'xunit', ->
###
$ ./bin/mocha-phantomjs -R xunit test/mixed.html
$ mocha -r chai/chai.js -R xunit --globals chai.expect test/lib/mixed.js
###
before ->
@args = ['-R', 'xunit', fileURL('mixed')]
it 'basically works', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.match /<testcase classname="Tests Mixed" name="passes 1" time=".*"\/>/
describe 'third party', ->
it 'loads and wraps node-style reporters to run in the browser', (done) ->
@runner done, ['-R', 'test/reporters/3rd-party', fileURL('mixed')], (code, stdout, stderr) ->
expect(stdout).to.match /<section class="suite">/
expect(stdout).to.match /<h1>Tests Mixed<\/h1>/
it 'gives a useful error when trying to require a node module', (done) ->
@runner done, ['-R', 'test/reporters/node-only', fileURL('mixed')], (code, stdout, stderr) ->
expect(stdout).to.match /Node modules cannot be required/
describe 'hooks', ->
###
$ ./bin/mocha-phantomjs -k test/before-start.js test/passing.html
###
describe 'before start', ->
before ->
@args = ['-k', 'test/hooks/before-start.js', fileURL('passing')]
it 'is called', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.contain 'Before start called!'
describe 'after end', ->
###
$ ./bin/mocha-phantomjs -k test/after-end.js test/passing.html
###
before ->
@args = ['-k', 'test/hooks/after-end.js', fileURL('passing')]
it 'is called', (done) ->
@runner done, @args, (code, stdout, stderr) ->
expect(stdout).to.contain 'After end called!'
describe 'config', ->
describe 'user-agent', ->
it 'has the default user agent', (done) ->
@runner done, [fileURL('user-agent')], (code, stdout, stderr) ->
expect(stdout).to.match /PhantomJS\//
it 'has a custom user agent', (done) ->
@runner done, ['-A', 'mochaUserAgent', fileURL('user-agent')], (code, stdout, stderr) ->
expect(stdout).to.match /^mochaUserAgent/
it 'has a custom user agent via setting flag and 2 equal signs', (done) ->
@runner done, ['-s', 'userAgent=mocha=UserAgent', fileURL('user-agent')], (code, stdout, stderr) ->
expect(stdout).to.match /^mocha=UserAgent/
describe 'cookies', ->
it 'has passed cookies', (done) ->
c1Opt = '{"name":"foo","value":"bar"}'
c2Opt = '{"name":"baz","value":"bat","path":"/"}'
@runner done, ['-c', c1Opt, '--cookies', c2Opt, fileURL('cookie')], (code, stdout, stderr) ->
expect(stdout).to.match /foo=bar; baz=bat/
describe 'viewport', ->
it 'has passed cookies', (done) ->
@runner done, ['-v', '123x456', fileURL('viewport')], (code, stdout, stderr) ->
expect(stdout).to.match /123x456/
describe 'no-colors', ->
it 'suppresses color output', (done) ->
@runner done, ['-C', fileURL('mixed')], (code, stdout, stderr) ->
expect(stdout).to.not.match /\u001b\[\d\dm/
it 'suppresses color output plural long form', (done) ->
@runner done, ['--no-colors', fileURL('mixed')], (code, stdout, stderr) ->
expect(stdout).to.not.match /\u001b\[\d\dm/
describe 'bail', ->
it 'should bail on the first error', (done) ->
@runner done, ['-b', fileURL('mixed')], (code, stdout, stderr) ->
expect(stdout).to.match failRegExp 1
describe 'path', ->
it 'has used custom path', (done) ->
@runner done, ['-p', 'fake/path/to/phantomjs', fileURL('passing')], (code, stdout, stderr) ->
expect(stderr).to.contain "PhantomJS does not exist at 'fake/path/to/phantomjs'"
it 'provides a useful error when phantomjs cannot be launched', (done) ->
@runner done, ['-p', 'package.json', fileURL('passing')], (code, stdout, stderr) ->
expect(stderr).to.contain "An error occurred trying to launch phantomjs"
describe 'file', ->
it 'pipes reporter output to a file', (done) ->
@runner done, ['-f', 'reporteroutput.json', '-R', 'json', fileURL('file')], (code, stdout, stderr) ->
expect(stdout).to.contain 'Extraneous'
results = JSON.parse fs.readFileSync 'reporteroutput.json', { encoding: 'utf8' }
expect(results.passes.length).to.equal 6
expect(results.failures.length).to.equal 6
after ->
fs.unlinkSync 'reporteroutput.json'
describe 'env', ->
it 'has passed environment variables', (done) ->
process.env.FOO = 'bar'
@runner done, [fileURL('env')], (code, stdout, stderr) ->
expect(stdout).to.match /^bar/