jquery/test/runner/createTestServer.js
Timmy Willison 95a4c94b81
Tests: reuse browser workers in BrowserStack tests (#5428)
- reuse BrowserStack workers.
- add support for "latest" and "latest-1" in browser version filters
- add support for specifying non-final browser versions, such as beta versions
- more accurate eslint for files in test/runner
- switched `--no-isolate` command flag to `--isolate`. Now that browser instances are shared, it made more sense to me to default to no isolation unless specified. This turned out to be cleaner because the only place we isolate is in browserstack.yml.
- fixed an issue with retries where it wasn't always waiting for the retried test run
- enable strict mode in test yargs command
2024-03-05 14:44:01 -05:00

65 lines
1.7 KiB
JavaScript

import bodyParser from "body-parser";
import express from "express";
import bodyParserErrorHandler from "express-body-parser-error-handler";
import fs from "fs";
import mockServer from "../middleware-mockserver.cjs";
export async function createTestServer( report ) {
const nameHTML = await fs.promises.readFile( "./test/data/name.html", "utf8" );
const indexHTML = await fs.promises.readFile( "./test/index.html", "utf8" );
const app = express();
// Redirect home to test page
app.get( "/", ( _req, res ) => {
res.redirect( "/test/" );
} );
// Redirect to trailing slash
app.use( ( req, res, next ) => {
if ( req.path === "/test" ) {
const query = req.url.slice( req.path.length );
res.redirect( 301, `${ req.path }/${ query }` );
} else {
next();
}
} );
// Add a script tag to the index.html to load the QUnit listeners
app.use( /\/test(?:\/index.html)?\//, ( _req, res ) => {
res.send( indexHTML.replace(
"</head>",
"<script src=\"/test/runner/listeners.js\"></script></head>"
) );
} );
// Bind the reporter
app.post( "/api/report", bodyParser.json( { limit: "50mb" } ), ( req, res ) => {
if ( report ) {
const response = report( req.body );
if ( response ) {
res.json( response );
return;
}
}
res.sendStatus( 204 );
} );
// Handle errors from the body parser
app.use( bodyParserErrorHandler() );
// Hook up mock server
app.use( mockServer() );
// Serve static files
app.post( "/test/data/name.html", ( _req, res ) => {
res.send( nameHTML );
} );
app.use( "/dist", express.static( "dist" ) );
app.use( "/src", express.static( "src" ) );
app.use( "/test", express.static( "test" ) );
app.use( "/external", express.static( "external" ) );
return app;
}