Jest.js 使用expect.any()和supertest检查响应体

o2gm4chl  于 2023-11-15  发布在  Jest
关注(0)|答案(2)|浏览(159)

我尝试使用supertest来检查Jest的res.body,但是下面的代码片段总是失败

request(app)
  .post('/auth/signup')
  .send(validEmailSample)
  .expect(200, {
    success: true,
    message: 'registration success',
    token: expect.any(String),
    user: expect.any(Object),
  });

字符串
但是当我重写测试以检查回调中的主体时,如下所示:

test('valid userData + valid email will result in registration sucess(200) with message object.', (done) => {
  request(app)
    .post('/auth/signup')
    .send(validEmailSample)
    .expect(200)
    .end((err, res) => {
      if (err) done(err);
      expect(res.body.success).toEqual(true);
      expect(res.body.message).toEqual('registration successful');
      expect(res.body.token).toEqual(expect.any(String));
      expect(res.body.user).toEqual(expect.any(Object));
      expect.assertions(4);
      done();
    });
});


测试会通过的。
我确信这与expect.any()有关。正如Jest的文档所说,expect.any和expect.anything只能与expect().toEqualexpect().toHaveBeenCalledWith()一起使用,我想知道是否有更好的方法来做到这一点,在supertest的expect API中使用expect.any。

fivyi3re

fivyi3re1#

可以使用expect.objectContaining(object)。
匹配任何递归匹配预期属性的接收对象。即,预期对象是接收对象的子集。因此,它匹配包含预期对象中存在的属性的接收对象。
app.js

const express = require("express");
const app = express();

app.post("/auth/signup", (req, res) => {
  const data = {
    success: true,
    message: "registration success",
    token: "123",
    user: {},
  };
  res.json(data);
});

module.exports = app;

字符串
app.test.js

const app = require('./app');
const request = require('supertest');

describe('47865190', () => {
  it('should pass', (done) => {
    expect.assertions(1);
    request(app)
      .post('/auth/signup')
      .expect(200)
      .end((err, res) => {
        if (err) return done(err);
        expect(res.body).toEqual(
          expect.objectContaining({
            success: true,
            message: 'registration success',
            token: expect.any(String),
            user: expect.any(Object),
          }),
        );
        done();
      });
  });
});


集成测试结果与覆盖率报告:

PASS  src/stackoverflow/47865190/app.test.js (12.857s)
  47865190
    ✓ should pass (48ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 app.js   |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        14.319s


源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/47865190

i34xakig

i34xakig2#

把它赋给一个变量然后做任何你想做的事。

const app = require('./app');
const request = require('supertest');

describe('valid userData + valid email will result in registration success(200) with message object.', () => {
  it('should pass', (done) => {
    const res = await request(app).post('/auth/signup');
    expect(res.body).toEqual(
      expect.objectContaining({
        success: true,
        message: 'registration success',
        token: expect.any(String),
        user: expect.any(Object),
      }),
    );
  });
});

字符串

相关问题