【问题标题】:Using "expect" without a matching function (with Supertest and Jest)在没有匹配函数的情况下使用 \"expect\"(使用 Supertest 和 Jest)
【发布时间】:2022-11-25 15:17:29
【问题描述】:

我最近在使用 supertestjest 的快速应用程序中遇到了以下测试代码

 const supertest = require("supertest");
    const app = require("../app");
    const api = supertest(app);

    test("notes are returned as json", async () => {
      await api
        .get("/api/notes")
        .expect(200)
        .expect("Content-Type", /application\/json/);
    });

我对 .expect(200) 的来源感到困惑。这是supertest的一部分。因为我知道当我们调用 expect 时开玩笑,我们通常使用这样的匹配器:

expect(200).toBe(200)

但不知何故,这个测试不需要调用匹配器就可以工作。

【问题讨论】:

    标签: testing jestjs supertest


    【解决方案1】:

    .expect(200) API 来自 supertest 包的 Test 类。关键代码:

    // status
    if (typeof a === 'number') {
      this._asserts.push(wrapAssertFn(this._assertStatus.bind(this, a)));
      // body 
      // ...
    }
    

    source code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2012-11-18
      • 2019-10-04
      相关资源
      最近更新 更多