# Jest configuration

First, go to the <mark style="color:purple;">package.json</mark> file. There, at the bottom of the file, we can see the <mark style="color:blue;">`jest`</mark> field with the configuration for it. Let's then cut this content and paste it into a new file we'll create at the root of the project: <mark style="color:purple;">jest.config.cjs</mark>.

{% hint style="info" %}
The extension <mark style="color:purple;">.cjs</mark> is basically the same as <mark style="color:purple;">.js</mark>, but enforcing **imports** as <mark style="color:blue;">`require()`</mark> (**CommonJS**), which is necessary for Jest here.
{% endhint %}

We should just change:

```javascript
"jest": {
  // Options
}
```

for:

```javascript
module.exports = {
  // Options
}
```

{% hint style="info" %}
In this case, we use a <mark style="color:purple;">.cjs</mark> (<mark style="color:red;">**JavaScript**</mark>) file because

* **JSON** would not allow for some function calls we'll make soon
* **TypeScript** would create a <mark style="color:purple;">src</mark> folder inside <mark style="color:purple;">dist</mark>, which would require adjustments, for instance, in the <mark style="color:blue;">`entities`</mark> and <mark style="color:blue;">`migrations`</mark> fields inside the <mark style="color:purple;">data-source</mark> file
  {% endhint %}

After that, save the file, and its syntax will automatically be adjusted from JSON to JS. And to fix the error, add the name of this file to the <mark style="color:blue;">`ignores`</mark> field in the file <mark style="color:purple;">eslint.config.mjs</mark>. Let's also already add another name that will be necessary soon.

```javascript
'jest.config.cjs', 'test/jest-e2e.cjs'
```

Then add the following imports at the top of the file:

```javascript
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig.json');
```

And add the following fields below <mark style="color:blue;">`rootDir`</mark>:

```javascript
modulePaths: ['<rootDir>'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
  prefix: '<rootDir>',
}),
```

This will make sure that **Jest** resolves the paths correctly. However, unfortunately we cannot use that path in the <mark style="color:purple;">tsconfig</mark> for the domain's entities in that fashion anymore, so we'll have to use the <mark style="color:blue;">`paths`</mark> more explicitly.

```json
"categories/*": ["domain/categories/*"],
"orders/*": ["domain/orders/*"],
"payments/*": ["domain/payments/*"],
"products/*": ["domain/products/*"],
"users/*": ["domain/users/*"]
```

If we now run the same test, that error will no longer happen. However, a new one will appear, suggesting that the dependencies cannot be resolved. This one will be solved when we write the tests themselves.

Let's also already configure the **e2e** tests. So, let's alter the extension of the file <mark style="color:purple;">test</mark>/<mark style="color:purple;">jest-e2e.json</mark> to <mark style="color:purple;">.cjs</mark> too and use the same imports as the other file.

However, there will be a subtle difference: the path to access the <mark style="color:purple;">tsconfig</mark> file should go up a folder, so it should be <mark style="color:blue;">`'../tsconfig.json'`</mark>. Also, we should go to the <mark style="color:purple;">package.json</mark> file and change the script <mark style="color:blue;">`test:e2e`</mark> to make it use the correct extension, so it should have <mark style="color:blue;">`.cjs`</mark> at the end instead of <mark style="color:blue;">`.json`</mark>.

Then, back in the <mark style="color:purple;">jest-e2e.cjs</mark> file, put the following path at the start:

```javascript
const modulePath = '<rootDir>/../src';
```

So that we can put below <mark style="color:blue;">`rootDir`</mark>:

```javascript
modulePaths: [modulePath],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
  prefix: modulePath,
}),
```

Now, relocate the file <mark style="color:purple;">test</mark>/<mark style="color:purple;">app.e2e-spec</mark> to a <mark style="color:purple;">domain</mark> folder we'll create in the same folder, and change the filename to <mark style="color:purple;">users.e2e-spec</mark>. In a future moment, we'll create the **e2e tests** for users here.

Lastly, let's install a package that will be very useful for **mocking**, and also the **faker** library to create **fake data** quickly.

```sh
yarn add -D jest-mock-extended @faker-js/faker
```

Now, before actually going to the tests themselves, let's just simplify the **users resource**.

<mark style="color:green;">**Commit**</mark> - Jest configuration


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/extra-module-6-automated-testing/jest-configuration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
