Skip to content

fix(fast-html): add errors fixture for f-template error cases#7406

Open
janechu wants to merge 4 commits intomainfrom
users/janechu/fix-more-than-one-template-error
Open

fix(fast-html): add errors fixture for f-template error cases#7406
janechu wants to merge 4 commits intomainfrom
users/janechu/fix-more-than-one-template-error

Conversation

@janechu
Copy link
Copy Markdown
Collaborator

@janechu janechu commented Apr 7, 2026

Pull Request

📖 Description

Adds an errors test fixture for @microsoft/fast-html that covers the two error conditions thrown by the <f-template> element:

  • No template element present — when an <f-template> contains no <template> child, a noTemplateProvided error (code 2000) is thrown.
  • Multiple template elements present — when an <f-template> contains more than one <template> child, a moreThanOneTemplateProvided error (code 2001) is thrown.

The fixture includes index.html, main.ts, and errors.spec.ts. Tests use page.addInitScript to intercept unhandledrejection events (the errors originate inside an unhandled promise chain) for reliable cross-browser coverage across Chromium, Firefox, and WebKit.

📑 Test Plan

Run the new fixture tests:

cd packages/fast-html
npx playwright test test/fixtures/errors/errors.spec.ts

All 6 tests pass (2 cases × 3 browsers).

✅ Checklist

General

  • I have included a change request file using $ npm run change
  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

janechu and others added 2 commits April 7, 2026 14:14
Add a test fixture that covers the two error conditions thrown by the
f-template element: no template element present and multiple template
elements present.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@janechu janechu changed the title test: add errors fixture for f-template error cases fix(fast-html): add errors fixture for f-template error cases Apr 7, 2026
@janechu janechu marked this pull request as ready for review April 10, 2026 20:40
@janechu janechu requested review from Copilot April 10, 2026 22:04
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds explicit coverage for <f-template> error cases in @microsoft/fast-html by introducing a new Playwright fixture and a new error code/message for the “multiple <template> children” condition.

Changes:

  • Added a new errors fixture (HTML + setup + Playwright spec) to validate <f-template> error behavior via unhandledrejection.
  • Introduced Message.moreThanOneTemplateProvided (2001) and throws it when an <f-template> contains more than one <template>.
  • Registered a new debug message for error code 2001.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/fast-html/test/fixtures/errors/main.ts Defines minimal custom elements and registers <f-template> for the new fixture.
packages/fast-html/test/fixtures/errors/index.html Fixture page that triggers “no template” and “multiple templates” cases.
packages/fast-html/test/fixtures/errors/errors.spec.ts Playwright tests asserting both error paths via unhandledrejection.
packages/fast-html/src/interfaces.ts Adds Message.moreThanOneTemplateProvided = 2001.
packages/fast-html/src/debug.ts Adds a debug message for error code 2001.
packages/fast-html/src/components/template.ts Updates <f-template> handling to throw 2001 when multiple templates exist.
change/@microsoft-fast-html-a4161f8e-9273-42b3-9c6f-95249dd0ff2e.json Beachball change file for the @microsoft/fast-html prerelease fix.

Comment on lines 2 to +3
[2000 /* noTemplateProvided */]: `The first child of the <f-template> must be a <template>, this is missing from ${name}.`,
[2001 /* moreThanOneTemplateProvided */]: `There can only be one <template> inside the <f-template>, you must add one for ${name}.`,
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debugMessages entries are defined using template literals, so ${name} is being interpolated by JavaScript at module evaluation time (e.g. to window.name in browsers) instead of being left as a placeholder for FAST.error(code, { name }) to format. These messages should be plain strings containing the literal ${name} placeholder so formatMessage() can substitute runtime values.

Suggested change
[2000 /* noTemplateProvided */]: `The first child of the <f-template> must be a <template>, this is missing from ${name}.`,
[2001 /* moreThanOneTemplateProvided */]: `There can only be one <template> inside the <f-template>, you must add one for ${name}.`,
[2000 /* noTemplateProvided */]: "The first child of the <f-template> must be a <template>, this is missing from ${name}.",
[2001 /* moreThanOneTemplateProvided */]: "There can only be one <template> inside the <f-template>, you must add one for ${name}.",

Copilot uses AI. Check for mistakes.
@@ -1,3 +1,4 @@
export const debugMessages = {
[2000 /* noTemplateProvided */]: `The first child of the <f-template> must be a <template>, this is missing from ${name}.`,
[2001 /* moreThanOneTemplateProvided */]: `There can only be one <template> inside the <f-template>, you must add one for ${name}.`,
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new moreThanOneTemplateProvided message text is misleading: it says "you must add one" even though this error is thrown when multiple <template> elements are present. Update the wording to instruct users to remove extra <template> elements and keep exactly one.

Suggested change
[2001 /* moreThanOneTemplateProvided */]: `There can only be one <template> inside the <f-template>, you must add one for ${name}.`,
[2001 /* moreThanOneTemplateProvided */]: `There can only be one <template> inside the <f-template>; remove any extra <template> elements and keep exactly one for ${name}.`,

Copilot uses AI. Check for mistakes.
Comment on lines +4 to +12
test("throws an error when no template element is present", async ({ page }) => {
await page.addInitScript(() => {
(window as any).__errors = [];
window.addEventListener("unhandledrejection", event => {
(window as any).__errors.push(
event.reason?.message ?? String(event.reason),
);
});
});
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The page.addInitScript setup for capturing unhandledrejection is duplicated in both tests. Consider moving this into a test.beforeEach (or a small helper) so the listener/storage initialization is defined once and stays consistent if it changes.

Copilot uses AI. Check for mistakes.
Comment on lines +7 to +11
window.addEventListener("unhandledrejection", event => {
(window as any).__errors.push(
event.reason?.message ?? String(event.reason),
);
});
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the unhandledrejection handler, the event isn't preventDefault()'d. Since these tests intentionally trigger unhandled rejections, calling event.preventDefault() will suppress default reporting/noise and reduce the risk of future harness/config changes treating the rejection as a test failure.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants