gpt4 book ai didi

typescript - NestJS - 测试套件无法运行从 'src/article/article.entity' 找不到模块 'comment/comment.entity.ts'

转载 作者:行者123 更新时间:2023-12-03 18:28:30 26 4
gpt4 key购买 nike

我需要有关 nestjs 和 jest 测试的帮助。我是 NestJS 的新手,我在运行测试时遇到了找不到模块错误。
我正在尝试测试我的服务,当我运行测试时,我收到了错误消息:
src/article/article.service.spec.ts
● 测试套件无法运行

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)
这个类似的错误正在通过不同 Controller 、服务等中的所有其他测试出现。
我正在尝试测试我的代码。
文章.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();
}
}
文章.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);
});
});
});


评论实体.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;
}
package.json 中有我的 Jest 设置。
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
我试图将我的 rootDir 更改为“./src”,但这不起作用。
我用nest new blabla 生成了项目。所以我的 Jest 设置是默认的。也许我在测试中模拟我的自定义存储库时做错了。

最佳答案

不要在你的应用程序中使用像“src/article/article.entity”这样的路径,这些在编译后不可用。取而代之的是像“../../article/article.entity”这样的相对路径(这只是一个猜测的路径)......问候

关于typescript - NestJS - 测试套件无法运行从 'src/article/article.entity' 找不到模块 'comment/comment.entity.ts',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63865678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com