我正在尝试学习与chai-http和摩卡api测试。我已经尝试了done()
和async/await
,但我不明白为什么它没有解决以下问题-
错误-
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Spec文件-
import * as chai from 'chai';
import { assert } from 'chai';
import chaiHttp = require('chai-http');
import 'mocha';
chai.use(chaiHttp);
const expect = chai.expect;
const url = 'https://api.weather.gov';
describe('Weather API', () => {
it('Should be up and running', () => {
return chai.request(url).get('/').then(res => {
expect(res).to.be.status(200);
});
});
it('Should return weather of washington monument', () => {
return chai.request(url).get('/gridpoints/LWX/96,70/forecast')
.set('User-Agent', 'test@email.com')
.set('Accept', 'application/vnd.noaa.dwml+xml')
.then(res => {
expect(res).to.be.status(200);
console.log(res.body.properties.periods);
});
});
});
否则,我没有正确地执行我尝试过的解决方案。如有任何帮助,不胜感激。
当你使用承诺时,你必须使用chai-as-promised
,有了这个,你可以用这种方式使用async/await:
import chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
//...
it('Should be up and running', async () => {
var response = await chai.request(url).get('/')
return response.should.have.status(200)
// you can use "return expect(response).to.have.status(200);" too
})
注意现在没有使用done
,而是添加了return
。