如何使用 Jest 成功模拟并捕获错误?

tjo*_*jol 3 javascript unit-testing node.js jestjs

几天来我一直在尝试创建一个特定的测试,如果您能深入了解我可能做错了什么,我将不胜感激。

\n\n

我正在尝试模拟数组过滤器函数以引发错误。

\n\n

用户助手.js

\n\n
//filter users by email ending\nconst filterUsersByEmailDomain = (usersArray, emailEnding) => {\n    try {\n        let bizUsers = usersArray.filter(user => {\n            return user.email.endsWith(emailEnding);\n        });\n        return bizUsers;\n    } catch (err) {\n        console.log('error filtering users. Throwing error.');\n        throw err;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

userHelper.test.js:

\n\n
it('should throw', () => {\n        const user1 = {id: 1, email: 'tyler@tyler.com'};\n        const user2 = {id: 2, email: 'tevin@tevin.biz'};\n        const userArray = [user1, user2];\n        const domainEnding = '.biz';\n\n        Array.prototype.filter = jest.fn().mockImplementation(() => {throw new Error()});\n\n        expect(() => {usersHelper.filterUsersByEmailDomain(userArray, domainEnding)}).toThrow();\n    });\n
Run Code Online (Sandbox Code Playgroud)\n\n

据我所知,错误被抛出,但没有成功被捕获。我还尝试在 try catch 块内调用 usersHelper.filterUsersByEmailDomain() ,就像我看到其他人所做的那样,但也没有成功。提前致谢!

\n\n

编辑:\n这是我在项目中本地运行此测试设置时收到的错误。

\n\n
  \xe2\x97\x8f Testing the usersHelper module \xe2\x80\xba should throw\n\n\n\n      56 |         const domainEnding = '.biz';\n      57 | \n    > 58 |         Array.prototype.filter = jest.fn().mockImplementation(() => {throw new Error()});\n         |                                                                            ^\n      59 | \n      60 |         expect(() => {usersHelper.filterUsersByEmailDomain(userArray, domainEnding)}).toThrow();\n      61 |     });\n\n      at Array.filter.jest.fn.mockImplementation (utils/__tests__/usersHelper.test.js:58:76)\n      at _objectSpread (node_modules/expect/build/index.js:60:46)\n      at Object.throwingMatcher [as toThrow] (node_modules/expect/build/index.js:264:19)\n      at Object.toThrow (utils/__tests__/usersHelper.test.js:60:87)\n\n(node:32672) UnhandledPromiseRejectionWarning: Error\n(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\natch(). (rejection id: 2)\n(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.\n
Run Code Online (Sandbox Code Playgroud)\n

Bri*_*ams 5

Array.prototype.filter是一个非常低级的函数,模拟它引发错误可能会导致您的测试无法正常运行。

进行这个简单的测试:

it('should throw', () => {
  expect(() => { throw new Error() }).toThrow();  // Success!
});
Run Code Online (Sandbox Code Playgroud)

...效果很好...

...但是模拟Array.prototype.filter抛出错误并且失败:

it('should throw', () => {
  Array.prototype.filter = jest.fn(() => { throw new Error() });
  expect(() => { throw new Error() }).toThrow();  // Fail!
});
Run Code Online (Sandbox Code Playgroud)

相反,只需模拟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!
});
Run Code Online (Sandbox Code Playgroud)

JavaScript 在检查其原型之前会查找对象本身的属性,以便调用filter模拟并且测试按预期通过。userArrayfilterUsersByEmailDomain