【问题标题】:Why is jest running my Typescript test twice and always failing once?为什么 jest 运行我的 Typescript 测试两次并且总是失败一次?
【发布时间】:2021-09-10 08:23:26
【问题描述】:

我正在后端编写一个测试来测试我的 API 的简单路由。我正在使用 Typescript 和 Jest 编写测试,这一切都发生在一个 docker 容器内,我首先使用 docker-compose

我有一个用于我的测试的辅助类,它通过 init 函数创建一个快速网络服务器的实例和一个数据库连接。在关闭功能中,它关闭连接等。 所以我的测试看起来像这样:

import { Helper } from "./helper";
import request from 'supertest'

describe('article', () => {
    const helper = new Helper();
    beforeAll(async () => {
        await helper.init();
    });

    afterAll(async () => {
        await helper.shutdown();
    });

    it('should test if reaching the api is possible', async (done) => {
        request(helper.app)
        .get('/test')
        .send()
        .set('Accept', 'application/json')
        .expect(200)
        .end( (error, response) => {
            if(error) throw error;
            expect(response.body.message).toBe("Hello");
        });
    });

为了执行测试,我使用这个脚本"test": "jest --verbose --forceExit --runInBand --detectOpenHandles"

如果我这样执行测试,我会得到以下输出:

我按照建议更改了超时,但没有任何区别。 现在出于测试目的,我让我的路线返回“Helloo”而不是预期的“Hello”。 这是我收到的输出:

所以它显然得到了回应。我的问题是,为什么它要运行两次测试,为什么在第二次测试时总是超时?

这是我的jest.config.js

module.exports = {
    "roots": [
      "<rootDir>/src"
    ],
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
    "moduleFileExtensions": [
      "ts",
      "js"
    ],
  }

【问题讨论】:

    标签: typescript testing jestjs ts-jest


    【解决方案1】:

    使用异步测试时,您可以从异步处理程序的主体中调用 done callback

    应该这样做:

    it('should test if reaching the api is possible', (done) => { // << async tests have the done callback declared here
            request(helper.app)
            .get('/test')
            .send()
            .set('Accept', 'application/json')
            .expect(200)
            .end( (error, response) => {
                if(error) {
                  done(error);
                } else {
                  expect(response.body.message).toBe("Hello");
                  done();
                }
            });
        });
    

    【讨论】:

    • 我试过了,还是没有通过测试。但是有一个不同的日志:它不再建议超时,现在它说:`测试函数不能同时接受“完成”回调并返回一些东西。要么使用“完成”回调,要么返回一个承诺。返回值:承诺{}`
    • 也许在 done(error) 行之后的return 不正确......我会更新。你能再试一次吗?我需要更多的来源来说明为什么测试运行了两次,但我的直觉是测试文件被加载了两次(使用这个文件检查包含)
    • 自上次发表评论以来,我在完成(错误)后删除了退货。我还搜索了文件引用,但没有找到。我还能向您提供我的资源吗?我会将其添加到问题中。提前致谢。
    • 我刚刚发现我测试什么并不重要,它总是会发生。例如,我刚刚尝试了测试test('xxx', () =&gt; { expect(1).toBe(2)}),它具有与上述相同的行为(执行 2 次,首先表示预期 2 收到 1,然后表示超过 5000 毫秒的测试超时)。
    • 这不是答案。 async/await 测试不需要done() 回调。 jestjs.io/docs/asynchronous#asyncawait
    【解决方案2】:

    解决方案

    由于这个问题最近引起了一些关注,我从问题文本中删除了解决方案并将其添加到此答案中。 我不确定这是否是正确的方法,以及我是否做了一些多余的事情,因为我第二次使用 jest 并且从那时起就没有碰过它。但是在我发布问题时,这解决了我的问题:

    我必须用 try-catch 块包围期望,不要将测试标记为异步,而是将 done 函数传入并在期望之后和 catch 块中调用 done 函数。

    【讨论】:

      猜你喜欢
      • 2011-05-02
      • 1970-01-01
      • 2018-10-13
      • 2022-01-12
      • 1970-01-01
      • 2017-04-20
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多