随机失败的测试jest和supertest Node.js [英] Randomly failing tests jest and supertest Node.js

查看:206
本文介绍了随机失败的测试jest和supertest Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这么长的故事简短我正在开发RESTapi,它将POST请求的电影标题带到/ movies路线,从外部api获取有关该电影的信息并将该对象保存到数据库。在POST /评论中,您可以为不同的集合添加评论,但每个评论都有一个'movie_id'属性,该属性具有关联的电影。



这是我的第一个更大的项目,所以我'我试图编写整合测试。
一切都很棒,至少在我看来,除了3个奇怪的测试用例,它们都是无处不在的。测试可以连续传递10次然后突然出现奇怪的'jest'计时器,3个案例失败。



我正在使用本机mongodb驱动程序,快速和开玩笑超级测试,丢弃测试数据库BeforeAll和AfterEach,我不知道是什么原因。



计时器东西:



在计时器出现后,测试失败:



完整的源代码在这里



任何想法,提示?

解决方案

我在同一个地狱开玩并行测试问题,我找到了解决方案,也许不是最好但现在开始在队列模式中运行测试,所以当我删除 beforeAll 中的数据时,我的下一组测试已准备好使用新的新插入数据。


- runInBand
别名:-i。在当前进程中以串行方式运行所有测试,而不是创建运行测试的子进程的工作池。这对于调试很有用。


jest source



所以在我的config.json中我有:

 scripts:{
test:set NODE_ENV = test&& jest ./tests --runInBand --detectOpenHandles --forceExit,
server:设置NODE_ENV =开发&& nodemon app.js
}


so long story short I'm developing RESTapi that takes a movie title on POST request to the /movies route, fetches info about that movie from external api and saves that object to the database. On POST /comments you add a comment to the different collection but every comment has a 'movie_id' property that has an associated movie.

That's my first bigger project so I'm trying to write integrational tests. Everything is great, at least in my opinion, except 3 weird test cases that are failing just out of nowhere. Tests could pass 10 times in a row and then suddenly that weird 'jest' timer shows up and 3 cases fail.

I'm using native mongodb driver, express and jest with supertest for testing, dropping test-database BeforeAll and AfterEach, I have no idea whats the reason of that.

Timer thingy:

And after timer this shows up, failed tests:

Full source code is here GITHUB

Other failed cases:

Any ideas, tips?

解决方案

I was in the same hell "jest parallel tests" problem and i find a solution, maybe not the best but now jest run tests in "queue mode" so when i delete datas in beforeAll my next group of tests is ready to go with "fresh" new inserted datas.

--runInBand Alias: -i. Run all tests serially in the current process, rather than creating a > worker pool of child processes that run tests. This can be useful for debugging.

jest source

So in my config.json i have :

    "scripts": {
        "test": "set NODE_ENV=test&& jest ./tests --runInBand --detectOpenHandles --forceExit",
        "server": "set NODE_ENV=development&& nodemon app.js"
    }

这篇关于随机失败的测试jest和supertest Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆