Skip to content

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

License

Notifications You must be signed in to change notification settings

nolanlawson/svelte-jester

 
 

Repository files navigation

svelte-jester

Simply precompiles Svelte components before importing them into Jest tests.

version MIT License


Table of Contents

Why not just use x?

Seems like other libraries won't working with preprocessors, won't maintained actively or didn't have proper licensing.

Installation

This library has peerDependencies listings for svelte >= 3.

npm install svelte-jester -D

Add the following to your Jest config

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

Babel

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

Add the following to your Jest config

"transform": {
  "^.+\\.js$": "babel-jest",
  "^.+\\.svelte$": "svelte-jester"
}

Create a .babelrc and add the following

{
  "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}

Preprocess

Preprocessors are loaded from svelte.config.js.

Add the following to your Jest config

"transform": {
  "^.+\\.js$": "babel-jest",
  "^.+\\.svelte$": ["svelte-jester", { "preprocess": true }]
}

Create a svelte.config.js file and configure it, see svelte-preprocess for more information.

Options

preprocess (default: false): Pass in true if you are using Svelte preprocessors. They are loaded from svelte.config.js.

debug (default: false): If you'd like to see the output of the compiled code then pass in true.

compilerOptions (default: {}): Use this to pass in Svelte compiler options.

"transform": {
  "^.+\\.js$": "babel-jest",
  "^.+\\.svelte$": ["svelte-jester", { 
    "preprocess": false,
    "debug": false,
    "compilerOptions": {}
  }]
}

Testing Library

This package is required when using Svelte with the Testing Library. If you'd like to avoid including implementation details in your tests, and making them more maintainble in the long run, then consider checking out @testing-library/svelte.

Credits

Thanks to all contributors, inspired by:

LICENSE

MIT

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 79.0%
  • HTML 21.0%