【问题标题】:How to share test cases in multiple suites with Jest?如何使用 Jest 共享多个套件中的测试用例?
【发布时间】:2020-01-24 16:01:40
【问题描述】:

我发现我在一个 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 是否提供任何方法来创建一些共享测试,以便我可以封装此错误验证并在其他套件案例中声明它以避免如此多的重复?

【问题讨论】:

    标签: javascript node.js express jestjs supertest


    【解决方案1】:

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

    测试必须同步定义,Jest 才能收集您的测试。

    例如:

    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 测试运行器会像往常一样收集和运行这些测试

    
    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/stackoverflow/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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-06
      • 2020-10-17
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 2018-10-28
      相关资源
      最近更新 更多