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

huangapple go评论81阅读模式
英文:

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!

英文:

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 &#39;@nestjs/testing&#39;;
import { PlaceController } from &#39;./place.controller&#39;;
import { PlaceService } from &#39;./place.service&#39;;
import { expect } from &#39;chai&#39;;

describe(&#39;PlaceController&#39;, () =&gt; {
  let appController: PlaceController;

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

    appController = app.get&lt;PlaceController&gt;(PlaceController);
  });

  it(&#39;should be defined&#39;, () =&gt; {
    expect(appController).toBeDefined();
  });
});

My jest.config.ts looks like this:

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

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

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

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

{
  &quot;extends&quot;: &quot;../../tsconfig.json&quot;,
  &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;test&quot;, &quot;dist&quot;]
}

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

Cannot find module &#39;@server/app/api/place/place.dto&#39; from &#39;src/server/app/api/place/place.controller.ts&#39;

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

    &gt; 1 | import { CreatePlaceDto } from &#39;@server/app/api/place/place.dto&#39;;
        | ^
      2 | import { PlaceService } from &#39;@server/app/api/place/place.service&#39;;
      3 | import { CrudRequest } from &#39;@server/app/core/crud/crud&#39;;
      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!

答案1

得分: 1

如果您直接使用'./'而不是src来显示路径,问题将得以解决。

英文:

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

huangapple
  • 本文由 发表于 2023年6月22日 11:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定