摩卡在解决承诺时运行测试



我想测试我的 Express 应用程序。在应用程序准备就绪并承诺解决之前,我会做一些异步设置工作。所以我把测试放在then函数中,但它们没有运行。

摩卡没有给出任何错误,而是报告"0 测试通过"。当我正常运行应用程序(节点服务器.js(时,一切正常。

如何在函数内运行then测试?

const app = new App();
app.ready.then(() => {
const express = app.express;
describe("GET api/v1/posts", test( () => {
beforeEach((done) => {
instance.testHelper();
done();
});
it("responds with array of 2 json objects", () => {
return chai.request(express).get("/api/v1/posts")
.then((res: ChaiHttp.Response) => {
expect(res.status).to.equal(200);
expect(res).to.be.json;
expect(res.body).to.be.an("array");
expect(res.body.length).to.equal(2);
});
});
it("json objects has correct shape", () => {
return chai.request(express)
.get("/api/v1/posts")
.then((res: ChaiHttp.Response) => {
const all: Post[] = res.body as Post[];
const post: Post = all[0];
expect( post ).to.have.all.keys( ["id", "author", "text"] );
});
});
}));
})
.catch( (err) => {
console.err(err);  // no errors!
});

你想使用before钩子,并稍微重组你的测试。 以下代码应该可以工作(但我不是在设置了摩卡的计算机上键入此代码,因此我无法对其进行测试(。

const app = new App();
describe('the test to run', () => {
let express = null;
before((done) => {
app.ready.then(() => {
express = app.express;
done();
});
});
it("test here", () => {
// some test
});
});

最新更新