Svelte Jester

screenshot of Svelte Jester
svelte
scss

A Jest transformer for Svelte - compile your components before importing them into tests.

Overview

This product analysis will focus on a library called svelte-jester. The library is used for testing Svelte applications with Jest. The analysis will cover key features, installation instructions, and a summary of the content.

Features

  • Svelte Testing: svelte-jester allows for easy testing of Svelte applications with Jest.
  • Preprocess Options: The library supports various preprocess options, including support for TypeScript and Babel.
  • ESM and CJS versions: svelte-jester provides both ESM (ECMAScript module) and CJS (CommonJS) versions for different project setups.

ESM version

To use the ESM version of svelte-jester, add the following to your Jest config:

"transform": {
  "^.+\\.svelte$": "svelte-jester"
},
"moduleFileExtensions": [
  "js",
  "ts",
  "svelte"
]

CJS (CommonJS) version

For the CJS version, add the following to your Jest config:

"transform": {
  "^.+\\.svelte$": "svelte-jester/dist/transformer.cjs"
},
"moduleFileExtensions": [
  "js",
  "ts",
  "svelte"
]

Babel (only for CJS)

If you need to use Babel for the CJS version, install the required packages:

npm install @babel/core @babel/preset-env babel-jest -D

Add the following to your Jest config:

"transform": {
  "^.+\\.svelte$": "svelte-jester/dist/transformer.cjs"
},
"moduleFileExtensions": [
  "js",
  "ts",
  "svelte"
],
"transformIgnorePatterns": [
  "node_modules/(?!(svelte)|(my-lib))"
],
"transformOptions": {
  "babelConfig": true
}

TypeScript

To enable TypeScript support, install the necessary packages:

npm install typescript svelte-preprocess ts-jest -D

ESM version

Create a svelte.config.js file at the root of your project with the following configuration:

const sveltePreprocess = require('svelte-preprocess');

module.exports = {
  preprocess: sveltePreprocess()
};

In your Jest config, enable preprocessing for svelte-jester and add ts-jest as a transform:

"transform": {
  "^.+\\.svelte$": [
    "svelte-jester",
    {
      "preprocess": true
    }
  ],
  ".+\\.(css|styl|less|sass|scss)$": "jest-transform-css"
},
"moduleFileExtensions": [
  "js",
  "ts",
  "svelte"
]

CJS version

Create a svelte.config.js file at the root of your project with the following configuration:

const sveltePreprocess = require('svelte-preprocess');

module.exports = {
  preprocess: sveltePreprocess()
};

In your Jest config, enable preprocessing for svelte-jester and add ts-jest as a transform:

"transform": {
  "^.+\\.svelte$": [
    "svelte-jester/dist/transformer.cjs",
    {
      "preprocess": true
    }
  ],
  ".+\\.(css|styl|less|sass|scss)$": "jest-transform-css"
},
"moduleFileExtensions": [
  "js",
  "ts",
  "svelte"
]

Note: TypeScript supports ES modules, so if you were previously using babel-jest for ES module transpilation, you can remove babel-jest, babel, and any associated presets and config. However, if you still want to use ES modules in .js files, you need to configure ts-jest accordingly.

Summary

svelte-jester is a library that facilitates testing of Svelte applications using Jest. It provides support for different preprocess options, including TypeScript and Babel. The library offers ESM and CJS versions to accommodate different project setups. The installation process requires configuring the Jest config file appropriately based on the chosen version and preprocess options.

svelte
Svelte

Svelte is a modern front-end framework that compiles your code at build time, resulting in smaller and faster applications. It uses a reactive approach to update the DOM, allowing for high performance and a smoother user experience.

scss
SCSS

SCSS is a preprocessor scripting language that extends the capabilities of CSS by adding features such as variables, nesting, and mixins. It allows developers to write more efficient and maintainable CSS code, and helps to streamline the development process by reducing repetition and increasing reusability.

rollup
Rollup

RollupJS is a popular and efficient JavaScript module bundler that takes the code from multiple modules and packages them into a single optimized file, minimizing the overall size of the application and improving its performance.

typescript
Typescript

TypeScript is a superset of JavaScript, providing optional static typing, classes, interfaces, and other features that help developers write more maintainable and scalable code. TypeScript's static typing system can catch errors at compile-time, making it easier to build and maintain large applications.