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.cjs.
We should just change:
for:
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 ignores
field in the file eslint.config.mjs. Let's also already add another name that will be necessary soon.
Then add the following imports at the top of the file:
And add the following fields below 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.
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 .cjs 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 .cjs
at the end instead of .json
.
Then, back in the jest-e2e.cjs file, put the following path at the start:
So that we can put below rootDir
:
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 for mocking, and also the faker library to create fake data quickly.
Now, before actually going to the tests themselves, let's just simplify the users resource.
Commit - Jest configuration
Last updated