【问题标题】:Not able to run test inside the async function in jest无法在开玩笑的异步函数中运行测试
【发布时间】:2018-07-13 03:43:22
【问题描述】:

我正在使用 jest 框架 (version 23.3.0) 来测试 javascript 代码,我在 async 函数中编写了一个测试,该函数没有被执行。

function fn(){
    return new Promise((resolve,rej)=>{
                  setTimeout(()=>{
                      resolve('done');

                  },500)
              })
   }

 async function fn1(){
   let a = await fn();
    test('audienceTest',()=>{
        console.log('test')
        return fn();   
    })
  }

  fn1();

在正常功能的情况下,测试执行良好。

【问题讨论】:

  • 你可以让测试本身异步并尝试吗? test('audienceTest', async()=>{ let a = await fn();
  • @SumanthKumarMora 我有一个场景,我正在制作describe function async,然后我需要运行测试,但是在使异步测试本身没有运行的描述函数之后。由于我们没有任何可以在 jest 中的 describe 函数之前运行的钩子,所以我只是在做一些工作。

标签: javascript node.js reactjs async-await jestjs


【解决方案1】:

如果你想运行一些async 函数并等待它完成,我们必须在这个函数之前使用await 字。在您的示例中:

await fn1();

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2020-10-08
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多