Setup
We recommend using Vitest but you're free to use the library with any testing framework and runner you're comfortable with.
Vitest
- Install Vitest and jsdom
We're using jsdom here as the test environment, but you can use any other
options e.g happy-dom.
- npm
- Yarn
npm install --save-dev vitest jsdom
yarn add --dev vitest jsdom
Optionally install @vitest/ui, which opens a UI within a browser window to
follow the progress and interact with your tests.
- npm
- Yarn
npm install --save-dev @vitest/ui
yarn add --dev @vitest/ui
Add the test scipts to your
package.jsonto run the tests with Vitest{
"scripts": {
"test": "vitest run",
"test:ui": "vitest --ui",
"test:watch": "vitest"
}
}To compile the Svelte components before using them in Vitest, you need to install @sveltejs/vite-plugin-svelte and Vite
- npm
- Yarn
npm install --save-dev @sveltejs/vite-plugin-svelte viteyarn add --dev @sveltejs/vite-plugin-svelte viteAdd a
vitest.config.tsconfiguration file to the root of your project. Addglobals: truesocleanup()runs after each test.import {defineConfig} from 'vitest/config'
import {svelte} from '@sveltejs/vite-plugin-svelte'
export default defineConfig({
plugins: [svelte({hot: !process.env.VITEST})],
test: {
globals: true,
environment: 'jsdom',
},
})Optionally install vitest-dom to add handy assertions to Vitest
4.1 Install
vitest-dom- npm
- Yarn
npm install --save-dev vitest-domyarn add --dev vitest-dom4.2 import
vitest-domat within the vitest setup file (usuallyvitest-setup.(js|ts))import * as matchers from 'vitest-dom/matchers'
import {expect} from 'vitest'
expect.extend(matchers)
// or:
import 'vitest-dom/extend-expect'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
npm testyarn test
Jest
Install Jest & jest-environment-jsdom
- npm
- Yarn
npm install --save-dev jest jest-environment-jsdomyarn add --dev jest jest-environment-jsdomAdd the following to your
package.json{
"scripts": {
"test": "jest src",
"test:watch": "jest src --watch"
}
}You'll need to compile the Svelte components before using them in Jest, so we need to install svelte-jester
- npm
- Yarn
npm install --save-dev svelte-jesteryarn add --dev svelte-jesterAdd the following Jest configuration to your
package.json{
"jest": {
"transform": {
"^.+\\.svelte$": "svelte-jester"
},
"moduleFileExtensions": ["js", "svelte"],
"testEnvironment": "jsdom"
}
}If you are using ES6 modules in your project you have to add Jest's babel transform setting (it is set by default, but since we are overriding the transform config, we have to add it explicitly)
5.1 Install
babel-jest- npm
- Yarn
npm install --save-dev babel-jestyarn add --dev babel-jest5.2. Add a basic
.babelrcconfiguration{
"presets": [["@babel/preset-env", {"targets": {"node": "current"}}]]
}5.3. Update the Jest transform configuration
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.svelte$": "svelte-jester"
},This is optional but it is recommended, you can install jest-dom to add handy assertions to Jest
6.1 Install
jest-dom- npm
- Yarn
npm install --save-dev @testing-library/jest-domyarn add --dev @testing-library/jest-dom6.2 Add the following to your Jest configuration in
package.json{
"setupFilesAfterEnv": ["@testing-library/jest-dom/extend-expect"]
}Create your component + test file (checkout the rest of the docs to see how) and run it
- npm
- Yarn
npm testyarn test
TypeScript
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.
Preprocessors
If you'd like to also include any Svelte preprocessors then simply follow the instructions over at svelte-jester.