npmtypescript95% confidence\u2191 49

NestJS - Test suite failed to run Cannot find module 'src/article/article.entity' from 'comment/comment.entity.ts'

Full error message
i need help with nestjs and jest testing. I am new to NestJS and i got stuck on Cannot find module error when i run tests.

I am trying to test my service and when i run tests i have received error message:

src/article/article.service.spec.ts
● Test suite failed to run

Cannot find module 'src/article/article.entity' from 'comment/comment.entity.ts'

Require stack:
  comment/comment.entity.ts
  article/article.entity.ts
  article/article.service.spec.ts

   6 |   ManyToOne,
   7 | } from 'typeorm';
>  8 | import { Article } from 'src/article/article.entity';
     | ^
   9 | 
  10 | @Entity()
  11 | export class Comment {

  at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:307:11)
  at Object.<anonymous> (comment/comment.entity.ts:8:1)

This similiar error is showing up through all other tests in different controllers, services etc.

There is my code what i am trying to test.

article.service.ts

import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Article } from "./article.entity";
import { ArticleRepository } from "./article.repository";
import { ArticleDTO } from "./dto/article.dto";
import { DeleteResult } from "typeorm";
import { ArticleRO } from "./dto/article.response";
import { UserRepository } from "src/user/user.repository";

@Injectable()
export class ArticleService {
  constructor(
    private readonly articleRepository: ArticleRepository,
    private readonly userRepository: UserRepository
  ) {}

  async getAllPosts(): Promise<ArticleRO[]> {
    return await this.articleRepository.find();
  }
}

article.repository.ts

import { Repository, EntityRepository } from 'typeorm';
import { Article } from './article.entity';
@EntityRepository(Article)
export class ArticleRepository extends Repository<Article> {
}

article.service.specs.ts

import { Test, TestingModule } from "@nestjs/testing";
import { getRepositoryToken } from "@nestjs/typeorm";
import { Article } from "./article.entity";
import { ArticleRepository } from "./article.repository";
import { ArticleService } from "./article.service";
import { ArticleRO } from "./dto/article.response";

describe("PostService", () => {
  let service: ArticleService;
  let articleRepository: ArticleRepository;
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        ArticleService,
        {
          provide: getRepositoryToken(Article),
          useClass: ArticleRepository,
        },
      ],
    }).compile();

    service = module.get<ArticleService>(ArticleService);
    articleRepository = module.get<ArticleRepository>(
      getRepositoryToken(Article)
    );
  });

  it("should be defined", () => {
    expect(service).toBeDefined();
  });
  describe("findAll", () => {
    it("should return an array of cats", async () => {
      const result: ArticleRO[] = [];
      jest.spyOn(service, "getAllPosts").mockResolvedValueOnce(result);

      expect(await service.getAllPosts()).toBe(result);
    });
  });
});

comment.entity.ts

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  ManyToOne,
} from 'typeorm';
import { Article } from 'src/article/article.entity';

@Entity()
export class Comment {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  author: string;

  @Column()
  content: string;

  @CreateDateColumn()
  createdAt: Date;

  @ManyToOne(
    () => Article,
    article => article.comments,
  )
  article: Article;
}

there are my jest settings from package.json.

"jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }

I tried to change my rootDir to "./src" but this does not work.

I generated project with nest new blabla. So my jest settings are default. Maybe i am doing something wrong with my custom repository mocking in tests.

You can tell Jest how to resolve module paths by configuring the moduleNameMapper option, which is useful if you're using packages like module-alias or if you're using absolute paths. Add these lines to your Jest configuration: { // ... "jest": { // ... "moduleNameMapper": { "^src/(.*)$": "<rootDir>/$1" } } } Now modules that start with src/ will be looked into <rootDir>/, which is the src folder by default (this configured a few lines above). Check out the documentation link above to get ideas of everything you can do with this. I don't know what's wrong with using absolute paths as the other reply said, it's not only useful but it also works just fine in both NestJS and in Angular.

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/b7f75c69ac4bd2be457d7e82ff3ea44856394eb30eb8a53148f13c44d84f7093
hash \u00b7 b7f75c69ac4bd2be457d7e82ff3ea44856394eb30eb8a53148f13c44d84f7093
NestJS - Test suite failed to run Cannot find module &#39;sr… — DepScope fix | DepScope