パッケージの詳細

svelte-jester

svelteness114.8kMIT5.0.0

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

svelte-jester, jest, svelte, sveltekit

readme

svelte-jester

Simply precompiles Svelte components before importing them into Jest tests.

This version requires Jest >= 27 and defaults to ESM, which is required with Svelte 4+. If you're using Svelte 3 and want to use CJS, you need to specify the full path for the jest transformer in your jest config.

version MIT License

Table of Contents

Why not just use x?

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

Installation

If you're using SvelteKit, you can set it up and install it with svelte-add-jest by running:

npx apply rossyman/svelte-add-jest

Manual installation

This library has peerDependencies listings for svelte >= 3 and jest >= 27.

npm install svelte-jester -D

ESM version

Add the following to your Jest config:

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

Run your tests with NODE_OPTIONS=--experimental-vm-modules. For Windows you need to add cross-env as well.

  "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest src",

CJS (CommonJS) version

Add the following to your Jest config:

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

Babel (only for CJS)

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" } }]]
}

TypeScript

To enable TypeScript support you'll need to setup svelte-preprocess and ts-jest.

  1. Install typescript, svelte-preprocess, and ts-jest:

    npm install typescript svelte-preprocess ts-jest -D

ESM version

  1. Create a svelte.config.js at the root of your project:

    import preprocess from 'svelte-preprocess'
    
    /** @type {import('@sveltejs/kit').Config} */
    export default config = {
      preprocess: preprocess(),
      // ...
    };

    To learn what options you can pass to sveltePreprocess, please refer to the documentation.

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

    "transform": {
      "^.+\\.svelte$": [
        "svelte-jester",
        {
          "preprocess": true
        }
      ],
      "^.+\\.ts$": [
        "ts-jest",
        {
          "useESM": true
          // optional: seperate tsconfig for tests
          //"tsconfig": "tsconfig.spec.json",
        }
      ],
    },
    "moduleFileExtensions": [
      "js",
      "ts",
      "svelte"
    ],
    "extensionsToTreatAsEsm": [
      ".svelte",
      ".ts"
    ],

However if you do not want to create a svelte.config.js at the root of your project or you wish to use a custom config just for tests, you may pass the path to the config file to the preprocess option thus:

  "transform": {
    "^.+\\.svelte$": [
      "svelte-jester",
      {
        "preprocess": "/some/path/to/svelte.config.js"
      }
    ],
    "^.+\\.ts$": [
      "ts-jest",
      {
        "useESM": true
        // optional: seperate tsconfig for tests
        //"tsconfig": "tsconfig.spec.json",
      }
    ],
  },
  "moduleFileExtensions": [
    "js",
    "ts",
    "svelte"
  ],
  "extensionsToTreatAsEsm": [
    ".svelte",
    ".ts"
  ],

CJS version

  1. Create a svelte.config.js at the root of your project:

    const sveltePreprocess = require("svelte-preprocess");
    
    module.exports = {
      preprocess: sveltePreprocess({
        // ...
      }),
    };

    To learn what options you can pass to sveltePreprocess, please refer to the documentation.

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

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

However if you do not want to create a svelte.config.js at the root of your project or you wish to use a custom config just for tests, you may pass the path to the config file to the preprocess option thus:

  "transform": {
    "^.+\\.svelte$": [
      "./node_modules/svelte-jester/dist/transformer.cjs",
      {
        "preprocess": "/some/path/to/svelte.config.js"
      }
    ],
    "^.+\\.ts$": "ts-jest"
  },
  "moduleFileExtensions": [
    "js",
    "ts",
    "svelte"
  ]

Note that TypeScript supports ES modules, so if you were previously using babel-jest just for ES module transpilation, you can remove babel-jest, babel, and any associated presets and config.

By default, ts-jest will only transpile .ts files though, so if you want to continue using ES modules in .js files, you'll need to configure ts-jest to process .js files as well. To do this, update the file glob above, and follow the instructions in the ts-jest docs.

Preprocess

Preprocessors are loaded from svelte.config.js or svelte.config.cjs.

Add the following to your Jest config:

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

For CJS, replace "svelte-jester" with "./node_modules/svelte-jester/dist/transformer.cjs".

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

In CJS mode, svelte-jester must start a new a process for each file needing to be preprocessed, which adds a performance overhead.

In ESM mode, this isn't necessary. You can set NODE_OPTIONS=--experimental-vm-modules and "extensionsToTreatAsEsm": [".svelte"] to run in ESM mode. However, be aware that ESM support in Jest is still experimental as according to their docs. Follow the development along at https://github.com/facebook/jest/issues/9430.

Options

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

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

svelteVersion (default: actual Version from Svelte package): If you'd like to override the svelteVersion for some reason.

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

rootMode (default: ""): Pass in upward to walk up from the transforming file's directory and use the first svelte.config.js or svelte.config.cjs found, or throw an error if no config file is discovered. This is particularly useful in a monorepo as it allows you to:

  • Run tests from the worktree root using Jest projects where you only want to put svelte.config.js in workspace folders, and not in the root.
  • Run tests from the worktree root using Jest projects, but have different svelte.config.js configurations for individual workspaces.
  • Have one common svelte.config.js in your worktree root (or any directory above the file being transformed) without needing individual svelte.config.js files in each workspace. Note - this root config file can be overriden if necessary by putting another config file into a workspace folder

The default mode is to load svelte.config.js or svelte.config.cjs from the current project root to avoid the risk of accidentally loading a config file from outside the current project folder.

When upward is set it will stop at the first config file it finds above the file being transformed, but will walk up the directory structure all the way to the filesystem root if it cannot find any config file. This means that if there is no svelte.config.js or svelte.config.cjs file in the project above the file being transformed, it is always possible that someone will have a forgotten config file in their home directory which could cause unexpected errors in your builds.

CJS mode options

showConsoleLog (default: false): If you'd like to see console.logs of the preprocessors then pass in true. Otherwise these will be surpressed, because the compiler could complain about unexpected tokens.

maxBuffer (default: 10485760): Sets limit for buffer when preprocess is true. It defines the largest amount of data in bytes allowed on stdout or stderr for child_process.spawnSync. If exceeded, the child process is terminated and any output is truncated. The default value of 10Mb overrides Node's default value of 1Mb.

"transform": {
  "^.+\\.js$": "babel-jest",
  "^.+\\.svelte$": ["./node_modules/svelte-jester/dist/transformer.cjs", {
    "preprocess": false,
    "debug": false,
    "compilerOptions": {},
    "rootMode": "",
    "maxBuffer": 15000000
  }]
}

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

更新履歴

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.0 (2024-06-14)

5.1.0 (2024-06-14)

Features

  • add $state rune to globals (586a6d8)
  • add coverage (e6679d0)
  • add e2e test for svelte 4 (80941b6)
  • add jest-dom (5f1d5f5)
  • add RELEASE documentation (c69d0e9)
  • add svelte4 test project (d9f4f2c)
  • add test case (109a0b4)
  • add test dependencies (08f2821)
  • add test dependencies, setup and scripts (71076e5)
  • enable import (f9a947d)
  • lock version to 3 (c9839cd)
  • rename svelte test folder to svelte-3 (25709ca)
  • replace cross-env with pnpm shell emulation (25afa84)
  • run e2e tests during validation (9a1cb23)
  • set version to 5.0.0 (44d54c0)
  • svelte5: add support for Svelte 5 modules (ed57c66)
  • test only supported engines (3f77257)

Bug Fixes

  • add npmrc with shell emulation for all e2e folders (fc3f525)
  • add option no-cache (1248b36)
  • bring back cross-env (759d4cb)
  • enable tests (8356e0f)
  • fix merge lockfile (17caa9a)
  • formatting (d28dfb8)
  • let tests complete on node 16 - 22 (a9b21e1)
  • pass linter (2f89077)
  • pnpm handles argument passing (0b2dd99)
  • reduce visibility of const (250441a)
  • remove missing vite logo (b7a54cb)
  • remove obsolete step (01c82c7)
  • remove unused npmrc (cea907d)
  • rewrite url as suggested by npm (ec050b6)
  • use different matcher to pass linter (df6d2a3)

3.0.0 (2023-07-24)

Features

  • add jest globals to standard (be8c23d)
  • allow svelteVersion to be injected for tests (5b12cc0)
  • disallow calling ESM mode from processSync (d6a381e)
  • document option svelteVersion (210591a)
  • extract functions into utils (ea8b0c3)
  • remove * import for treeshaking (d079756)
  • remove globals (15c5f5c)
  • remove restriction on Svelte3 (3051249)
  • run dependabot only on root package.json (6abfb88)
  • split release script into pre phase (96edf43)
  • split test call to not use NODE_OPTIONS for CJS (7493725)
  • upgrade e2e packages (1372d0f)
  • upgrade e2e packages (5d4d08c)
  • upgrade svelte to v4 (227785e)

Bug Fixes

  • add jest globals (a047c45)
  • add missing jest import (c88841d)
  • add missing jest-environment (66bad7b)
  • add missing words to comment (801162e)
  • enable validation for standardjs (a028920)
  • migrate deprecated tsconfig object (875f27f)
  • remove duplicate line (5cf2ca6)
  • rename constant (9243034)
  • revert change from last PR breaking windows (a4f1341)
  • use real svelte version as default (847933e)
  • use same relative path (6004a96)

2.3.2 (2022-02-16)

2.3.1 (2022-02-01)

Bug Fixes

  • drop min node version to 14 for node-sass (aa4508d)

2.3.0 (2022-02-01)

Features

  • bump min node version to 16 (9172250)

2.2.0 (2022-02-01)

Features

  • bump min node version to 14 (526d3ed)

Bug Fixes

  • [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received an instance of URL (1384839)

2.1.5 (2021-09-22)

Bug Fixes

  • add preprocess to published files array (#84) (4a10e92)
  • allow installation on Node 12 (7093aee)

2.1.4 (2021-09-21)

Bug Fixes

2.1.3 (2021-09-19)

Bug Fixes

  • invalid transformer export (#75) (4729f0b)
  • surface frame on failed Svelte compilation (a747d96)

2.1.2 (2021-09-12)

Bug Fixes

  • add back process method for CJS support (#71) (6ee7c5c)

2.1.1 (2021-08-31)

Bug Fixes

  • file paths in package.json pointing to src instead of dist (#69) (b6f2cbb)

2.1.0 (2021-08-27)

Features

  • build CJS and ESM versions of the library (#68) (d93f2dc)

2.0.1 (2021-08-05)

Bug Fixes

2.0.0 (2021-08-04)

⚠ BREAKING CHANGES

  • async transformers are only supported in Jest >=27.

Features

  • avoid creating new node processes by leveraging processAsync (#57) (92760dd)

1.8.2 (2021-08-04)

Bug Fixes

  • actually revert async changes -_- (64c3d4d)

1.8.1 (2021-08-04)

Bug Fixes

  • revert "Avoid creating new node processes by leveraging processAsync (#57)" (#58) (3a3e500)

1.8.0 (2021-08-04)

Features

  • avoid creating new node processes by leveraging processAsync (#57) (d5be238)
  • provide option to show console.logs from preprocessors (#53) (173620a)

1.7.0 (2021-06-01)

Features

1.6.0 (2021-05-25)

Features

1.5.0 (2021-04-22)

Features

  • all process env variables are passed down to child transform process (#40) (beecf48)

1.4.0 (2021-04-12)

Features

1.3.2 (2021-03-24)

Bug Fixes

  • use RawSourceMap for sourcemap (b056107)

1.3.1 (2021-03-19)

Bug Fixes

1.3.0 (2020-12-10)

Features

  • allow user to specify svelte.config path (540e986)

1.2.0 (2020-12-09)

Features

  • adds maxBuffer option that sets buffer limit for preprocess (bd80185), closes #20

1.1.5 (2020-09-02)

Bug Fixes

  • svelte.config.js should not be required unless preprocessing (6b3e567)

1.1.4 (2020-09-02)

Bug Fixes

  • use double quotes for windows (e45313d)

1.1.3 (2020-08-31)

Bug Fixes

  • error when path to preprocessor contains some characters like '&' (00e61d8)

1.1.2 (2020-08-16)

1.1.1 (2020-08-15)

Bug Fixes

  • add svelteconfig.js to files pattern (a0f57b3)

1.1.0 (2020-08-15)

Features

1.0.6 (2020-05-18)

Bug Fixes

  • raise unhandled rejection on child process as uncaught exception (ae64409), closes #5

1.0.5 (2020-03-05)

1.0.4 (2020-02-14)

Bug Fixes

  • replace sync-rpc due to leaving open handles in jest@25 (9024ca9)

1.0.3 (2019-12-05)

Bug Fixes

  • cosmiconfig was loading config from pkg if svelte key was defined (8783131)

1.0.2 (2019-12-05)

1.0.1 (2019-12-05)