Skip to content

Commit b79fa78

Browse files
committed
Programmatically start server from compat test scripts
EXTRA: If tests fail return an error code.
1 parent 94025cb commit b79fa78

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

benchmarks/compat/tests/browserstack.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import browserstack from "browserstack-local";
66
import webdriver from "selenium-webdriver";
77

8-
import { runWebTest, fastSelenium } from "./utils.js"
8+
import { runWebTest, fastSelenium, App } from "./utils.js"
9+
10+
const app = new App();
11+
app.start();
912

1013
fastSelenium();
1114

@@ -76,6 +79,8 @@ for (const browser of BROWSERS) {
7679
}
7780
}
7881

82+
app.stop();
83+
7984
// Stop BrowserStack.Local.
8085
await new Promise(resolve => {
8186
bs.stop(

benchmarks/compat/tests/local.test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import webdriver from "selenium-webdriver"
66
import { Capabilities } from "selenium-webdriver";
77

8-
import { runWebTest } from "./utils.js"
8+
import { App, runWebTest } from "./utils.js"
9+
10+
const app = new App();
11+
await app.start();
912

1013
await (async function() {
1114
let driver;
@@ -17,9 +20,11 @@ await (async function() {
1720

1821
await runWebTest(driver);
1922
} catch(e) {
20-
// We just catch this so that even if the test fails we quit the driver.
21-
// Either way there will be a log saying if the test failed.
23+
// Make sure the process exits with an error code
24+
process.exitCode = 1;
2225
}
2326

2427
driver.quit();
2528
}());
29+
30+
await app.stop();

benchmarks/compat/tests/utils.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,48 @@
44

55
import http from "http";
66
import https from "https";
7+
import path, { dirname } from "path";
8+
import { fileURLToPath } from "url";
79

810
import { By, until } from "selenium-webdriver";
11+
import webpack from "webpack";
12+
import DevServer from "webpack-dev-server";
13+
14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = dirname(__filename);
16+
17+
const PORT = 3000;
18+
19+
export class App {
20+
constructor() {
21+
const compiler = webpack({
22+
mode: "development",
23+
entry: path.join(__dirname, "../index.js")
24+
});
25+
26+
this.server = new DevServer({
27+
static: {
28+
directory: path.join(__dirname, "../"),
29+
},
30+
port: 3000,
31+
}, compiler);
32+
}
33+
34+
async start() {
35+
console.log("Starting server...");
36+
await this.server.start();
37+
}
38+
39+
async stop() {
40+
console.log("Stopping server...");
41+
await this.server.stop();
42+
}
43+
}
944

1045
/**
1146
* Executes a smoke test for @mozilla/glean/web.
1247
*
13-
* This assumes a server is running on localhost:8081 with the sample page.
48+
* This will execute the sample app in port 3000.
1449
*
1550
* @param {*} driver A selenium webdriver.
1651
*/
@@ -19,18 +54,18 @@ export async function runWebTest(driver) {
1954
// Loading the sample webpage will record a metric and submit a ping.
2055
// If the ping is submitted succesfully an element in the DOM with the id `msg`
2156
// will receive the text "Ping submitted succesfully."
22-
await driver.get("http://localhost:8081");
57+
await driver.get(`http://localasdfhost:${PORT}/`);
2358
// Give it time to send the ping request.
2459
const successTextContainer = await driver.findElement(By.id("msg"));
2560
await driver.wait(
2661
until.elementTextIs(
2762
successTextContainer,
2863
"Ping submitted succesfully."
29-
), 5000);
64+
), 60* 5000);
3065

3166
console.log("Test passed.");
3267
} catch(e) {
33-
console.log("Test failed.");
68+
console.log("Test failed.", e);
3469
// Bubble error up to calling function.
3570
throw e;
3671
}

benchmarks/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
"size:docs:dry": "DRY_RUN=1 node --experimental-json-modules size/docs.js",
1414
"size:docs": "node --experimental-json-modules size/docs.js",
1515
"compat:glean": "glean translate compat/metrics.yaml compat/pings.yaml -f javascript -o compat/generated",
16-
"compat:build": "npm run compat:glean && webpack ./compat/index.js --output-path=./compat/dist --output-filename=index.js --mode=development",
17-
"compat:dev": "npm run compat:build && webpack serve ./compat/index.js --static-directory=./compat --mode=development --open ",
18-
"compat:test": "node --experimental-json-modules compat/tests/local.test.js",
19-
"compat:test:browserstack": "node --experimental-json-modules compat/tests/browserstack.test.js"
16+
"compat:test": "npm run compat:glean && node --experimental-json-modules compat/tests/local.test.js",
17+
"compat:test:browserstack": "npm run compat:glean && node --experimental-json-modules compat/tests/browserstack.test.js"
2018
},
2119
"author": "The Glean Team <glean-team@mozilla.com>",
2220
"license": "MPL-2.0",

0 commit comments

Comments
 (0)