Mochajs 测试在使用 async/await 时仍然持续运行。

huangapple go评论98阅读模式
英文:

Mochajs test keep running when using async/await

问题

我正在使用mochajs来测试一个Express API。我有一些异步函数,因为我要查询数据库。我在这个github问题中读到,使用async/await应该可以工作。然而,测试一直在运行,我不得不手动关闭进程,尽管可以看到console.log中的Promise已经解决。

令人惊讶的是,如果我只运行一个先前的测试,该测试也使用相同的async/await模式,它会停止。唯一的区别是,在第二个测试中,被测试的函数中还有另一个await,但它也被正确解决。

it('当令牌不正确时返回错误', async () => {
    let result = await addSciStudy({token: 'fake'})
    console.log(result)
    expect(result).to.have.key('error')
});

// { error: '令牌不正确,请联系管理员' }

这是测试(第12行):https://github.com/icra/snappAPI-v2/blob/main/tests/test-add-sci-study.js

这是被测试的函数:https://github.com/icra/snappAPI-v2/blob/main/lib/add-sci-study.js

谢谢。

英文:

I'm using mochajs to test an express API. I have some async functions because I query a database. I read in this github issue that using async / await should work. However, the test keeps running and I have to manually close the process regardless that the promise is resolved as can be seen in the console.log.

Surprisingly, it stops if I only run a previous test that is using the same async / await pattern. The only difference is that, in the second, there is another await in the function tested, but it is also resolved properly.

       it('Returns error when token is not correct', async () => {
            let result = await addSciStudy({token: 'fake'})
            console.log(result)
            expect(result).to.have.key('error')
        });

// { error: 'Token is not correct, please contact the administrator' }

Here is the test (line 12): https://github.com/icra/snappAPI-v2/blob/main/tests/test-add-sci-study.js

And here the tested function: https://github.com/icra/snappAPI-v2/blob/main/lib/add-sci-study.js

Thank you.

答案1

得分: 0

我找到了问题。在被测试的函数中,有一个调用另一个查询数据库的函数。Mochajs 在等待连接关闭以停止测试。因此,我创建了一个关闭连接的函数,可以在测试脚本中导入:

const closeDB = function(){
    connection.end()
    return 0
}

问题是,一旦关闭连接,就不再打开。所以,如果其他测试在查询数据库,它们就会失败。我所做的是在所有测试运行完之后创建一个虚假的测试,以关闭连接:

describe('test something', () => {
    describe('all the test here', () => {
        <所有测试在此>
    });
    describe('close DB connection', () => {
        it('close DB connection', () => {
            closeDB()
        });
    });
});

唯一的缺点是当单独运行一个测试时,仍然需要手动停止测试。但当以编程方式运行测试时,它会在重要的情况下(例如,在部署之前使用 bash 脚本进行集成测试)停止测试。

英文:

I found the problem. In the tested function there was a call to another function that queries a database. Mochajs was waiting for the connection to close to stop the tests. So, I created a function to close the connection that I can import in the test scripts:

const closeDB = function(){
    connection.end()
    return 0
}

The problem is that when the connection is closed, then is no longer opened. So, if other tests where querying the database, they failed. What I did is to create a fake test at the end that close the connection once all test have run:

describe(&#39;test something&#39;, () =&gt; {
    describe(&#39;all the test here&#39;, () =&gt; {
        &lt;all tests here&gt;
    });
    describe(&#39;close DB connection&#39;, () =&gt; {
        it(&#39;close DB connection&#39;, () =&gt; {
            closeDB()
        });
    });
});

The only drawback is that when one test is run interactively, the test must still be manually stopped. But it stops when tests are run programatically, which is important if you want to integrate them in a bash script to test before deploy, for instance.

huangapple
  • 本文由 发表于 2023年7月28日 01:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定