Jest configuration

Configuring Jest and installing some libraries.

First, go to the package.json file. There, at the bottom of the file, we can see the jest 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: jest.config.js. We should just change:

"jest": {
  // Options
}

for:

module.exports = {
  // Options
}

In this case, we use a JavaScript file because

  • JSON would not allow for some function calls we'll make soon

  • TypeScript would create a src folder inside dist, which would require adjustments, for instance, in the entities and migrations fields inside the data-source file

After that, hover the cursor over the error and click on Quick Fix -> Fix all prettier/prettier problems.

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

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

And add the following fields below rootDir:

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 tsconfig for the domain's entities in that fashion anymore, so we'll have to use the paths more explicitly.

"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 test -> jest-e2e.json to .js too and use the same imports as the other file.

However, there will be a subtle difference: the path to access the tsconfig file should go up a folder, so it should be '../tsconfig.json'. Also, we should go to the package.json file and change the script test:e2e to make it use the correct extension, so it should have .js at the end instead of .json.

Then, back in the jest-e2e.json file, put the following path at the start:

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

So that we can put below rootDir:

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

Now, relocate the file test -> app.e2e-spec to a domain folder we'll create in the same folder, and change the filename to users.e2e-spec. In a future moment, we'll create the e2e tests for users here.

Lastly, let's install a package that will be very useful in mocking.

yarn add -D jest-mock-extended

And also the faker library, to create fake data quickly.

yarn add -D @faker-js/faker

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

Commit - Jest configuration

Last updated