typescript - 异步笑话断言没有失败测试,但断言错误打印在测试输出中
问题描述
我有一个返回承诺的提供者对象。我正在尝试为此提供程序对象创建开玩笑的单元测试。
这是一个示例测试:
it('Should return repos for a known account', (done) => {
expect.assertions(1);
expect(apiProvider.ReposForUser('joon').then((data) => data.length)).resolves.toBeGreaterThan(0).then(done());
});
我现在只是在提供程序类中用一个空白数组来解决承诺(即我希望测试失败):
export class GithubAPIProvider implements IGithubDataProvider {
public ReposForUser(githubUser: string): Promise<GithubRepoData[]> {
return new Promise<GithubRepoData[]>((resolve, reject) => {
resolve([]);
});
}
}
当我运行我的 jest 脚本时,我在输出文本中看到一个失败的 promise 错误,但测试显示已通过:
我在测试中使用了 done 回调,为什么 jest 不等待回调?
解决方案
弄清楚问题是什么。我在 then 方法中调用 done 函数,而不是传递对 done 方法的引用。
代码现在看起来像这样:[“done”而不是“done()”]:
it('Should return repos for a known account', (done) => {
expect.assertions(1);
expect(apiProvider.ReposForUser('joon').then((data) => data.length)).resolves.toBeGreaterThan(0).then(done);
});
推荐阅读
- state - 如何从其主体内更新视图的复杂模型
- android - 如何在 TextInputLayout 中为文本/标题/错误设置差异填充?
- c++ - TSubclassOf<> 不存储类类型
- python - 如何在python3中声明数组
- javascript - 尽管生产环境,IE11 中编译的 Vue 代码的控制台错误
- php - Laravel 路由组不会在 API 中返回数据
- svg - inkscape 扩展 - 隐蔽使用节点到路径节点
- c# - .NET Core 2.1 字符串。创建
- android - 在 travis(排毒)中测试 android.emu.release:-read-only 错误
- ios - 我可以在没有苹果开发者帐户的真实苹果设备上测试我的 Flutter 应用程序吗?如果可以,我该怎么做?