简体   繁体   中英

jest asynchronous test ignoring expect() function

server.js is a simple express.js file that uses jwt tokens. I currently want to test a simple route that will only return the string "Hello World" as shown below

app.get("/", (req, res) => {
  res.send("Hello World");
});

The code below is my jest file that is using supertest to send the request along with the valid jwt token

const supertest = require("supertest");

let server, request;
const serverStart = new Promise(resolve => {
  server = require("../server.js");
  request = supertest(server);
  server.on("app_started", () => {
    resolve();
  });
});

beforeAll(async () => {
  await serverStart;
});

afterAll(() => {
  server.close();
});

describe("When testing the server.js", () => {
  it("Should connect successfully and be able to return a response", async () => {
    const response = await request
      .get("/")
      .set("Authorization", `bearer ${process.env.AUTHTOKEN}`);

    expect(response.text).toBe("Hello World");
    console.log(response.text);
  });
});

When running this jest (after it's timeout of 5 seconds) says Timeout - Async callback was not invoked within the 5000ms timeout specified however, the console.log that I have added after the expect function outputs "Hello World" to the console meaning the request is made and returns a value but it carriers on with the code but is just skipping the expect function

I've also tried this with done() and also using a then() but got the same error both times and I've console logged the time before and after the call and found it only takes a few milliseconds to return a value, so why does the expect not seem to complete the test?

Pretty sure your problem is the app_started event that you are listening to. I don't know where that event is documented. I think you should use listening instead. I'm going to make some assumptions about your server.js file.

The following test passes. I think your tests never actually start because you are listening for an event that will never be fired.

This is the server.js file that I am testing with:

const http = require("http");
const express = require("express");

const app = express();

app.get("/", (req, res) => {
    res.send("hello");
});

const server = http.createServer(app);

server.listen(8081);

module.exports = server;

This is my test file server.test.js :

const supertest = require("supertest");

let server, request;

const serverStart = new Promise(resolve => {
    server = require("./server.js");
    request = supertest(server);
    server.on("listening", () => resolve());
});

beforeAll(async () => {
    await serverStart;
});

afterAll(() => {
    server.close();
});

describe("server", () => {
    it("should get hello", async () => {
        const response = await request.get("/");
        expect(response.text).toBe("hello");
    });
});

User zero298 pointed out that the test was failing during the beforeAll() function because server.on() wasn't returning anything. In the end I wrote the promise inside the server.js which resolves after it has started and then exported this promise.

let server;
const serverStart = new Promise(resolve => {
  server = app.listen(port, err => {
    if (err) {
      console.log(err);
    }
    console.log("Listening on port " + port);
    resolve();
  });
});

module.exports = app;
module.exports.close = () => server.close();
module.exports.serverStart = serverStart;

and the beforeAll() in server.test.js now looks like

const server = require("../server.js");
let request;

beforeAll(async () => {
  await server.serverStart;
  request = supertest(server);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM