Search code examples
node.jstypescripttestingjestjsnestjs

When running Jest tests on a Nest project, my file fails to find a module that has no issues when running outside of tests


I have a NestJs file and I am trying to write some tests with Jest because I am trying to run some e2e tests on the endpoints. My test file sits on the same directory level of the controller. I am trying to test the PlaceController and my place.controller.spec.ts file is sitting on the same directory level as PlaceService and PlaceController.

The spec file looks like this:

import { Test, TestingModule } from '@nestjs/testing';
import { PlaceController } from './place.controller';
import { PlaceService } from './place.service';
import { expect } from 'chai';

describe('PlaceController', () => {
  let appController: PlaceController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [PlaceController],
      providers: [PlaceService],
    }).compile();

    appController = app.get<PlaceController>(PlaceController);
  });

  it('should be defined', () => {
    expect(appController).toBeDefined();
  });
});

My jest.config.ts looks like this:

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testMatch: ['**/**/*.spec.ts'],
  verbose: true,
  forceExit: true,
  clearMocks: true,
  resetMocks: true,
  restoreMocks: true,
  bail: false,
};

My tsconfig.json at the root of the project is this:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "./dist",
    "incremental": true,
    "sourceMap": true,
    "declaration": false,
    "removeComments": true,
    "downlevelIteration": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "importHelpers": true,
    "skipLibCheck": true,
    "target": "es2017",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "@app/*": [
        "./client/app/*"
      ],
      "@assets/*": [
        "./client/assets/*"
      ],
      "@env/*": [
        "./client/environments/*"
      ],
      "@common/*": [
        "./common/*"
      ],
      "@server/*": [
        "./server/*"
      ]
    }
  },
}

I don't know why, but there is another tsconfig file two levels up in the directory where all the controller/controller.spec files are. I didn't write this so I'd rather not touch it. But it looks like this

{
  "extends": "../../tsconfig.json",
  "exclude": ["node_modules", "test", "dist"]
}

To run the tests, I have "test": "NODE_ENV=test jest" in my package.json scripts. However, when I run npm run test, I got the following error:

Cannot find module '@server/app/api/place/place.dto' from 'src/server/app/api/place/place.controller.ts'

    Require stack:
      src/server/app/api/place/place.controller.ts
      src/server/app/api/place/place.controller.spec.ts

    > 1 | import { CreatePlaceDto } from '@server/app/api/place/place.dto';
        | ^
      2 | import { PlaceService } from '@server/app/api/place/place.service';
      3 | import { CrudRequest } from '@server/app/core/crud/crud';
      4 | import {

But when I run the app normally, not tests, everything works perfectly and my api returns the correct response when I call it in Postman. What can be causing the issue? Thank you!


Solution

  • If you show the path directly with './' instead of src, the problem will be solved.