-
Notifications
You must be signed in to change notification settings - Fork 617
feat: add Playwright test harness with SSR and CSR support #7428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
radium-v
wants to merge
10
commits into
main
Choose a base branch
from
users/radium-v/fast-test-harness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a38157
feat: add Playwright test harness with SSR and CSR support
radium-v 3f396dc
update README to match fast-element
radium-v 2788ed9
validate testId and improve error handling
radium-v 38dac90
fix: update regex in processDsdTemplate for attribute handling
radium-v c552d2d
remove createWasmRenderer and update exports in README
radium-v 10b3d61
regex fix
radium-v 9c66db7
avoid regex backtracking for attribute handling
radium-v 05fe98f
use realTempDir for file path resolution in generate-fixture
radium-v 97dbff1
fix: hash testId for generated filename in generate-fixture
radium-v 130f47f
update fixture generation to use testId directly and implement cachinβ¦
radium-v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,218 @@ | ||
| # FAST Test Harness | ||
|
|
||
| [](https://opensource.org/licenses/MIT) | ||
|
|
||
| The `fast-test-harness` package is a Playwright testing harness for FAST Element web components with CSR and SSR support. | ||
|
|
||
| ## Installation | ||
|
|
||
| To install `fast-test-harness` using `npm`: | ||
|
|
||
| ```shell | ||
| npm install --save-dev @microsoft/fast-test-harness | ||
| ``` | ||
|
|
||
| ## Writing tests | ||
|
|
||
| Import `test` and `expect` from the harness. Configure the component tag name with `test.use()`, then call `fastPage.setTemplate()` in each test to render it. | ||
|
|
||
| ```ts | ||
| import { expect, test } from "@microsoft/fast-test-harness"; | ||
|
|
||
| test.describe("Button", () => { | ||
| test.use({ tagName: "my-button", innerHTML: "Click me" }); | ||
|
|
||
| test("should render", async ({ fastPage }) => { | ||
janechu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await fastPage.setTemplate(); | ||
| await expect(fastPage.element).toBeVisible(); | ||
| }); | ||
|
|
||
| test("should accept attributes", async ({ fastPage }) => { | ||
| await fastPage.setTemplate({ | ||
| attributes: { appearance: "primary", disabled: true }, | ||
| }); | ||
| await expect(fastPage.element).toHaveAttribute("appearance", "primary"); | ||
| }); | ||
|
|
||
| test("should work inside a form", async ({ fastPage }) => { | ||
| await fastPage.setTemplate(` | ||
| <form> | ||
| <my-button type="submit">Submit</my-button> | ||
| </form> | ||
| `); | ||
| await expect(fastPage.element).toBeVisible(); | ||
| }); | ||
| }); | ||
| ``` | ||
|
|
||
| Use `updateTemplate()` to modify attributes or innerHTML after the initial render without navigating away from the page: | ||
|
|
||
| ```ts | ||
| await fastPage.setTemplate(); | ||
| await fastPage.updateTemplate(fastPage.element, { attributes: { disabled: true } }); | ||
| ``` | ||
|
|
||
| The `toHaveCustomState` assertion checks `ElementInternals` custom states: | ||
|
|
||
| ```ts | ||
| await expect(element).toHaveCustomState("checked"); | ||
| ``` | ||
|
|
||
| ## Fixture options | ||
|
|
||
| | Option | Type | Default | Description | | ||
| |--------|------|---------|-------------| | ||
| | `tagName` | `string` | `""` | Custom element tag name | | ||
| | `innerHTML` | `string` | `""` | Default inner HTML | | ||
| | `waitFor` | `string[]` | `[]` | Additional elements to wait for before testing | | ||
| | `ssr` | `boolean` | `false` | Use SSR mode (or set `PLAYWRIGHT_TEST_SSR=true`) | | ||
|
|
||
| ## Test directory setup | ||
|
|
||
| The harness serves a Vite dev server from a `test/` directory in your project. CSR and SSR modes use different entry points from the same directory. | ||
|
|
||
| ``` | ||
| test/ | ||
| βββ index.html # CSR: loads main.ts | ||
| βββ ssr.html # SSR: template with comment placeholders | ||
| βββ vite.config.ts # Vite config (shared by both modes) | ||
| βββ src/ | ||
| βββ main.ts # CSR: registers components, applies theme | ||
| βββ entry-client.ts # SSR: registers components for hydration | ||
| βββ entry-server.ts # SSR: exports render() for fixture generation | ||
| ``` | ||
|
|
||
| ### CSR files | ||
|
|
||
| **`index.html`** loads a script that registers your components: | ||
|
|
||
| ```html | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head><meta charset="UTF-8" /></head> | ||
| <body> | ||
| <script type="module" src="/src/main.ts"></script> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
| **`main.ts`** registers components and applies global config. The body starts empty; `setTemplate()` injects HTML per test. | ||
|
|
||
| ```ts | ||
| import "./define-all.js"; | ||
| import { setTheme } from "./theme.js"; | ||
| setTheme(lightTheme); | ||
| ``` | ||
|
|
||
| ### SSR files | ||
|
|
||
| **`ssr.html`** contains comment placeholders the server fills in per request: | ||
|
|
||
| ```html | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title><!--fixturetitle--></title> | ||
| <!--stylespreload--> | ||
| </head> | ||
| <body> | ||
| <!--fixture--> | ||
| <!--templates--> | ||
| <script type="module" src="/src/entry-client.ts"></script> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
| **`entry-client.ts`** registers components for DSD hydration using `defineAsync`: | ||
|
|
||
| ```ts | ||
| import { TemplateElement } from "@microsoft/fast-html"; | ||
| TemplateElement.define({ name: "f-template" }); | ||
|
|
||
| // Load all define-async modules | ||
| const modules = import.meta.glob("../../src/*/define-async.{ts,js}"); | ||
| Promise.all(Object.values(modules).map(m => m())); | ||
| ``` | ||
|
|
||
| **`entry-server.ts`** exports a `render()` function that the server calls for each `setTemplate()` request. It returns three strings that get injected into `ssr.html`: | ||
|
|
||
| ```ts | ||
| export function render(queryObj: Record<string, string>): { | ||
| template: string; // <f-template> HTML β <!--templates--> | ||
| fixture: string; // rendered element HTML β <!--fixture--> | ||
| preloadLinks: string; // <link> tags β <!--stylespreload--> | ||
| }; | ||
| ``` | ||
|
|
||
| Each component needs three build artifacts for SSR: an `<f-template>` (`.template.html`), a DSD template (`.template-dsd.html`), and optionally a stylesheet (`.styles.css`). Use `renderFixture` and `renderTemplate` from the harness to assemble the output: | ||
|
|
||
| ```ts | ||
| import { readAsset, resolveAssetUrl } from "@microsoft/fast-test-harness/ssr/assets.js"; | ||
| import { renderFixture, renderTemplate } from "@microsoft/fast-test-harness/ssr/render.js"; | ||
|
|
||
| const fTemplate = readAsset("@my-scope/button/template.html"); | ||
| const dsd = readAsset("@my-scope/button/template-dsd.html"); | ||
| const styles = resolveAssetUrl("@my-scope/button/styles.css"); | ||
|
|
||
| export function render(queryObj: Record<string, string> = {}) { | ||
| return { | ||
| template: renderTemplate(fTemplate, styles), | ||
| fixture: renderFixture(queryObj, dsd, styles), | ||
| preloadLinks: "", | ||
| }; | ||
| } | ||
| ``` | ||
|
|
||
| ## Server | ||
|
|
||
| The package includes an Express + Vite server that handles both CSR page serving and SSR fixture generation. Run it directly or let Playwright manage it via `webServer`: | ||
|
|
||
| ```ts | ||
| // playwright.config.ts | ||
| export default defineConfig({ | ||
| webServer: { | ||
| command: "fast-test-harness", | ||
| port: 5173, | ||
| reuseExistingServer: true, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| For custom setup, import `startServer`: | ||
|
|
||
| ```ts | ||
| import { startServer } from "@microsoft/fast-test-harness/server.mjs"; | ||
| await startServer(process.cwd(), "./test", "./test/vite.config.ts"); | ||
| ``` | ||
|
|
||
| | Parameter | Default | Description | | ||
| |-----------|---------|-------------| | ||
| | `cwd` | `process.cwd()` | Static file serving root | | ||
| | `root` | `<cwd>/test` | Vite root (contains `index.html`, `ssr.html`) | | ||
| | `configFile` | `<root>/vite.config.ts` | Vite config path | | ||
|
|
||
| | Environment variable | Default | Description | | ||
| |---------------------|---------|-------------| | ||
| | `PORT` | `5173` | Server port | | ||
| | `BASE` | `/` | Base URL path | | ||
| | `PLAYWRIGHT_TEST_SSR` | β | Set `"true"` for SSR mode | | ||
|
|
||
| ## Rendering utilities | ||
|
|
||
| **`renderFixture(queryObj, dsdTemplate?, styles?, templateData?, childTemplates?)`** builds the fixture element HTML. Injects the DSD template inside the element when provided. `childTemplates` is a `Record<tagName, dsdHtml>` that injects DSD into nested custom elements found in the innerHTML or raw HTML. | ||
|
|
||
| **`renderTemplate(rawTemplate, styles)`** replaces `{{styles}}` in an f-template HTML string with a `<link>` tag for the given stylesheet URL. | ||
|
|
||
| **`readAsset(specifier)`** reads a file as UTF-8 from a package export path or filesystem path using `import.meta.resolve`. | ||
|
|
||
| **`resolveAssetUrl(specifier, root?)`** resolves a specifier to a server-relative URL path for use in `<link>` tags. | ||
|
|
||
| ## Exports | ||
|
|
||
| | Specifier | Contents | | ||
| |-----------|----------| | ||
| | `@microsoft/fast-test-harness` | `test`, `expect`, `CSRFixture`, `SSRFixture`, `readAsset`, `resolveAssetUrl`, `renderFixture`, `renderTemplate` | | ||
| | `@microsoft/fast-test-harness/server.mjs` | `startServer`, `app` | | ||
| | `@microsoft/fast-test-harness/ssr/render.js` | `renderFixture`, `renderTemplate`, `renderPreloadLinks` | | ||
| | `@microsoft/fast-test-harness/ssr/assets.js` | `readAsset`, `resolveAssetUrl` | | ||
| | `@microsoft/fast-test-harness/public/*` | Static assets (base CSS) | | ||
This file contains hidden or 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,71 @@ | ||
| { | ||
| "name": "@microsoft/fast-test-harness", | ||
| "private": true, | ||
| "version": "0.0.1", | ||
| "author": { | ||
| "name": "Microsoft", | ||
| "url": "https://discord.gg/FcSNfg4" | ||
| }, | ||
| "description": "Playwright testing harness for FAST web components", | ||
| "license": "MIT", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/Microsoft/fast.git", | ||
| "directory": "packages/fast-test-harness" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/Microsoft/fast/issues/new/choose" | ||
| }, | ||
| "type": "module", | ||
| "bin": { | ||
| "fast-test-harness": "./start.mjs" | ||
| }, | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/dts/index.d.ts", | ||
| "test": "./src/index.ts", | ||
| "default": "./dist/esm/index.js" | ||
| }, | ||
| "./vite.config.mjs": { | ||
| "default": "./vite.config.mjs" | ||
| }, | ||
| "./ssr/*.js": { | ||
| "types": "./dist/dts/ssr/*.d.ts", | ||
| "test": "./src/ssr/*.ts", | ||
| "default": "./dist/esm/ssr/*.js" | ||
| }, | ||
| "./playwright.config.ts": "./playwright.config.ts", | ||
| "./public/*": "./public/*", | ||
| "./server.mjs": "./server.mjs", | ||
| "./start.mjs": "./start.mjs", | ||
| "./package.json": "./package.json" | ||
| }, | ||
| "scripts": { | ||
| "build": "npm run build:tsc", | ||
| "build:tsc": "tsc -p tsconfig.build.json" | ||
| }, | ||
| "dependencies": { | ||
| "express": "5.2.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@microsoft/fast-html": "*", | ||
| "@microsoft/fast-build": "*" | ||
| }, | ||
| "peerDependencies": { | ||
| "@microsoft/fast-build": ">=0.3.0", | ||
| "@microsoft/fast-html": ">= 1.0.0-alpha.46 || ^1.0.0", | ||
| "@playwright/test": ">=1.40.0", | ||
| "vite": ">=7.0.0" | ||
| }, | ||
| "peerDependenciesMeta": { | ||
| "@microsoft/fast-build": { | ||
| "optional": true | ||
| }, | ||
| "@microsoft/fast-html": { | ||
| "optional": true | ||
| } | ||
| }, | ||
| "engines": { | ||
| "node": ">=22.18.0" | ||
| } | ||
| } |
This file contains hidden or 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,31 @@ | ||
| import { defineConfig, devices } from "@playwright/test"; | ||
|
|
||
| export default defineConfig({ | ||
| retries: 3, | ||
| fullyParallel: true, | ||
| use: { | ||
| contextOptions: { | ||
| reducedMotion: "reduce", | ||
| }, | ||
| }, | ||
| projects: [ | ||
| { name: "chromium", use: devices["Desktop Chrome"] }, | ||
| { name: "firefox", use: devices["Desktop Firefox"] }, | ||
| { | ||
| name: "webkit", | ||
| use: { | ||
| ...devices["Desktop Safari"], | ||
| deviceScaleFactor: 1, | ||
| }, | ||
| }, | ||
| ], | ||
| reporter: "list", | ||
| testMatch: "src/**/*.pw.spec.ts", | ||
| webServer: { | ||
| command: "node start.mjs", | ||
| port: 5273, | ||
| reuseExistingServer: true, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }, | ||
| }); |
This file contains hidden or 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 @@ | ||
| /* | ||
| * Base styles for the FAST test harness. | ||
| * Consumers can override or extend these in their own test setups. | ||
| */ | ||
| *, | ||
| *::before, | ||
| *::after { | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| body { | ||
| margin: 0; | ||
| padding: 0; | ||
| font-family: system-ui, -apple-system, sans-serif; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.