【问题标题】:Sometimes Jest integration tests fails and sometimes don't when I run all the suites当我运行所有套件时,有时 Jest 集成测试会失败,有时不会
【发布时间】:2022-09-24 22:32:50
【问题描述】:

我使用 Jest、Supertest、Moongose 编写了一些集成测试。我运行所有独立的测试(test.only),它们可以工作,但有时不能。当我运行整个测试套件时,通常会发生这种情况。我给你一个例子:这个测试在 MongoDB 集合中创建一个新的注册表,然后其他测试使用这个新的注册表来执行另一个操作:

            beforeAll(async () => {
              await mongoose.connect(config.mongoose.url, config.mongoose.options);
            });
            
            afterAll(async () => {
              await mongoose.disconnect();
              await new Promise(resolve => setTimeout(() => resolve(), 500));
            });
            
            let credentials = [];
            let fechaHora = [];

            // generate a new Id registry
            // generate credentials
            // generate date and hour
            beforeEach(async () => {
                rooms.insertRoomsToFile(rooms.getNewIdRoom() + \'|\');
                _idRoom = rooms.getIdRoom();
                credentials = await rooms.generateCredentialsBE(_idOrganization, basicToken);
                fechaHora = rooms.generateRoomDateAndHour();
            });

            test(`endpoint ${BASE_URL}${registerMeetingRoute} : 200 OK (Happy Path)`, async () => {
                generatedIdRoom = _idRoom;
                const data = {
                    idOrganization: 1,
                    idRoom: generatedIdRoom,
                    date: fechaHora[0],
                    hour: fechaHora[1],
                    attendes: [
                        {
                            email: \"john.doe@example.com.mx\",
                            id: 1,
                            firstName: \"John\",
                            lastName: \"Doe\",
                            userAttende: \"10000000-0000-0000-0000-000000000000\",
                            rol: 1,
                            telephone: \"5555555555\"
                        },
                        {
                            email: \"tom.taylor@example.com.mx\",
                            id: 2,
                            firstName: \"Tom\",
                            lastName: \"Taylor\",
                            userAttende: \"20000000-0000-0000-0000-000000000000\",
                            rol: 2,
                            telephone: \"5555555556\"
                        }
                    ]
                };
                const encryptedData = await rooms.encrypt(data);
                idAccess = encryptedData.idAccess;
                await request(app)
                    .post(`${BASE_URL}${registerMeetingRoute}`)
                    .set(\'content-type\', \'application/json\')
                    .set(\'authorization\', \'Bearer \' + credentials[2])
                    .set(\'x-accessId\', idAccess)
                    .send(encryptedData)
                    .expect(200);

                    rooms.saveLog(JSON.stringify(encryptedData), `endpoint      ${BASE_URL}${registerMeetingRoute} : 200 OK (Happy Path)`);
            });

这工作正常,问题有时不。我在这里尝试了很多答案并阅读了有关此主题的博客,但我无法解决它。我试过了:

  • 在 jest.config.js 中增加 testTimeout 属性
  • 每次测试打开和关闭 MongoDb 连接
  • 使用 mongodb-memory-server
  • --runInBand 选项

提前致谢 :)

    标签: node.js testing mongoose jestjs supertest


    【解决方案1】:

    Jest 将以不同的顺序并行执行不同的测试文件。

    您可以更改此行为并编写自己的自定义test sequencer

    【讨论】:

      【解决方案2】:

      Jest 将按顺序执行单个文件中的所有测试。 但是,我也遇到了完整文件运行与运行单个测试的问题,以防将带有 WebHashHistory 的路由器插件安装到包装器安装中。 MemoryHistory 不会出现此问题:

          const router = createRouter({
              history: createMemoryHistory(),
              routes: myRoutes,
          })

      Vue 3.2、Vue-router 4.0、vue-test-utils v2.0、选项 API、SFC、Jest v27.0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-29
        • 2022-10-18
        • 1970-01-01
        • 1970-01-01
        • 2015-02-19
        • 2010-10-04
        • 1970-01-01
        • 2020-10-17
        相关资源
        最近更新 更多