簡體   English   中英

如何與 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 是否提供任何方法來創建一些共享測試,以便我可以封裝此錯誤驗證並在其他套件案例中聲明它以避免如此多的重復?

您可以將這些測試封裝到 function 中。 文檔說:

為了讓 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');
    });
  });
}

然后,您可以使用此 function 來定義您的測試。 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM