npm package: @lottiefiles/jest-config
Description: Shared Jest config for LottieFiles projects
Author: LottieFiles
Latest version: 1.0.0

# @lottiefiles/jest-config

> Shared configuration for [jest](https://jestjs.io/).

## Table of contents

* [Installation](#installation)
  * [Install Dependencies](#install-dependencies)
* [Configuration](#configuration)
  * [Monorepos](#monorepos)
  * [ESM projects](#esm-projects)
* [Tips and Tricks](#tips-and-tricks)
* [Changelog](#changelog)

## Installation

1. Install the @lottiefiles/jest-config to the project with pnpm.

```sh
pnpm add -D @lottiefiles/jest-config
```

2. Create a `jest.config.cjs` file in the project with the contents:

```js
const { createConfig } = require('@lottiefiles/jest-config');

const packageJson = require('./package.json');

module.exports = {
  ...createConfig(),

  name: packageJson.name,
  displayName: packageJson.name,
};
```

### Install Dependencies

The following must be installed as a dev dependency in your project.

#### Peer Dependencies

| Package Name | Version |
| ------------ | ------- |
| esbuild      | \*      |
| jest         | \*      |

## Configuration

### Monorepos

The recommended setup for a monorepo project is:

1. Install `jest` and `esbuild` as a workspaces root dev dependency.

```sh
pnpm add -DW jest @types/jest esbuild
```

2. Create a `jest.config.cjs` in the workspace root.

```js
module.exports = {
  // Setup projects
  projects: ['<rootDir>/packages/*'],
};
```

3. Install `@lottiefiles/jest-config` as a project dev dependency in each of the projects you are setting up `jest` for.

```sh
pnpm add -D @lottiefiles/jest-config
```

4. Create a `jest.config.cjs` file in each of the projects.

```js
const { createConfig } = require('@lottiefiles/jest-config');

const packageJson = require('./package.json');

module.exports = {
  ...createConfig(),

  name: packageJson.name,
  displayName: packageJson.name,
};
```

You can then customize this file to fit the needs of each project individually.

### ESM projects

1. Pass in the transform format and transform target arguments to the `createConfig` function to configure `jest` for processing and treating the files as ESM.

```js
...createConfig({
    transformFormat: 'esm',
    transformTarget: 'es2020'
  }),
```

2. Make sure you invoke jest with running `node` with ES module support. You can do this by setting the NODE\_OPTIONS environment variable to `--experimental-vm-modules`.

For e.g., the "test" script command can be:

```json
{
  // ...
  "scripts": {
    "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
  }
}
```

## Tips and Tricks

#### Mocking objects and functions, with dynamic return values

Use [`jest-mock-extended`](https://www.npmjs.com/package/jest-mock-extended)

#### Recording HTTP calls

Use [`PollyJS`](https://github.com/spotify/polly-jest-presets)

#### Running heavy and long running tasks under forked processes in parallel

Use [`jest-worker`](https://www.npmjs.com/package/jest-worker)

#### Creating spies for classes

Use [`jest-auto-spies`](https://www.npmjs.com/package/jest-auto-spies)

#### Starting a server before running tests

Use [`jest-dev-server`](https://www.npmjs.com/package/jest-dev-server)

#### Running tests all in a browser environment

Use Playwright Test instead of Jest. See [playwright.dev](https://playwright.dev/docs/intro)

## Changelog

See [CHANGELOG.md](CHANGELOG.md) for the latest changes.