【问题标题】:How to handle async Rest Error in unit test cases using jest如何使用 jest 处理单元测试用例中的异步 Rest 错误
【发布时间】:2021-09-27 21:22:59
【问题描述】:

以下是我的节点js测试文件代码,这是一个单元测试用例,它失败了。请在下面找到详细的代码和错误消息。

jest.unmock('./utils.js');
describe('test', () => {
     it('test', async (done) => {
        await expect(getAPISecretKey('testKey')).rejects.toEqual('RestError: AKV10000: Request is missing a Bearer or PoP token.');
     });
 });

失败并出现以下错误

FAIL  src/utils.test.js
  ● test › test

    expect(received).rejects.toEqual(expected) // deep equality

    Expected: ["RestError: AKV10000: Request is missing a Bearer or PoP token."]
    Received: [RestError: AKV10000: Request is missing a Bearer or PoP token.]

       6 |      it('test', async (done) => {
    >  7 |         await expect(getAPISecretKey('testKey')).rejects.toEqual(error);
         |                                                          ^
       8 |      });
       9 |  });
      10 | 

      at Object.toEqual (node_modules/expect/build/index.js:241:20)
      at Object.<anonymous> (src/utils.test.js:7:58)

我尝试了以下方法,但还是不行。

jest.unmock('./utils.js');
describe('test', () => {
     var error = ['RestError: AKV10000: Request is missing a Bearer or PoP token.'];
     it('test', async (done) => {
        await expect(getAPISecretKey('testKey')).rejects.toEqual(new Error('Request is missing a Bearer or PoP token.'));
     });
 });

getAPISecretKey 代码

async function getAPISecretKey(secretNameStr) {
    let credentials = getKeyVaultCredentials();
    let keyVaultClient = new SecretClient(KEY_VAULT_URL, credentials);
    let secret = await keyVaultClient.getSecret(secretNameStr);
    return secret.value;
}

尝试了以下方式并给出如下错误

jest.unmock('./utils.js');
describe('test', () => {
  //toEqual('RestError: AKV10000: Request is missing a Bearer or PoP token.');
     it('test', async (done) => {
        await expect(getAPISecretKey('testKey')).rejects.toThrow('AKV10000: Request is missing a Bearer or PoP token.');
     });
 });

错误:

 FAIL  src/utils.test.js (14.891 s)
  ● test › test

    : Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error:

      4 | describe('test', () => {
      5 |   //toEqual('RestError: AKV10000: Request is missing a Bearer or PoP token.');
    > 6 |      it('test', async (done) => {
        |      ^
      7 |         await expect(getAPISecretKey('testKey')).rejects.toThrow('AKV10000: Request is missing a Bearer or PoP token.');
      8 |       

      at new Spec (node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
      at Suite.<anonymous> (src/utils.test.js:6:6)
      at Object.<anonymous> (src/utils.test.js:4:1)

【问题讨论】:

  • 您需要出示getAPISecretKey的代码
  • @slideshowp2 ,使用请求的代码更新问题
  • 也许是toThrow 而不是toEqual?异常可能是一个对象,而您正在测试一个字符串。 “预期”和“接收”值看起来相同,因为 Error.toString 只是打印错误消息。
  • @Cully,感谢您的回复,我也尝试过,但收到错误“超时 - 在 jest.setTimeout.Timeout 指定的 5000 毫秒超时内未调用异步回调 - 异步回调是在 jest.setTimeout.Error 指定的 5000 毫秒超时内未调用:”我还更新了问题。请看
  • 你必须await getAPISecretKey吗?它是异步的。看起来您只是将 Promise 传递给 expect,而不是 getAPISecretKey 的结果。实际上,nm,看起来你可能不需要 .rejects

标签: javascript node.js unit-testing jestjs


【解决方案1】:

我可以通过从异步方法参数中删除“完成”来解决问题,如下所示。

jest.unmock('./utils.js');
//jest.setTimeout(10000);
describe('test', () => {
     it('test', async () => {
        await expect(getAPISecretKey('testKey')).rejects.toThrow('AKV10000: Request is missing a Bearer or PoP token.');
      
     });
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 2021-02-17
    • 2018-03-24
    • 2017-04-18
    • 1970-01-01
    • 2017-04-14
    • 2021-05-21
    • 1970-01-01
    相关资源
    最近更新 更多