首页 > 解决方案 > 如何使用 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.

标签: javascriptnode.jsunit-testingjestjs

解决方案


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 filteron并且测试按预期通过。userArrayfilterUsersByEmailDomain


推荐阅读