当我尝试在异步函数上使用toThrow()时,Jest Test记录了一个错误(但仍通过测试)

10110

我正在尝试使用Jest测试异步功能。到目前为止,我似乎通过了三个测试。

但是,测试完成运行后,我得到一个错误UnhandledPromiseRejectionWarning: Error: expect(received).toThrow(expected)...(请参阅下面的完整日志)。我已经尝试过在此处以及在GitHub上这样的其他地方发布的多种解决方案,但是我无法摆脱错误。

我有以下异步功能:

async function aliasIsAdmin(alias, roles = DEFAULT_ROLES) {
  try {
    const url = "https://xxxxxx.execute-api.us-west-2.amazonaws.com/local/xxxxx/";
    if (typeof alias !== "string") {
      throw new Error('Entity "alias" must be a string.');
    }

    if(!Array.isArray(roles)){
      throw new Error('Entity "roles" must be an array but got "' + typeof roles + '". Permission Denied.');
    } 
    else if(roles.some((role) => typeof role !== "string")){
      throw 'Entity "roles" must be an array of strings but got at least one element of type: "' + typeof alias + '". Permission Denied.';
    }

    let { data } = (await axios.get(url + alias)); //Fetch roles that the alias holds.
    return data.some((role) => roles.includes(role)); //If alias has any of the roles in the policy, grant access.

  } catch (error) {
    console.error("Error when attempting to authorize user @" + alias
      + ". Access for this user has been denied, please try again.\nError:\n" + error);
    return false;
  }
}

我编写了以下测试:

const { aliasIsAdmin, getRolesByAlias } = require("../authorization");

describe('aliasIsAdmin', () => {
    it('should return true', () => {
        expect.assertions(1);
        return aliasIsAdmin("anAdmin")
        .then((r => {expect(r).toBe(true)}));
      });
    
    it('should return false', () => {
    expect.assertions(1);
    return aliasIsAdmin("notAnAdmin")
    .then((r => {expect(r).toBe(false)}));
    });

    it('should throw', () => {
        aliasIsAdmin(1)
        .then((r => {expect(r).toThrow('Entity "alias" must be a string.')}));
        });
});

当我运行测试时,我得到:

(node:94497) UnhandledPromiseRejectionWarning: Error: expect(received).toThrow(expected)

Matcher error: received value must be a function

Received has type:  boolean
Received has value: false
(node:94497) 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 .catch(). (rejection id: 1)
(node:94497) [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.
PASS  public/utils/test/authorization.test.js
  aliasIsAdmin
    ✓ should return true (895 ms)
    ✓ should return false (238 ms)
    ✓ should throw (37 ms)

  console.error
    Error when attempting to authorize user @1. Access for this user has been denied, please try again.
    Error:
    Error: Entity "alias" must be a string.

      41 | 
      42 |   } catch (error) {
    > 43 |     console.error("Error when attempting to authorize user @" + alias
         |             ^
      44 |       + ". Access for this user has been denied, please try again.\nError:\n" + error);
      45 |     return false;
      46 |   }

      at aliasIsAdmin (public/utils/authorization.js:43:13)
      at Object.<anonymous> (public/utils/test/authorization.test.js:17:9)
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total

我的测试命令是: jest --coverage public/utils/test

EOL

如错误消息所述,toThrow期望函数作为其接收值,在您的情况下,这是一个Promise解析的布尔值,因为您在aliasIsAdmincatch块中捕获了所有错误并返回false

如果更改catch块中的逻辑并重新引发错误,则可以在单元测试中使用以下内容:

it('should throw', async () => {
      await expect(aliasIsAdmin(1)).rejects.toThrow('Entity "alias" must be a string.');
});

如果当前逻辑是正确的,并且您实际上并不希望引发错误,则需要更改单元测试以检查已解析的false布尔值:

it('should return false if non-string parameter is passed', async () => {
      const result = await aliasIsAdmin(1);
      expect(result).toBe(false);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

LeetCode - 为什么这个函数返回 undefined,尽管它刚刚记录了一个数组?

来自分类Dev

当我尝试使用递归时函数名称未定义错误

来自分类Dev

当我尝试使用hideColumns时,Google可视化错误“ this.Ca.RZ不是函数”

来自分类Dev

当我尝试从函数中的脚本中使用变量时出现错误

来自分类Dev

当我在函数中打印第一个值后尝试打印f时出现C分段错误

来自分类Dev

每当我尝试创建一个新的反应函数时,Undefined 都不是一个函数

来自分类Dev

当我尝试使用过滤器删除项目时,我得到了item.map不是一个函数

来自分类Dev

当我尝试插入新记录时,Hibernate 使用更新

来自分类Dev

当我给出更多然后一个测试用例时,这显示错误的输出

来自分类Dev

访问查询工作正常,但是当我尝试报告时,我得到一个错误

来自分类Dev

当我尝试创建角色时,它给我一个错误(discord.js)

来自分类Dev

当我在c中使用strlcpy函数时,编译器给我一个错误

来自分类Dev

在进行硒测试时 - 我收到一个错误

来自分类Dev

当我尝试使用C中的参数调用函数时,出现错误“错误:...之前的预期表达式”

来自分类Dev

当我在另一个函数中使用setTimeout并尝试打印传递的参数的值时,到底发生了什么?

来自分类Dev

当我使用预定义函数定义另一个函数时,RaiseError被调用

来自分类Dev

当我使用错误?尝试,错误需要一个值

来自分类Dev

Yargs记录了一个标志

来自分类Dev

Yargs记录了一个标志

来自分类Dev

使用hiredis通过一个呼叫发送多个记录

来自分类Dev

当我尝试从一个JList到另一个JList的getSelectedValue()时出现错误

来自分类Dev

当我尝试使用此函数从我的数据库获取数据时出现错误(我在 android studio 中使用 kotlin)

来自分类Dev

当我尝试向扩展另一个类的类添加新的构造函数时,为什么会有此错误消息?

来自分类Dev

在等待下一个之后,当我尝试使用serilog LogContext时,它不会推送任何属性来记录消息

来自分类Dev

当我尝试使用OpenProjectAsync打开项目时,为什么msbuild会给我一个“不支持语言” C#”的错误

来自分类Dev

当我尝试使用递归打印 1-10 个数字时,mingw gcc 编译器给出了一个我无法理解的错误

来自分类Dev

当我声明一个ajax函数时出错

来自分类Dev

我无法找出一个错误,即当我尝试单击我网站上的链接时,它出现 404

来自分类Dev

当我尝试使用我的 Json 时,它只显示第一个值(标题)目标 c

Related 相关文章

  1. 1

    LeetCode - 为什么这个函数返回 undefined,尽管它刚刚记录了一个数组?

  2. 2

    当我尝试使用递归时函数名称未定义错误

  3. 3

    当我尝试使用hideColumns时,Google可视化错误“ this.Ca.RZ不是函数”

  4. 4

    当我尝试从函数中的脚本中使用变量时出现错误

  5. 5

    当我在函数中打印第一个值后尝试打印f时出现C分段错误

  6. 6

    每当我尝试创建一个新的反应函数时,Undefined 都不是一个函数

  7. 7

    当我尝试使用过滤器删除项目时,我得到了item.map不是一个函数

  8. 8

    当我尝试插入新记录时,Hibernate 使用更新

  9. 9

    当我给出更多然后一个测试用例时,这显示错误的输出

  10. 10

    访问查询工作正常,但是当我尝试报告时,我得到一个错误

  11. 11

    当我尝试创建角色时,它给我一个错误(discord.js)

  12. 12

    当我在c中使用strlcpy函数时,编译器给我一个错误

  13. 13

    在进行硒测试时 - 我收到一个错误

  14. 14

    当我尝试使用C中的参数调用函数时,出现错误“错误:...之前的预期表达式”

  15. 15

    当我在另一个函数中使用setTimeout并尝试打印传递的参数的值时,到底发生了什么?

  16. 16

    当我使用预定义函数定义另一个函数时,RaiseError被调用

  17. 17

    当我使用错误?尝试,错误需要一个值

  18. 18

    Yargs记录了一个标志

  19. 19

    Yargs记录了一个标志

  20. 20

    使用hiredis通过一个呼叫发送多个记录

  21. 21

    当我尝试从一个JList到另一个JList的getSelectedValue()时出现错误

  22. 22

    当我尝试使用此函数从我的数据库获取数据时出现错误(我在 android studio 中使用 kotlin)

  23. 23

    当我尝试向扩展另一个类的类添加新的构造函数时,为什么会有此错误消息?

  24. 24

    在等待下一个之后,当我尝试使用serilog LogContext时,它不会推送任何属性来记录消息

  25. 25

    当我尝试使用OpenProjectAsync打开项目时,为什么msbuild会给我一个“不支持语言” C#”的错误

  26. 26

    当我尝试使用递归打印 1-10 个数字时,mingw gcc 编译器给出了一个我无法理解的错误

  27. 27

    当我声明一个ajax函数时出错

  28. 28

    我无法找出一个错误,即当我尝试单击我网站上的链接时,它出现 404

  29. 29

    当我尝试使用我的 Json 时,它只显示第一个值(标题)目标 c

热门标签

归档