I've been looking into Svelte, and while most of the info that I'm looking for can be found in the documentation, there's one piece missing. There's no section on how to test components, so I went to explore this area.
The first place where I was looking, was the Svelte repository itself. It contains tests that seem simple at first, like this one. The only problem that I have, is that it requires me to orchestrate the test setup.
After taking a look at the Svelte repository, I quickly came across the Svelte Testing Library. I'm the creator of the Angular Testing Library, and because of the Testing Library has a similar API across all frameworks, I was quickly used to it.
The Babel compiler has to be installed to use the ES6 syntax while writing tests.
To compile Svelte components, it's also needed to install svelte-jester.
After the installation is complete, Jest and Babel have to be configured.
To configure Jest, create a jest.config.js file in the root of the project.
jest.config.js
module.exports={
transform:{
'^.+\\.svelte$':'svelte-jester',
'^.+\\.js$':'babel-jest',
},
moduleFileExtensions: ['js','svelte'],
}
For Babel, create a babel.config.js file in the root of the project.
babel.config.js
module.exports={
presets: [
[
'@babel/preset-env',
{
targets:{
node:'current',
},
},
],
],
}
The last step of the setup is to add a script inside the package.json file: