使用 Mocha Chai 异步函数进行测试
Testing with Mocha Chai async function
我有一个带路由的 Hapi JS 服务器,这是我的 Hapi js 服务器
const Hapi = require('hapi');
import { Users } from './users';
const port 3000;
const server = new Hapi.Server();
server.connection({ port });
export async function HapiServer() {
server.route([
{
method: 'GET',
path: '/getUsers',
config: {
handler: async (req: any, reply: any) => {
try {
await Users.getUsers(req.params.id)
.then((result) => {
reply(result);
});
} catch (error) {
reply(error);
}
},
},
},
]);
await server.start();
console.log('server running at', server.info.port);
}
HapiServer();
我使用 Mocha 和 Chai 进行如下单元测试:
'use strict';
import { should } from 'chai';
import { HapiServer } from '../../server';
const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
describe('testing users ', () => {
it('can get all users', async (done) => {
const result = await chai
.request(HapiServer)
.get(/getUsers)
.then((error, response) => {
if (error) { done(error); }
response.should.have.status(200);
done();
}).catch(done);
});
});
当 运行 测试总是这样:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (./server.test.ts)
我已经尝试在 package.json
中更改我的 mocha 配置
mocha --timeout 10000
我做错了什么?我了解 Mocha 支持 promise。
我们实际上结合了所有解决方案(async/await
、promise based
和 callback done
)以在测试文件中进行异步测试。
让我们使用最近使用 async/await
的解决方案,所以它将是:
describe('testing users ', () => {
it('can get all users', async () => {
const response = await chai.request(HapiServer).get('/getUsers');
response.should.have.status(200);
});
});
看到这段代码中没有 done
也没有使用 then.
。
希望对您有所帮助。
我有一个带路由的 Hapi JS 服务器,这是我的 Hapi js 服务器
const Hapi = require('hapi');
import { Users } from './users';
const port 3000;
const server = new Hapi.Server();
server.connection({ port });
export async function HapiServer() {
server.route([
{
method: 'GET',
path: '/getUsers',
config: {
handler: async (req: any, reply: any) => {
try {
await Users.getUsers(req.params.id)
.then((result) => {
reply(result);
});
} catch (error) {
reply(error);
}
},
},
},
]);
await server.start();
console.log('server running at', server.info.port);
}
HapiServer();
我使用 Mocha 和 Chai 进行如下单元测试:
'use strict';
import { should } from 'chai';
import { HapiServer } from '../../server';
const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
describe('testing users ', () => {
it('can get all users', async (done) => {
const result = await chai
.request(HapiServer)
.get(/getUsers)
.then((error, response) => {
if (error) { done(error); }
response.should.have.status(200);
done();
}).catch(done);
});
});
当 运行 测试总是这样:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (./server.test.ts)
我已经尝试在 package.json
中更改我的 mocha 配置mocha --timeout 10000
我做错了什么?我了解 Mocha 支持 promise。
我们实际上结合了所有解决方案(async/await
、promise based
和 callback done
)以在测试文件中进行异步测试。
让我们使用最近使用 async/await
的解决方案,所以它将是:
describe('testing users ', () => {
it('can get all users', async () => {
const response = await chai.request(HapiServer).get('/getUsers');
response.should.have.status(200);
});
});
看到这段代码中没有 done
也没有使用 then.
。
希望对您有所帮助。