I want to test my Express app. I do some async setup stuff before app is ready and promise resolves. So I put the tests inside the then
function but they are not run.
Mocha gives no errors, instead it reports "0 tests passing". When I run the app normally (node server.js) everything works fine.
How can I run tests inside then
function?
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!
});