LoopBack4:使用@loopback/testlab为多组/全部测试用例设置超时时间
Mocha测试超时配置:为一组/全部测试用例设置超时时间
你遇到的测试超时错误如下:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (xxxxx/dist/__tests__/acceptance/employee.controller.acceptance.js) at listOnTimeout (node:internal/timers:559:17) at processTimers (node:internal/timers:502:7)
你已实现为单个测试用例设置超时(通过it().timeout(5000)),以下是为一组或全部测试用例配置超时的方法:
一、为一组测试用例(describe块)设置超时
直接在describe后链式调用.timeout()方法,该describe下的所有测试用例(it)、钩子函数(beforeEach/afterEach等)都会继承这个超时时间:
describe('EmployeeController', () => { let app: ExpressServer; let client: Client; afterEach(async() => { clearDatabase(); await app.stop(); }); beforeEach(async() => { ({app, client} = await setupApplication()); }); it('Create an employee - Fresh User', async() => { const token = await generateUserToken(client, 'user.1@5by.one'); const resL = await client.get('/api/auth/users/my-account') .set({Authorization: `Bearer ${token}`}).send() .expect(200); const permissions = { ...accouontingPermissions, ...systemPermissions }; await client.post('/api/auth/employees') .set({Authorization: `Bearer ${token}`}).send({ name: 'Emp One', email: 'employee.1@5by.one', permissions, role: 'user', }) .expect(200); }); }).timeout(5000); // 为整个describe组设置5秒超时
二、为全部测试用例设置全局超时
有两种常用方式:
1. 命令行参数启动
执行Mocha时添加--timeout参数指定全局超时时间:
mocha --timeout 5000
2. 配置文件设置
创建Mocha配置文件(如.mocharc.json或mocha.config.js),在其中配置全局超时:
JSON格式配置文件(.mocharc.json)
{ "timeout": 5000 }
JS格式配置文件(mocha.config.js)
module.exports = { timeout: 5000 };
内容的提问来源于stack exchange,提问作者Prince Francis
相关产品推荐
相关产品推荐

