Setup
We recommend using Jest but you're free to use the library with any testing framework and runner you're comfortable with.
Jest
Install Jest
npm install --save-dev jest
Add the following to your
package.json
{ "scripts": { "test": "jest src", "test:watch": "npm run test -- --watch" } }
You'll need to compile the Svelte components before using them in Jest, so we need to install svelte-jester
npm install --save-dev svelte-jester
Add the following Jest configuration to your
package.json
{ "jest": { "transform": { "^.+\\.svelte$": "svelte-jester" }, "moduleFileExtensions": ["js", "svelte"] } }
This is optional but it is recommended, you can install jest-dom to add handy assertions to Jest
5.1 Install jest-dom
npm install --save-dev @testing-library/jest-dom
5.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 run test
Babel / Preprocessors
If you'd like to also include Babel or any Svelte preprocessors then simply follow the instructions over at svelte-jester.