如何与Jest共享多个套件中的测试用例?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何与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说:
测试必须同步定义,Jest才能收集您的测试。
例如:
function createInvalidRequestTests() {
describe('invalid request', () => {
let response;
beforeAll(() => {
response = { 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');
});
});
}
然后,您可以使用此功能定义测试。开玩笑的测试跑步者将照常收集和运行这些测试
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
以上是关于如何与Jest共享多个套件中的测试用例?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用不同的jest.config.js进行单元和组件测试?
如何在机器人框架中并行运行多个测试套件上的多个测试用例 | Python