mcpField
J
javascript-testing

javascript-testing

github.com/autumnsgroveTesting & quality672 installsUpdated 6d ago
Repository

About javascript-testing

A skill for writing, configuring, and running JavaScript and TypeScript tests. It provides framework-selection guidance and examples for unit, component, mocking, and SvelteKit testing.

Key features

  1. 01Vitest and Jest selection guidance
  2. 02Test setup and AAA structure
  3. 03Mocking and spy examples
  4. 04Svelte component testing
  5. 05SvelteKit load and form-action tests
  6. 06Coverage and test commands

Use cases

  1. 01Setting up tests in a JavaScript project
  2. 02Testing React, Svelte, or Vue components
  3. 03Mocking API calls in unit tests
  4. 04Testing SvelteKit load functions and form actions

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/autumnsgrove/lattice --skill javascript-testing

After installing, type/javascript-testingin Claude Code to confirm it loaded.

Details

From the project README

JavaScript/TypeScript Testing Skill

When to Activate

Activate this skill when:

  • Writing JavaScript or TypeScript tests
  • Testing Svelte, React, or Vue components
  • Setting up Vitest or Jest configuration
  • Working with mocks, spies, or test utilities
  • Running tests or checking coverage

Framework Selection

Use CaseFramework
SvelteKit, Vite projectsVitest (recommended)
Non-Vite projects, React NativeJest

Quick Commands

Vitest
npx vitest              # Watch mode
npx vitest run          # Single run (CI)
npx vitest run --coverage
npx vitest --ui         # Visual UI
Jest
pnpm test
pnpm test --watch
pnpm test --coverage

Test Structure: AAA Pattern

import { describe, it, expect, beforeEach } from 'vitest';

describe('UserService', () => {
    let userService: UserService;

    beforeEach(() => {
        userService = new UserService();
    });

    it('should create a new user with valid data', () => {
        // Arrange
        const email = '[email protected]';
        const password = 'secure_pass123';

        // Act
        const result = userService.register(email, password);

        // Assert
        expect(result.success).toBe(true);
        expect(result.user.email).toBe(email);
    });
});

Vitest Setup (SvelteKit)

// vite.config.ts
import { defineConfig } from 'vitest/config';
import { sveltekit } from '@sveltejs/kit/vite';

export default defineConfig({
    plugins: [sveltekit()],
    test: {
        include: ['src/**/*.{test,spec}.{js,ts}'],
        globals: true,
        environment: 'jsdom',
        setupFiles: ['./src/tests/setup.ts'],
    }
});

Mocking

Vitest
import { vi } from 'vitest';

vi.mock('./api', () => ({
    fetchUser: vi.fn()
}));

vi.mocked(fetchUser).mockResolvedValue({ id: 1, name: 'John' });
Jest
jest.mock('./api', () => ({
    fetchUser: jest.fn()
}));

Component Testing (Svelte)

import { render, screen, fireEvent } from '@testing-library/svelte';
import Counter from './Counter.svelte';

it('should increment count on click', async () => {
    render(Counter, { props: { initialCount: 0 } });

    const button = screen.getByRole('button', { name: /increment/i });
    await fireEvent.click(button);

    expect(screen.getByText('Count: 1')).toBeInTheDocument();
});

Common Assertions

// Equality
expect(value).toBe(expected);           // Strict ===
expect(value).toEqual(expected);        // Deep equality

// Truthiness
expect(value).toBeTruthy();
expect(value).toBeNull();

// Arrays/Objects
expect(array).toContain(item);
expect(obj).toHaveProperty('key');

// Exceptions
expect(() => fn()).toThrow('error');

// Async
await expect(promise).resolves.toBe(value);
await expect(promise).rejects.toThrow();

Query Priority (Testing Library)

  1. getByRole - Accessible queries (best)
  2. getByLabelText - Form fields
  3. getByPlaceholderText - Inputs
  4. getByText - Non-interactive elements
  5. getByTestId - Last resort

Directory Structure

src/
├── lib/
│   ├── components/
│   │   ├── Button.svelte
│   │   └── Button.test.ts
│   └── utils/
│       ├── format.ts
│       └── format.test.ts
└── tests/
    ├── setup.ts
    └── integration/

SvelteKit Testing

Load Functions
import { load } from './+page.server';

it('should fetch posts', async () => {
    const mockFetch = vi.fn().mockResolvedValue({
        json: () => Promise.resolve([{ id: 1 }])
    });

    const result = await load({ fetch: mockFetch } as any);
    expect(result.posts).toHaveLength(1);
});
Form Actions
import { actions } from './+page.server';

it('should validate login', async () => {
    const formData = new FormData();
    formData.set('email', '[email protected]');

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

    const result = await actions.default({ request } as any);
    expect(result.success).toBe(true);
});

Related Resources

See AgentUsage/testing_javascript.md for complete documentation including:

  • Jest configuration
  • Async testing patterns
  • SvelteKit-specific patterns
  • CI/CD integration

Comments

Markdown supported