-
Notifications
You must be signed in to change notification settings - Fork 4
/
puppeteerQUnit.js
126 lines (104 loc) · 3.74 KB
/
puppeteerQUnit.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
/* eslint-disable */
// Adapted from https://github.com/davidtaylorhq/qunit-puppeteer which is distributed under the MIT License // eslint-disable-line
/* eslint-enable */
'use strict';
module.exports = function( browser, targetURL ) {
return new Promise( async function( resolve, reject ) {
const page = await browser.newPage();
let ended = false;
const end = async function( result ) {
if ( !ended ) {
ended = true;
await page.close();
resolve( result );
}
};
page.on( 'error', msg => end( { ok: false, result: 'error', message: msg } ) );
page.on( 'pageerror', msg => end( { ok: false, result: 'pageerror', message: msg } ) );
const moduleErrors = [];
let testErrors = [];
let assertionErrors = [];
await page.exposeFunction( 'harness_moduleDone', context => {
if ( context.failed ) {
const msg = 'Module Failed: ' + context.name + '\n' + testErrors.join( '\n' );
moduleErrors.push( msg );
testErrors = [];
}
} );
await page.exposeFunction( 'harness_testDone', context => {
if ( context.failed ) {
const msg = ' Test Failed: ' + context.name + assertionErrors.join( ' ' );
testErrors.push( msg );
assertionErrors = [];
process.stdout.write( 'F' );
}
else {
// process.stdout.write( '.' );
}
} );
await page.exposeFunction( 'harness_log', ( passed, message, source ) => {
if ( passed ) { return; } // If success don't log
let msg = '\n Assertion Failed:';
if ( message ) {
msg += ' ' + message;
}
if ( source ) {
msg += '\n\n' + source;
}
assertionErrors.push( msg );
} );
await page.exposeFunction( 'harness_done', async function( context ) {
// console.log( '\n' );
if ( moduleErrors.length > 0 ) {
for ( let idx = 0; idx < moduleErrors.length; idx++ ) {
console.error( moduleErrors[ idx ] + '\n' );
}
}
end( {
ok: context.passed === context.total,
time: context.runtime,
totalTests: context.total,
passed: context.passed,
failed: context.failed,
errors: moduleErrors
} );
} );
try {
if ( targetURL.indexOf( '?' ) === -1 ) {
throw new Error( 'URL should have query parameters' );
}
await page.goto( `${targetURL}&qunitHooks` );
await page.evaluate( () => {
const launch = () => {
QUnit.config.testTimeout = 10000;
// Cannot pass the window.harness_blah methods directly, because they are
// automatically defined as async methods, which QUnit does not support
QUnit.moduleDone( context => window.harness_moduleDone( context ) );
QUnit.testDone( context => window.harness_testDone( context ) );
// This context could contain objects that can't be sent over to harness_log, so just take the parts we need.
QUnit.log( context => window.harness_log( context.result, context.message, context.source ) );
QUnit.done( context => window.harness_done( context ) );
// Launch the qunit tests now that listeners are wired up
window.qunitLaunchAfterHooks();
};
// Start right away if the page is ready
if ( window.qunitLaunchAfterHooks ) {
launch();
}
else {
// Polling to wait until the page is ready for launch
let id = null;
id = setInterval( () => {
if ( window.qunitLaunchAfterHooks ) {
clearInterval( id );
launch();
}
}, 16 );
}
} );
}
catch( e ) {
end( { ok: false, message: 'caught exception ' + e } );
}
} );
};