I am facing an issue with NestJS and Jest testing. As a newcomer to NestJS, I encountered the "Cannot find module" error when running tests.
Specifically, the error message I received while trying to test my service was:
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 same error is occurring in various tests across different controllers and services.
Below is the code I am attempting 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;
}
Here 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 attempted changing my rootDir to "./src" but that did not resolve the issue.
Upon generating the project using `nest new`, my Jest settings remained default. It's possible that I may have made an error with my custom repository mocking in the tests.