Index of every page on this site: llms.txt
All skills
fubits1 avatar

/testing-svelte

@a024d57
by Iljafubits1/svelte-skills11 stars
2
Run it yourself

Fix or write Svelte 5 tests with vitest-browser-svelte and Playwright. Use when debugging broken or flaky tests, 'Cannot connect to the iframe', strict mode violations, or writing unit/SSR/e2e tests.

Use this Skill: https://skilld.dev/gh/fubits1/svelte-skills/testing-svelte

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencescore-principles.md

≈705 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Core Principles

1. Locators First, Raw DOM Only With a Reason

vitest-browser-svelte uses Playwright-style locators with automatic retry logic. A raw container.querySelector(...) has no retry and no accessibility check, so it is the exception: allowed only with a one-line comment saying why no accessible locator works (a third-party widget without roles, an overlay that intercepts clicks).

// ❌ default to raw DOM: no retry logic, and no accessibility check
const { container } = await render(MyComponent);
const button = container.querySelector('button');
// ✅ locator: retries until the element is there
await render(MyComponent);
const button = page.getByRole('button', { name: 'Submit' });
await button.click();

/**
 * ✅ escape hatch, with its reason: the third-party date picker's day cells carry no
 * role or accessible name. `page.css` is a custom locator added with
 * `locators.extend` (svelte-5:vitest browser-mode reference)
 */
const day = page.css('.calendar-day[data-day="15"]');

2. Handle Strict Mode Violations

When multiple elements match a locator, use .first(), .nth(), or .last():

// ❌ FAILS: "strict mode violation: resolved to 2 elements"
page.getByRole('link', { name: 'Home' }); // desktop + mobile nav

// ✅ CORRECT: Handle multiple elements explicitly
page.getByRole('link', { name: 'Home' }).first();
page.getByRole('link', { name: 'Home' }).nth(1); // second element
page.getByRole('link', { name: 'Home' }).last();

3. Read $derived Values Directly

Access $derived values directly in tests; no untrack() needed. sveltest wraps such reads in untrack() as a defensive convention, so they cannot leak into a reactive scope (troubleshooting Error 3).

// ✅ a getter that returns a $derived value: read it
expect(component.derivedValue).toBe(42);

// ✅ an exported function: call it
expect(component.computeTotal()).toBe(42);

4. Real FormData/Request Objects

Use real web APIs instead of heavy mocking to catch client-server mismatches:

/**
 * fragment: `vi` from 'vitest', `database` from '#lib/server/database.js'
 * (mocked with a top-level `vi.mock('#lib/server/database.js')`)
 */

// ❌ BRITTLE: Mocks hide API mismatches
const mockRequest = {
	formData: vi.fn().mockResolvedValue({
		get: vi.fn().mockReturnValue('test@example.com'),
	}),
};

// ✅ ROBUST: Real FormData catches mismatches
const formData = new FormData();
formData.append('email', 'test@example.com');
const request = new Request('http://localhost/api/users', {
	method: 'POST',
	body: formData,
});

// only mock external services
vi.mocked(database.createUser).mockResolvedValue({
	id: '123',
	email: 'test@example.com',
});

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides comprehensive instructions and best practices for testing Svelte 5 applications using Vitest and Playwright. It covers unit, SSR, and E2E testing, with a focus on resilient locators, accessibility, and the use of real web API objects like FormData and Request. No security issues were detected.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at a024d57. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Activeupdated last month
user-invocable
true

README badge

README badge for fubits1/svelte-skills/testing-svelte