英文:
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('test something', () => {
describe('all the test here', () => {
<all tests here>
});
describe('close DB connection', () => {
it('close DB connection', () => {
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论