Setup
We recommend using Vitest, but you're free to use the library with any test runner that's ESM compatible.
Vitest
-
Add development dependencies
@testing-library/svelte@testing-library/jest-dom(Optional, but highly recommended)@sveltejs/vite-plugin-sveltevitestjsdom,happy-dom, or other Vitest DOM environment
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev \@testing-library/svelte \@testing-library/jest-dom \@sveltejs/vite-plugin-svelte \vitest \jsdomyarn add --dev \@testing-library/svelte \@testing-library/jest-dom \@sveltejs/vite-plugin-svelte \vitest \jsdompnpm add --save-dev \@testing-library/svelte \@testing-library/jest-dom \@sveltejs/vite-plugin-svelte \vitest \jsdombun add --dev \@testing-library/svelte \@testing-library/jest-dom \@sveltejs/vite-plugin-svelte \vitest \jsdomOptionally install
@vitest/ui, which opens a UI within a browser window to follow the progress and interact with your tests.- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @vitest/uiyarn add --dev @vitest/uipnpm add --save-dev @vitest/uibun add --dev @vitest/ui -
Add a
vitest-setup.jsfile to optionally set up@testing-library/jest-domexpect matchers.vitest-setup.jsimport '@testing-library/jest-dom/vitest' -
Add
vitest.config.js, or update your existingvite.config.js, with thesvelteandsvelteTestingVite plugins. Set the testing environment to your DOM library of choice and optionally configure your setup file from step (2).vite.config.jsimport {defineConfig} from 'vitest/config'import {svelte} from '@sveltejs/vite-plugin-svelte'import {svelteTesting} from '@testing-library/svelte/vite'export default defineConfig({plugins: [svelte(), svelteTesting()],test: {environment: 'jsdom',setupFiles: ['./vitest-setup.js'],},})Or, if you're using SvelteKit:
vite.config.jsimport {defineConfig} from 'vitest/config'import {sveltekit} from '@sveltejs/kit/vite'import {svelteTesting} from '@testing-library/svelte/vite'export default defineConfig({plugins: [sveltekit(), svelteTesting()],test: {environment: 'jsdom',setupFiles: ['./vitest-setup.js'],},})noteThe
svelteTestingplugin:- Adds an automatic cleanup fixture to
test.setupFiles - Adds
browsertoresolve.conditions
If needed, you can disable either behavior. Disabling both options is equivalent to omitting the plugin.
svelteTesting({// disable auto cleanupautoCleanup: false,// disable browser resolution conditionresolveBrowser: false,})Resolving the
browsercondition may cause issues if you have a complex Vite configuration or dependencies that cannot be loaded into Node.js. See testing-library/svelte-testing-library#222 for more information and alternative configuration options to ensure Svelte's browser code is used. - Adds an automatic cleanup fixture to
-
Add test scripts to your
package.jsonto run the tests with Vitestpackage.json{"scripts": {"test": "vitest run","test:ui": "vitest --ui","test:watch": "vitest"}} -
Create your component and a test file (checkout the rest of the docs to see how) and run the following command to run the tests.
- npm
- Yarn
- pnpm
- Bun
npm testyarn testpnpm testbun run test
TypeScript
Include @testing-library/jest-dom to the
TypeScript types to make the TypeScript compiler aware about the
@testing-library/jest-dom matchers.
{
"compilerOptions": {
"types": ["@testing-library/jest-dom"]
}
}
Jest
@testing-library/svelte is ESM-only, which means
you must use Jest in ESM mode.
-
Add development dependencies
@testing-library/svelte@testing-library/jest-dom(Optional, but highly recommended)svelte-jesterjestjest-environment-jsdom
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev \@testing-library/svelte \@testing-library/jest-dom \svelte-jester \jest \jest-environment-jsdomyarn add --dev \@testing-library/svelte \@testing-library/jest-dom \svelte-jester \jest \jest-environment-jsdompnpm add --save-dev \@testing-library/svelte \@testing-library/jest-dom \svelte-jester \jest \jest-environment-jsdombun add --dev \@testing-library/svelte \@testing-library/jest-dom \svelte-jester \jest \jest-environment-jsdom -
Add a
jest-setup.jsfile to configure@testing-library/jest-dom, if using.jest-setup.jsimport '@testing-library/jest-dom' -
Configure Jest to use jsdom, transform Svelte files, and use your setup file
jest.config.jsexport default {transform: {'^.+\\.svelte$': 'svelte-jester',},moduleFileExtensions: ['js', 'svelte'],extensionsToTreatAsEsm: ['.svelte'],testEnvironment: 'jsdom',setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],}noteIf you are using Svelte 5, you must use
svelte-jester@5or later, and you will need to make additional changes to your Jest configuration.- Update
transformto compile.svelte.(js|ts)modules - Allow
@testing-library/svelteto be transformed, even though it's innode_modules
jest.config.jsexport default {transform: {- '^.+\\.svelte$': 'svelte-jester',+ '^.+\\.svelte(\\.(js|ts))?$': 'svelte-jester',},+ transformIgnorePatterns: [+ '/node_modules/(?!@testing-library/svelte/)',+ ],moduleFileExtensions: ['js', 'svelte'],extensionsToTreatAsEsm: ['.svelte'],testEnvironment: 'jsdom',setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],} - Update
-
Add the following to your
package.jsonpackage.json{"scripts": {"test": "npx --node-options=\"--experimental-vm-modules\" jest src","test:watch": "npx --node-options=\"--experimental-vm-modules\" jest src --watch"}} -
Create your component + test file (checkout the rest of the docs to see how) and run it
- npm
- Yarn
- pnpm
- Bun
npm testyarn testpnpm testbun run test
TypeScript and preprocessors
To use TypeScript with Jest, you'll need to install and configure
svelte-preprocess and ts-jest. For full instructions, see the
svelte-jester docs.
If you'd like include any other Svelte preprocessors, see
the svelte-jester docs.