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

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

问题描述

长话短说,我正在开发 RESTapi,它在向/movies 路由发送 POST 请求时获取电影标题,从外部 api 获取有关该电影的信息并将该对象保存到数据库中.在 POST/comments 上,您向不同的集合添加评论,但每个评论都有一个movie_id"属性,该属性具有关联的电影.

这是我的第一个更大的项目,所以我正在尝试编写集成测试.一切都很好,至少在我看来,除了 3 个突然失败的奇怪测试用例.测试可以连续通过 10 次,然后突然出现那个奇怪的开玩笑"计时器,3 个案例失败.

我正在使用原生 mongodb 驱动程序,express 和 jest 与 supertest 一起进行测试,删除了测试数据库 BeforeAll 和 AfterEach,我不知道这是什么原因.

定时器的东西:

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

完整源代码在这里

有什么想法、技巧吗?

解决方案

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

<块引用>

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

笑话来源

所以在我的 config.json 中有:

 "脚本": {"test": "set NODE_ENV=test&& jest ./tests --runInBand --detectOpenHandles --forceExit","server": "设置 NODE_ENV=development&& 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天全站免登陆