【发布时间】:2019-09-08 17:30:59
【问题描述】:
这里是 Jest 的新手,不知道该怎么做:
假设我有一些包含const 的函数,我已将其设置为特定类型 (newArtist):
export class myTestClass {
async map(document: string) {
const artist: newArtist = document.metadata.artist;
...
}
}
除此之外,我还有:
export interface newArtist {
name: string;
title: string;
}
现在,当我写一个测试时,如果我确实这样说:
it("My example test", async () => {
const result: any = await new myTestClass(__context()).map({
name: "An Artist"
title: null
});
...
}
测试将失败,因为 title 设置为 null。为了测试的目的,我需要该界面有点不同 - 基本上是这样的:
export interface newArtist {
name: string;
title: string | null;
}
我该怎么做?我见过模拟类,但这是否意味着我最终会复制/粘贴所有 map 函数代码?
任何帮助表示赞赏。
谢谢。
【问题讨论】:
-
你确定原因是关于
null和类型检查?对我来说,您传递的数据结构不相关:而不是具有metadata类型的object属性的string,而是直接传递object。
标签: typescript jestjs