How can I configure jest to handle SvelteKit's `$lib` alias?

Viewed 134

When I try to run a test in jest that imports a file using the $lib alias, jest fails to run with the error Cannot find module '$lib/...'.

1 Answers

Add the following to jest.config.cjs:

module.exports = {
  ...,
  moduleNameMapper: { '^\\$lib(.*)$': '<rootDir>/src/lib$1' },
}

The documentation can be found under Using with webpack.

Related