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.

referencesclient-server-alignment.md

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

Client-Server Test Alignment

Client-Server Alignment

The Problem

Heavy mocking in server tests can hide client-server mismatches that only appear in production.

The Solution

Use real FormData and Request objects. Only mock external services (database, APIs).

/**
 * src/routes/api/register/server.test.ts (fragment): `vi`, `database` and `POST` come from
 * `import { vi } from 'vitest'`, `import * as database from '#lib/server/database.js'`,
 * `import { POST } from './+server'`, with `vi.mock('#lib/server/database.js')` at the top level
 */

// ❌ BRITTLE APPROACH
const mockRequest = {
	formData: vi.fn().mockResolvedValue({
		get: vi.fn((key) => {
			if (key === 'email') return 'test@example.com';
			if (key === 'password') return 'pass123';
		}),
	}),
};
// this passes even if real FormData API differs!

// ✅ ROBUST APPROACH
const formData = new FormData();
formData.append('email', 'test@example.com');
formData.append('password', 'pass123');

const request = new Request('http://localhost/api/register', {
	method: 'POST',
	body: formData,
});

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

const response = await POST({ request } as Parameters<typeof POST>[0]);

Shared Validation Logic

Use the same validation on client and server:

// src/lib/validation.ts
export function validateEmail(email: string) {
	if (!email) return 'Email is required';
	if (!email.includes('@')) return 'Invalid email format';
	return null;
}
// src/lib/validation.test.ts: unit test for the shared rule
import { expect, test } from 'vitest';
import { validateEmail } from '#lib/validation.js';
test('validates email', () => {
	expect(validateEmail('')).toBe('Email is required');
	expect(validateEmail('invalid')).toBe('Invalid email format');
	expect(validateEmail('test@example.com')).toBe(null);
});
// src/routes/api/register/+server.ts: the endpoint uses the same validation
import type { RequestHandler } from './$types';
import { validateEmail } from '#lib/validation.js';
import { createUser } from '#lib/server/database.js';

export const POST: RequestHandler = async ({ request }) => {
	const formData = await request.formData();
	/**
	 * `FormData.get()` returns string | File | null: keep strings only,
	 * since String(file) is "[object File]" and passes the required check
	 */
	const rawEmail = formData.get('email');
	const rawPassword = formData.get('password');
	const email = typeof rawEmail === 'string' ? rawEmail : '';
	const password = typeof rawPassword === 'string' ? rawPassword : '';
	const errors: Record<string, string> = {};
	const emailError = validateEmail(email);
	if (emailError) errors.email = emailError;
	if (!password) errors.password = 'Password is required';
	if (Object.keys(errors).length > 0) {
		return Response.json({ errors }, { status: 400 });
	}
	await createUser({ email, password });
	return Response.json({ ok: true }, { status: 201 });
};

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