Skip to content

Commit a075efc

Browse files
committed
feat: add support for multiple test URLs (for jquery-ui)
1 parent bcd7705 commit a075efc

File tree

5 files changed

+90
-62
lines changed

5 files changed

+90
-62
lines changed

bin/command.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ yargs( process.argv.slice( 2 ) )
5151
"Expected to always start and end with a slash (/). " +
5252
"Defaults to \"/test/\"."
5353
} )
54+
.option( "test-url", {
55+
alias: "t",
56+
type: "array",
57+
description: "URLs to load the tests from. " +
58+
"Can be multiple, but defaults to the base URL."
59+
} )
5460
.option( "flag", {
5561
alias: "f",
5662
type: "array",
@@ -152,7 +158,14 @@ yargs( process.argv.slice( 2 ) )
152158
];
153159
const middleware = await parseMiddleware( config, argv );
154160

155-
return run( { ...config, ...argv, flag, isolatedFlag, middleware } );
161+
return run( {
162+
...config,
163+
testUrl: config.testUrls,
164+
...argv,
165+
flag,
166+
isolatedFlag,
167+
middleware
168+
} );
156169
}
157170
} )
158171
.command( {

createTestServer.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,13 @@ import { readFile, stat } from "node:fs/promises";
33
import { createReadStream } from "node:fs";
44
import getRawBody from "raw-body";
55

6-
7-
function urlWithIndex( baseUrl ) {
8-
let url = baseUrl;
9-
if ( !url.startsWith( "/" ) ) {
10-
url = `/${ url }`;
11-
}
12-
if ( !url.endsWith( "/index.html" ) ) {
13-
url = `${ url }/index.html`;
14-
}
15-
return url;
16-
}
17-
186
export async function createTestServer( {
197
baseUrl, // Expected to always end in /
208
report,
219
middleware: userMiddleware = [],
22-
quiet
10+
quiet,
11+
testUrls = []
2312
} = {} ) {
24-
const urlWithoutSlash = baseUrl.slice( 0, -1 );
25-
const indexUrl = urlWithIndex( urlWithoutSlash );
26-
27-
// Get the index HTML ahead-of-time,
28-
// to which we will add the QUnit listener script.
29-
const indexHTML = await readFile( `.${ indexUrl }`, "utf8" );
3013

3114
// Support connect-style middleware
3215
const middlewares = [];
@@ -94,20 +77,24 @@ export async function createTestServer( {
9477

9578
// Redirect to trailing slash
9679
use( ( req, res, next ) => {
97-
if ( req.parsedUrl.pathname === urlWithoutSlash ) {
80+
if ( req.parsedUrl.pathname === baseUrl.replace( /\/$/, "" ) ) {
9881
res.redirect( 308, `${ req.parsedUrl.pathname }/${ req.parsedUrl.search }` );
9982
} else {
10083
next();
10184
}
10285
} );
10386

10487
// Add a script tag to the index.html to load the QUnit listeners
105-
use( ( req, res, next ) => {
88+
use( async( req, res, next ) => {
89+
const pathname = req.parsedUrl.pathname;
10690
if (
10791
( req.method === "GET" || req.method === "HEAD" ) &&
108-
( req.parsedUrl.pathname === baseUrl ||
109-
req.parsedUrl.pathname === indexUrl )
92+
( pathname === baseUrl || testUrls.includes( pathname.replace( baseUrl, "" ) ) )
11093
) {
94+
const indexHTML = await readFile(
95+
`.${ pathname }${ pathname.endsWith( ".html" ) ? "" : "index.html" }`,
96+
"utf8"
97+
);
11198
res.writeHead( 200, { "Content-Type": "text/html" } );
11299
res.end(
113100
indexHTML.replace(

lib/buildTestUrl.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export function buildTestUrl( {
77
isolatedFlag,
88
jsdom,
99
port,
10-
reportId
10+
reportId,
11+
testUrl = ""
1112
} ) {
1213
if ( !port ) {
1314
throw new Error( "No port specified." );
@@ -37,5 +38,5 @@ export function buildTestUrl( {
3738
// BrowserStack supplies a custom domain for local testing,
3839
// which is especially necessary for iOS testing.
3940
const host = browserstack ? "bs-local.com" : "localhost";
40-
return `http://${ host }:${ port }${ baseUrl }?${ query }`;
41+
return `http://${ host }:${ port }${ baseUrl }${ testUrl }?${ query }`;
4142
}

listeners.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
( function() {
2-
32
"use strict";
43

54
// Get the report ID from the URL.
@@ -82,29 +81,39 @@
8281
return request;
8382
}
8483

85-
// Send acknowledgement to the server.
86-
send( "ack" );
87-
88-
QUnit.on( "testEnd", function( data ) {
89-
send( "testEnd", data );
90-
} );
84+
function listen( QUnit ) {
9185

92-
QUnit.on( "runEnd", function( data ) {
86+
// Send acknowledgement to the server.
87+
send( "ack" );
9388

94-
// Reduce the payload size.
95-
// childSuites is large and unused.
96-
data.childSuites = undefined;
89+
QUnit.on( "testEnd", function( data ) {
90+
send( "testEnd", data );
91+
} );
9792

98-
var request = send( "runEnd", data );
99-
request.onload = function() {
100-
if ( request.status === 200 && request.responseText ) {
101-
try {
102-
var json = JSON.parse( request.responseText );
103-
window.location = json.url;
104-
} catch ( e ) {
105-
console.error( e );
93+
QUnit.on( "runEnd", function( data ) {
94+
95+
// Reduce the payload size.
96+
// childSuites is large and unused.
97+
data.childSuites = undefined;
98+
99+
var request = send( "runEnd", data );
100+
request.onload = function() {
101+
if ( request.status === 200 && request.responseText ) {
102+
try {
103+
var json = JSON.parse( request.responseText );
104+
window.location = json.url;
105+
} catch ( e ) {
106+
console.error( e );
107+
}
106108
}
107-
}
108-
};
109-
} );
109+
};
110+
} );
111+
}
112+
113+
// jquery-ui loads QUnit asynchronously
114+
if ( typeof QUnit === "undefined" && typeof require !== "undefined" ) {
115+
require( [ "qunit" ], listen );
116+
} else {
117+
listen( QUnit );
118+
}
110119
} )();

run.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export async function run( {
3333
middleware = [],
3434
retries = 0,
3535
runId,
36+
testUrl: testUrls = [],
3637
verbose
3738
} ) {
3839
if ( !browserNames.length ) {
@@ -76,6 +77,7 @@ export async function run( {
7677
baseUrl,
7778
middleware,
7879
quiet: !verbose,
80+
testUrls,
7981
report: async( message ) => {
8082
switch ( message.type ) {
8183
case "testEnd": {
@@ -107,10 +109,7 @@ export async function run( {
107109
const reportId = message.id;
108110
const report = reports[ reportId ];
109111
touchBrowser( report.browser );
110-
const { failed, total } = reportEnd(
111-
message.data,
112-
reports[ reportId ]
113-
);
112+
const { failed, total } = reportEnd( message.data, reports[ reportId ] );
114113
report.total = total;
115114

116115
// Handle failure
@@ -243,10 +242,19 @@ export async function run( {
243242
}
244243
}
245244

246-
function queueRun( browser, isolatedFlag ) {
245+
function queueRun( browser, { isolatedFlag, testUrl } = {} ) {
247246
const fullBrowser = getBrowserString( browser, headless );
248-
const reportId = generateHash( `${ hashValue }-${ isolatedFlag }-${ fullBrowser }` );
249-
reports[ reportId ] = { browser, flags, headless, id: reportId, isolatedFlag };
247+
const reportId = generateHash(
248+
`${ hashValue }-${ isolatedFlag }-${ testUrl }-${ fullBrowser }`
249+
);
250+
reports[ reportId ] = {
251+
browser,
252+
flags,
253+
headless,
254+
id: reportId,
255+
isolatedFlag,
256+
testUrl
257+
};
250258

251259
const url = buildTestUrl( {
252260
baseUrl,
@@ -255,7 +263,8 @@ export async function run( {
255263
isolatedFlag,
256264
jsdom: browser.browser === "jsdom",
257265
port,
258-
reportId
266+
reportId,
267+
testUrl
259268
} );
260269

261270
const options = {
@@ -268,6 +277,7 @@ export async function run( {
268277
isolatedFlag,
269278
reportId,
270279
runId,
280+
testUrl,
271281
tunnelId,
272282
verbose
273283
};
@@ -278,7 +288,17 @@ export async function run( {
278288
for ( const browser of browsers ) {
279289
if ( isolatedFlags.length > 0 ) {
280290
isolatedFlags.forEach( ( isolatedFlag ) => {
281-
queueRun( browser, isolatedFlag );
291+
if ( testUrls.length > 0 ) {
292+
testUrls.forEach( ( testUrl ) => {
293+
queueRun( browser, { isolatedFlag, testUrl } );
294+
} );
295+
} else {
296+
queueRun( browser, { isolatedFlag } );
297+
}
298+
} );
299+
} else if ( testUrls.length > 0 ) {
300+
testUrls.forEach( ( testUrl ) => {
301+
queueRun( browser, { testUrl } );
282302
} );
283303
} else {
284304
queueRun( browser );
@@ -306,11 +326,9 @@ export async function run( {
306326
];
307327
console.error(
308328
chalk.red(
309-
`No tests were run for page with flags "${
310-
reportFlags.join( "&" )
311-
}" in ${
312-
getBrowserString( report.browser )
313-
} (${ report.id })`
329+
`No tests were run for page with flags "${ reportFlags.join(
330+
"&"
331+
) }" in ${ getBrowserString( report.browser ) } (${ report.id })`
314332
)
315333
);
316334
}

0 commit comments

Comments
 (0)