简体   繁体   English

忽略expect()函数的玩笑异步测试

[英]jest asynchronous test ignoring expect() function

server.js is a simple express.js file that uses jwt tokens. server.js 是一个使用 jwt 令牌的简单 express.js 文件。 I currently want to test a simple route that will only return the string "Hello World" as shown below我目前想测试一个简单的路由,它只会返回字符串“Hello World”,如下所示

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下面的代码是我的玩笑文件,它使用 supertest 将请求与有效的 jwt 令牌一起发送

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当运行这个玩笑时(在它超时 5 秒之后)说Timeout - Async callback was not invoked within the 5000ms timeout specified但是,我在 expect 函数输出“Hello World”到控制台后添加的 console.log 意味着请求被制作并返回一个值,但它与代码一起运行,但只是跳过了expect函数

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?我也用done()then()尝试过这个,但两次都遇到了同样的错误,我在控制台记录了调用前后的时间,发现它只需要几毫秒就可以返回一个值,那么为什么期望似乎没有完成测试?

Pretty sure your problem is the app_started event that you are listening to.很确定您的问题是您正在收听的app_started事件。 I don't know where that event is documented.我不知道该事件记录在哪里。 I think you should use listening instead.我认为你应该使用listening来代替。 I'm going to make some assumptions about your server.js file.我将对你的server.js文件做一些假设。

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:这是我正在测试的server.js文件:

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 :这是我的测试文件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.用户 zero298 指出测试在beforeAll()函数期间失败,因为server.on()没有返回任何内容。 In the end I wrote the promise inside the server.js which resolves after it has started and then exported this promise.最后我在server.js 中写了 promise,它在它启动后解析,然后导出这个 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 server.test.js 中beforeAll()现在看起来像

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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