javascript - 如何使用 Jest 成功模拟和捕获错误?
问题描述
几天来,我一直在尝试创建一个特定的测试,如果能深入了解我可能做错了什么,我将不胜感激。
我正在尝试模拟 Array 过滤器函数以引发错误。
userHelper.js
//filter users by email ending
const filterUsersByEmailDomain = (usersArray, emailEnding) => {
try {
let bizUsers = usersArray.filter(user => {
return user.email.endsWith(emailEnding);
});
return bizUsers;
} catch (err) {
console.log('error filtering users. Throwing error.');
throw err;
}
}
userHelper.test.js:
it('should throw', () => {
const user1 = {id: 1, email: 'tyler@tyler.com'};
const user2 = {id: 2, email: 'tevin@tevin.biz'};
const userArray = [user1, user2];
const domainEnding = '.biz';
Array.prototype.filter = jest.fn().mockImplementation(() => {throw new Error()});
expect(() => {usersHelper.filterUsersByEmailDomain(userArray, domainEnding)}).toThrow();
});
据我所知,错误正在被抛出,但没有被成功捕获。我也尝试过在 try catch 块中调用 usersHelper.filterUsersByEmailDomain(),就像我看到其他人所做的那样,但也没有成功。提前致谢!
编辑:这是我在项目中本地运行此测试设置时收到的错误。
● Testing the usersHelper module › should throw
56 | const domainEnding = '.biz';
57 |
> 58 | Array.prototype.filter = jest.fn().mockImplementation(() => {throw new Error()});
| ^
59 |
60 | expect(() => {usersHelper.filterUsersByEmailDomain(userArray, domainEnding)}).toThrow();
61 | });
at Array.filter.jest.fn.mockImplementation (utils/__tests__/usersHelper.test.js:58:76)
at _objectSpread (node_modules/expect/build/index.js:60:46)
at Object.throwingMatcher [as toThrow] (node_modules/expect/build/index.js:264:19)
at Object.toThrow (utils/__tests__/usersHelper.test.js:60:87)
(node:32672) UnhandledPromiseRejectionWarning: Error
(node:32672) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .c
atch(). (rejection id: 2)
(node:32672) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案
Array.prototype.filter
是一个非常低级的函数,模拟它以引发错误可能会导致您的测试无法正常运行。
做这个简单的测试:
it('should throw', () => {
expect(() => { throw new Error() }).toThrow(); // Success!
});
...效果很好...
...但是模拟Array.prototype.filter
抛出错误并且失败:
it('should throw', () => {
Array.prototype.filter = jest.fn(() => { throw new Error() });
expect(() => { throw new Error() }).toThrow(); // Fail!
});
相反,只需模拟filter
数组本身:
it('should throw', () => {
const user1 = { id: 1, email: 'tyler@tyler.com' };
const user2 = { id: 2, email: 'tevin@tevin.biz' };
const userArray = [user1, user2];
const domainEnding = '.biz';
userArray.filter = () => { throw new Error() }; // <= mock filter on userArray
expect(() => { usersHelper.filterUsersByEmailDomain(userArray, domainEnding) }).toThrow(); // Success!
});
JavaScript 在检查其原型之前会在对象本身上查找一个属性,以便调用mock filter
on并且测试按预期通过。userArray
filterUsersByEmailDomain
推荐阅读
- firebase - 如何从自定义身份提供者的响应参数中提取数据
- eclipse - Eclipse 更新 ==> “没有 id org.python.pydev.editor.PythonEditor 的编辑器描述符”
- php - 带有 html 输入的 MYSQL 查询
- servlets - 嵌套的 ContextHandler 未按预期运行
- html - 如何在没有 div 的情况下通过 css 将图像放在内联 svg 之上?
- python - Pytest - 如何访问所有收集的测试报告以进行报告?
- android - 如何在两个 Spannable 之间添加水平间距?
- javascript - jQuery newsticker 添加新列表 onclick 无法正常工作
- python - 从 YAML 文件传递 Python 函数
- r - 是否可以从模块返回反应式表达式?