-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal-setup.ts
97 lines (82 loc) · 2.76 KB
/
global-setup.ts
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
import { chromium, Page, type FullConfig } from "@playwright/test";
import Mustache from "mustache";
import path from "node:path";
import fs from "node:fs/promises";
import { PBUObject, PBU_OBJECT_KEY } from "./common";
import { PBUOptions, SourceMap } from ".";
const getTestTitles = async (page: Page) => {
return await page.evaluate((PBU_OBJECT_KEY: string) => {
const PBUObj = (window as any)[PBU_OBJECT_KEY];
if (!(PBUObj?.constructor.name === "PBUObject")) {
return [];
}
return Array.from((PBUObj as PBUObject).tests.keys());
}, PBU_OBJECT_KEY);
};
const getSourceMaps = async (page: Page, sourceMaps: SourceMap[]) => {
const client = await page.context().newCDPSession(page);
await client.send("Debugger.enable");
client.on("Debugger.scriptParsed", (payload) => {
if (
payload.url &&
!payload.url.includes("/node_modules/") &&
payload.sourceMapURL
) {
sourceMaps.push({
url: payload.url,
sourceMapUrl: payload.sourceMapURL,
});
}
});
};
const generateBrowserUnitTestFile = async (
templateFile: string,
testFile: string,
view: any
) => {
const header = `/*
This file was automatically generated by "playwright-browser-unit".
Any modifications made to this file may be overwritten.
*/`;
const template = await fs.readFile(templateFile, "utf8");
const output = Mustache.render(template, view);
await fs.writeFile(testFile, `${header}\n${output}`);
};
const getTestFile = (config: FullConfig<PBUOptions>) => {
const testFile =
config.projects[0].use.pbuTestFile || "browser-unit.test.ts";
return path.isAbsolute(testFile)
? testFile
: path.resolve(config.projects[0].testDir, testFile);
};
const getPBUOptions = (config: FullConfig<PBUOptions>): PBUOptions => {
return {
pbuURL: config.projects[0].use.pbuURL || "http://localhost:3000",
pbuTestFile: getTestFile(config),
};
};
async function globalSetup(config: FullConfig<PBUOptions>) {
// get playwright-browser-unit options with default values
const pbuOptions = getPBUOptions(config);
const browser = await chromium.launch();
const page = await browser.newPage();
// source maps are stored in this variable when pbuURL is loaded
const sourceMaps: SourceMap[] = [];
getSourceMaps(page, sourceMaps);
await page.goto(pbuOptions.pbuURL);
// get the test titles from the PBUObject in the page enviroment
const testTitles = await getTestTitles(page);
// view used for expanding the mustache template
const view = {
testTitles,
sourceMaps,
pbuOptions,
};
// reads the template, render it, and save the test file
await generateBrowserUnitTestFile(
path.join(__dirname, "browser-unit.test.in"),
pbuOptions.pbuTestFile,
view
);
}
export default globalSetup;