-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: add mechanism for running tests in Playwright
This should replace the obsolete way of running tests using Karma. We do not want to run tests in virtual DOM (e.g. Jest), because it is a Potemkin village.
- Loading branch information
Showing
17 changed files
with
3,924 additions
and
2,372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/temp/ | ||
/esm/ | ||
/types/ | ||
/test-results/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v20 | ||
v22 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
}, | ||
"root": false, | ||
"parser": "@typescript-eslint/parser" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { URL } from "node:url"; | ||
import { resolve } from "node:path"; | ||
|
||
const __dirname = new URL(".", import.meta.url).pathname; | ||
|
||
export const TEMP_DIR = resolve(__dirname, "../temp"); | ||
export const LIB_SRC_PATH = resolve(__dirname, "../src/index.ts"); | ||
export const LIB_BUILD_DIR = resolve(TEMP_DIR, "lib"); | ||
export const LIB_BUILD_PATH = resolve(LIB_BUILD_DIR, "index.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Page } from "@playwright/test"; | ||
|
||
export class BlankPage { | ||
constructor(public readonly page: Page) {} | ||
|
||
async setContent(content: string) { | ||
await this.page.setContent(` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<body>${content}</body> | ||
</html> | ||
`); | ||
} | ||
|
||
async body() { | ||
return this.page.locator("body"); | ||
} | ||
|
||
async document(): Promise<Document> { | ||
return this.page.evaluate("document"); | ||
} | ||
|
||
async window() { | ||
return this.page.evaluate("window"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { test as base } from "@playwright/test"; | ||
import { BlankPage } from "./fixture.blank-page.js"; | ||
|
||
type Fixtures = { | ||
blankPage: BlankPage; | ||
}; | ||
|
||
export const test = base.extend<Fixtures>({ | ||
blankPage: async ({ page }, use) => { | ||
const blankPage = new BlankPage(page); | ||
await use(blankPage); | ||
}, | ||
}); | ||
|
||
export { expect } from "@playwright/test"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { FullConfig } from "@playwright/test"; | ||
import { promises as fs } from "node:fs"; | ||
import { LIB_BUILD_DIR, LIB_BUILD_PATH, LIB_SRC_PATH } from "./constants.js"; | ||
import { buildScript } from "./utilities.js"; | ||
|
||
const __dirname = new URL(".", import.meta.url).pathname; | ||
|
||
async function buildLib() { | ||
console.time("build lib"); | ||
await fs.mkdir(LIB_BUILD_DIR, { recursive: true }); | ||
await buildScript({ | ||
srcPath: LIB_SRC_PATH, | ||
buildPath: LIB_BUILD_PATH, | ||
globalName: "CssSelectorGenerator", | ||
}); | ||
console.timeEnd("build lib"); | ||
} | ||
|
||
async function globalSetup(config: FullConfig) { | ||
await buildLib(); | ||
} | ||
|
||
export default globalSetup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
async function globalTeardown() { | ||
console.log("Global teardown"); | ||
} | ||
|
||
export default globalTeardown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineConfig } from "@playwright/test"; | ||
|
||
export default defineConfig({ | ||
globalSetup: "./global-setup.ts", | ||
globalTeardown: "./global-teardown.ts", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { test, expect } from "./fixtures.js"; | ||
import { sanitizeRoot } from "../src/utilities-options.js"; | ||
|
||
test("selector test", async ({ blankPage }) => { | ||
await blankPage.setContent("ahoj"); | ||
const body = await blankPage.body(); | ||
await expect(body).toHaveText("ahoj"); | ||
}); | ||
|
||
test("sanitize root", async ({ blankPage }) => { | ||
await blankPage.page.exposeBinding("sanitizeRoot", sanitizeRoot); | ||
const result = await blankPage.page.evaluate(() => { | ||
const element = document.body.appendChild(document.createElement("div")); | ||
return sanitizeRoot(document, element); | ||
}); | ||
|
||
console.log("result", result); | ||
expect(true).toBe(false); | ||
|
||
/* | ||
const element = root.appendChild(document.createElement("div")); | ||
const result = sanitizeRoot(document, element); | ||
assert.equal(result, document); | ||
*/ | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"module": "Node16" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { BuildOptions } from "esbuild"; | ||
import { build } from "esbuild"; | ||
|
||
export interface BuildScriptProps { | ||
srcPath: string; | ||
buildPath: string; | ||
globalName: string; | ||
buildOptions?: BuildOptions; | ||
} | ||
|
||
// Uses EsBuild, builds a single file script optimized to be injected into the browser environment. | ||
export async function buildScript({ | ||
srcPath, | ||
buildPath, | ||
globalName, | ||
buildOptions = {}, | ||
}: BuildScriptProps) { | ||
// TODO add caching support to prevent unnecessary rebuilds | ||
return await build({ | ||
entryPoints: [srcPath], | ||
outfile: buildPath, | ||
bundle: true, | ||
format: "iife", | ||
globalName: globalName, | ||
platform: "browser", | ||
...buildOptions, | ||
}); | ||
} |
Oops, something went wrong.