### Setup Development Environment Source: https://github.com/testing-library/svelte-testing-library/blob/main/CONTRIBUTING.md Install dependencies and run all checks after cloning the repository using the 'setup' npm script. This ensures the development environment is correctly configured. ```shell npm run setup ``` -------------------------------- ### Setup for Different Svelte Versions Source: https://github.com/testing-library/svelte-testing-library/blob/main/CONTRIBUTING.md Use provided npm scripts to set up your environment for different versions of Svelte (5, 4, 3) and run the associated test suites. This allows testing compatibility with various Svelte releases. ```shell # Svelte 5 npm run install:5 npm run all # Svelte 4 npm run install:4 npm run all:legacy # Svelte 3 npm run install:3 npm run all:legacy ``` -------------------------------- ### Manual Setup and Cleanup with @testing-library/svelte Source: https://github.com/testing-library/svelte-testing-library/blob/main/README.md Demonstrates the manual process of setting up and cleaning the test environment using `setup()` and `cleanup()` functions from `@testing-library/svelte`. This is useful when auto-cleanup is disabled or not supported by the test runner. ```javascript import { cleanup, render, setup } from '@testing-library/svelte' // before setup() // test render(/* ... */) // after cleanup() ``` -------------------------------- ### Preview Release with npm Source: https://github.com/testing-library/svelte-testing-library/blob/main/CONTRIBUTING.md Preview the next release version and changelog from a given branch using the 'preview-release' npm script. This requires push access to the repository and the branch to exist in GitHub. ```shell npm run preview-release ``` -------------------------------- ### Lint and Format Code Source: https://github.com/testing-library/svelte-testing-library/blob/main/CONTRIBUTING.md Run auto-formatting to ensure any changes adhere to the code style of the repository. Use 'lint' to run checks without making any modifications. ```shell npm run format npm run lint ``` -------------------------------- ### Svelte Component: Basic Greeting Source: https://github.com/testing-library/svelte-testing-library/blob/main/examples/basic/readme.md A simple Svelte component that accepts a 'name' prop and displays a greeting. It includes a button that, when clicked, reveals the greeting message. ```svelte {#if showGreeting}
Hello {name}
{/if} ``` -------------------------------- ### Install @testing-library/svelte Source: https://github.com/testing-library/svelte-testing-library/blob/main/README.md Installs the @testing-library/svelte package as a development dependency. This library provides utilities for testing Svelte components and supports Svelte versions 3, 4, and 5. It's recommended to also install @testing-library/jest-dom for custom matchers. ```shell npm install --save-dev @testing-library/svelte ``` -------------------------------- ### Update Documentation Source: https://github.com/testing-library/svelte-testing-library/blob/main/CONTRIBUTING.md Use the 'docs' script to ensure the README's table of contents is up to date. The 'contributors:add' script adds a contributor to the README, and 'contributors:generate' ensures the contributor list is current. ```shell npm run docs npm run contributors:add npm run contributors:generate ``` -------------------------------- ### Svelte Component Test: Basic Greeting Source: https://github.com/testing-library/svelte-testing-library/blob/main/examples/basic/readme.md Tests for the Svelte greeting component, verifying initial state and behavior after user interaction. It uses @testing-library/svelte for rendering and querying, and @testing-library/user-event for simulating clicks. ```javascript import { render, screen } from '@testing-library/svelte' import { userEvent } from '@testing-library/user-event' import { expect, test } from 'vitest' import Subject from './basic.svelte' test('no initial greeting', () => { render(Subject, { name: 'World' }) const button = screen.getByRole('button', { name: 'Greet' }) const greeting = screen.queryByText(/hello/iu) expect(button).toBeInTheDocument() expect(greeting).not.toBeInTheDocument() }) test('greeting appears on click', async () => { const user = userEvent.setup() render(Subject, { name: 'World' }) const button = screen.getByRole('button') await user.click(button) const greeting = screen.getByText(/hello world/iu) expect(greeting).toBeInTheDocument() }) ``` -------------------------------- ### Run Unit Tests Source: https://github.com/testing-library/svelte-testing-library/blob/main/CONTRIBUTING.md Execute unit tests once or in watch mode for continuous testing. The 'test' script runs tests once, while 'test:watch' keeps tests running and re-runs them on file changes. ```shell npm test npm run test:watch ``` -------------------------------- ### Add svelteTesting Plugin to Vite Config Source: https://github.com/testing-library/svelte-testing-library/blob/main/README.md Integrates the svelteTesting plugin into your Vite configuration to enable testing utilities. This setup requires `@sveltejs/vite-plugin-svelte` and `@testing-library/svelte/vite`. ```diff // vite.config.js import { svelte } from '@sveltejs/vite-plugin-svelte' + import { svelteTesting } from '@testing-library/svelte/vite' export default defineConfig({ plugins: [ svelte(), + svelteTesting(), ] }); ``` -------------------------------- ### Svelte Component: Bindable Prop Example Source: https://github.com/testing-library/svelte-testing-library/blob/main/examples/binds/readme.md A Svelte component demonstrating the use of `$bindable()` for two-way data binding. It includes a script section to define the bindable prop and an input element that binds to this prop. ```svelte ``` -------------------------------- ### Svelte Component Using Context Source: https://github.com/testing-library/svelte-testing-library/blob/main/examples/contexts/readme.md This Svelte component demonstrates how to consume context data using the `getContext` function from 'svelte'. It expects a context named 'messages' and iterates over its 'current' property to display messages. ```svelte{message.text}
{@render message?.(greeting)}
``` ```js import { render, screen } from '@testing-library/svelte' import { createRawSnippet } from 'svelte' import { expect, test } from 'vitest' import Subject from './complex-snippet.svelte' test('renders greeting in message snippet', () => { render(Subject, { name: 'Alice', message: createRawSnippet((greeting) => ({ render: () => `${greeting()}`, })), }) const message = screen.getByTestId('message') expect(message).toHaveTextContent('Hello, Alice!') }) ``` -------------------------------- ### Test Basic Svelte Snippet with Wrapper Component Source: https://github.com/testing-library/svelte-testing-library/blob/main/examples/snippets/readme.md Demonstrates testing a basic Svelte snippet by rendering a wrapper component with dummy children. It uses `data-testid` for slot testing and relies on `@testing-library/svelte` and `vitest`. ```svelte