-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.setup.ts
61 lines (49 loc) · 1.5 KB
/
jest.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
import { Browser, Page } from "puppeteer";
import { getBrowser, closeBrowsers } from "./src/PuppeteerInstance.js";
import { Audit } from "./src/audits/Audit.js";
import { Gatherer } from "./src/gatherers/Gatherer.js";
let browser: Browser | void | null;
jest.mock("./src/audits/esmHelpers");
beforeAll(async () => {
browser = await getBrowser();
});
afterAll(async () => {
await closeBrowsers();
});
export { browser };
export const testAudit = async (
page: Page | null,
auditInstance: Audit | null,
filePath: string,
expectedScore: number,
): Promise<void> => {
let result: Record<string, unknown> = { score: expectedScore === 1 ? 0 : 1 };
if (page && auditInstance) {
//check if is an external url
const url = filePath.startsWith("http") ? filePath : `file://${filePath}`;
await page.goto(url);
await auditInstance.auditPage(page, page.url());
result = await auditInstance.returnGlobal();
}
expect(result?.score).toEqual(expectedScore);
};
export const testGatherer = async (
page: Page | null,
gathererInstance: Gatherer | null,
filePath: string,
expectedScore: number,
): Promise<void> => {
let result: number = 0;
if (page && gathererInstance) {
//check if is an external url
const url = filePath.startsWith("http") ? filePath : `file://${filePath}`;
await page.goto(url);
try {
await gathererInstance.navigateAndFetchPages(url, 1, page);
result = 1;
} catch {
result = 0;
}
}
expect(result).toEqual(expectedScore);
};