【发布时间】:2019-05-15 18:22:38
【问题描述】:
几天来,我一直在尝试创建一个特定的测试,如果能深入了解我可能做错了什么,我将不胜感激。
我正在尝试模拟 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.
【问题讨论】:
-
我建议你试试
spyOn,就像const spy = jest.spyOn(userArray, 'filter').mockImplementation(() => throw new Error())一样。 jestjs.io/docs/en/jest-object#jestspyonobject-methodname -
我已经设置了测试repl.it/repls/MedicalIntelligentTriangles,它似乎工作正常
-
@Teneff 嗯,这很奇怪。您在上面发布的 repl.it 示例似乎也对我有用。但是,当我在本地运行此测试时,我仍然会遇到错误:( 我已经编辑了帖子以包含我遇到的错误。
标签: javascript node.js unit-testing jestjs