如何使用 Jest 在多个套件中共享测试用例?

How to share test cases in multiple suites with Jest?

我发现我在Node.js REST API中的多个集成测试中有很多重复的测试用例。因此,例如,我测试了每个端点的无效请求,我希望错误始终具有相同的属性。

import { app } from 'server';
import * as request from 'supertest';

describe('Authentication tests', () => {
    describe('POST /login', () => {
        // other test cases
        // describe('valid request should ...', () => {...})

        describe('invalid requests with missing fields', () => {
            let response = null;

            beforeAll(async () => {
                await request(app)
                    .post('/login')
                    .expect('Content-Type', 'application/json; charset=utf-8')
                    .field('email', 'invalid@test.com')
                    .then(res => {
                        response = res;
                    });
            });

            it('should return an invalid status code', () => {
                expect(response.status).toBe(400);
            });

            it('should return a valid error schema', () => {
                expect(typeof response.body).toBe('object');
                expect(response.body).toHaveProperty('error');
                expect(response.body.error).toHaveProperty('code');
                expect(response.body.error).toHaveProperty('message');
            });

            it('should return an error with explicit message', () => {
                expect(response.body.error).toHaveProperty('message');
            });
        });
    });
});

Jest 是否提供任何方法来创建一些共享测试,以便我可以封装此错误验证并在其他套件案例中声明它以避免太多重复?

您可以将这些测试封装到一个函数中。 docs 表示:

Tests must be defined synchronously for Jest to be able to collect your tests.

例如:

function createInvalidRequestTests() {
  describe('invalid request', () => {
    let response;
    beforeAll(async () => {
      // simulate request of supertest
      response = await Promise.resolve({ status: 400, body: { error: { code: 1, message: 'network error' } } });
    });

    it('should return an invalid status code', () => {
      expect(response.status).toBe(400);
    });

    it('should return a valid error schema', () => {
      expect(typeof response.body).toBe('object');
      expect(response.body).toHaveProperty('error');
      expect(response.body.error).toHaveProperty('code');
      expect(response.body.error).toHaveProperty('message');
    });

    it('should return an error with explicit message', () => {
      expect(response.body.error).toHaveProperty('message');
    });
  });
}

然后,您可以使用此功能来定义您的测试。 Jest test 运行ner 将像往常一样收集并运行这些测试


describe('Authentication tests', () => {
  describe('POST /login', () => {
    describe('valid request', () => {
      it('should login correctly', () => {
        expect(1).toBe(1);
      });
    });

    createInvalidRequestTests();
  });

  describe('POST /register', () => {
    describe('valid request', () => {
      it('should register correctly', () => {
        expect(2).toBe(2);
      });
    });

    createInvalidRequestTests();
  });
});

单元测试结果:

 PASS  src/Whosebug/58081822/index.spec.ts (9.622s)
  Authentication tests
    POST /login
      valid request
        ✓ should login correctly (5ms)
      invalid request
        ✓ should return an invalid status code
        ✓ should return a valid error schema (2ms)
        ✓ should return an error with explicit message
    POST /register
      valid request
        ✓ should register correctly (1ms)
      invalid request
        ✓ should return an invalid status code (1ms)
        ✓ should return a valid error schema (2ms)
        ✓ should return an error with explicit message (1ms)

Test Suites: 1 passed, 1 total
Tests:       8 passed, 8 total
Snapshots:   0 total
Time:        12.053s, estimated 14s