[英]How to cover TypeORM @Column decorator with Jest unit testing?
[英]How to cover a ManyToOne decorator with Jest?
我有一个带有 @ManyToOne 装饰器的实体。 问题是我的 Jest 单元测试没有覆盖那一行,即使它覆盖了其他装饰器。
实体(简体):
@Entity({ name: 'user' })
export default class User {
@PrimaryGeneratedColumn('uuid')
public id: string;
@Column({ name: 'username' })
@IsNotEmpty()
@MinLength(3)
@MaxLength(30)
public username: string;
@ManyToOne(() => Account)
@IsNotEmpty()
@JoinColumn({ name: 'account_id', referencedColumnName: 'id' })
public account: Account;
typeorm
也在__mocks__
文件夹中__mocks__
,导出使用的属性,包括:
export const ManyToOne = jest.fn();
我应该如何开玩笑地编写我的测试,以便它涵盖 ManyToOne 装饰器?
我的猜测是未覆盖的行不是带有装饰器的整行,而只是回调的主体。
因为你已经嘲笑了const ManyToOne = jest.fn();
您的装饰器是一个没有实现且不调用任何传递的回调的函数。
因此,如果您将其模拟为一个立即执行并返回给定回调结果的函数
export const ManyToOne = jest.fn(callback => callback());
您应该能够实现该线路的 100% 覆盖率
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.