Skip to content

Commit c3405fe

Browse files
committed
add test harness (from #97)
1 parent aee78a4 commit c3405fe

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

scripts/test-harness.mjs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import puppeteer from "puppeteer";
2+
import Webpack from "webpack";
3+
import { fs } from "memfs";
4+
5+
import webpackConfig from "../webpack.config.js";
6+
7+
/**
8+
* Validate a URL
9+
* @param {URL} url
10+
* @returns {boolean}
11+
*/
12+
const validateUrl = (url) => {
13+
try {
14+
return new URL(url);
15+
} catch (_e) {
16+
return false;
17+
}
18+
};
19+
20+
if (!process.argv[2]) {
21+
console.error("Usage: yarn test '<url>'");
22+
process.exit(1);
23+
}
24+
25+
if (!validateUrl(process.argv[2])) {
26+
console.error("Invalid URL (hint: include http:// or https://)");
27+
process.exit(1);
28+
}
29+
30+
const config = webpackConfig({}, { mode: "development" });
31+
32+
const compiler = Webpack(config);
33+
compiler.outputFileSystem = fs;
34+
35+
const browser = await puppeteer.launch({ headless: false, devtools: true });
36+
const page = await browser.newPage();
37+
38+
const _watching = compiler.watch({}, async (err, stats) => {
39+
if (err) {
40+
console.error(err);
41+
console.error("Not opening browser");
42+
return;
43+
}
44+
console.log(
45+
stats.toString({
46+
colors: true,
47+
preset: "summary",
48+
}),
49+
);
50+
const behaviorScript = fs.readFileSync("dist/behaviors.js", "utf8");
51+
52+
await page.goto(validateUrl(process.argv[2]));
53+
54+
await page.evaluate(
55+
behaviorScript +
56+
`
57+
self.__bx_behaviors.init({
58+
autofetch: true,
59+
autoplay: true,
60+
autoscroll: true,
61+
siteSpecific: true,
62+
});
63+
`,
64+
);
65+
66+
// call and await run on top frame and all child iframes
67+
await Promise.allSettled(
68+
page
69+
.frames()
70+
.map(async (frame) => frame.evaluate("self.__bx_behaviors.run()")),
71+
);
72+
});

0 commit comments

Comments
 (0)