带有玩笑、超级测试、typeORM、typeDI 的节点 - 测试超时超过 30000 毫秒

问题描述 投票:0回答:0

我知道这是一个已知问题,其他一些人已经问过这个问题,但我无法解决它。

我有以下代码: 服务器.ts:

import http from "http";
import config from "../config";
import app from "./app";
import connectDB from "./dataBase/connectionDB";

const port = config.port;

function startServer() {
  const server = http.createServer(app);

  connectDB
    .initialize()
    .then(() => {
      console.log("Data base has been initialized");

      // Start app
      server.listen(port, () => {
        console.log(`Listen on server ${port}`);
      });
    })
    .catch((error) => {
      console.log(error);
    });

  return server;
}

export default startServer;

app.ts

import "reflect-metadata";
import express, { Application } from "express";
import cors from "cors";
import helmet from "helmet";
import bodyParser from "body-parser";
import swaggerUi from "swagger-ui-express";
import swaggerDocument from "./utils/swaggerDocument.json";
import Container from "typedi";

import registerRoute from "./routes/register";
import loginRoute from "./routes/login";
import usersRoute from "./routes/users";
import teamRoute from "./routes/teams";
import { eventRoute } from "./routes/event";
import AuthService from "./services/AuthService";
import TeamService from "./services/TeamService";
import UsersService from "./services/UsersService";
import PostgressTeamRepository from "./repositories/PostgressTeamRepository";
import PostgressUserRepository from "./repositories/PostgressUserRepository";

const app: Application = express();
Container.set("IAuthService", AuthService);
Container.set("IUsersService", UsersService);
Container.set("ITeamService", TeamService);
Container.set("ITeamRepositoryLayer", PostgressTeamRepository);
Container.set("IUserRepositoryLayer", PostgressUserRepository);

// Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Security
app.use(
  cors({
    origin: "http://localhost:3000",
  })
);
app.use(helmet());

// Routes
app.use(usersRoute);
app.use(registerRoute);
app.use(loginRoute);
app.use(teamRoute);
app.use(eventRoute);

// Swagger
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));

export default app;

寄存器.ts

import express from "express";
import { Request, Response } from "express";
import Container from "typedi";

import { IAuthService } from "src/interfaces/services/IAuthService";

const registerRoute = express.Router();

registerRoute.post("/api/register", async (req: Request, res: Response) => {
  try {
    const authService = Container.get<IAuthService>("IAuthService");

    const response = await authService.register(req.body);
    return res.status(200).json(response);
  } catch (error) {
    return error;
  }
});

export default registerRoute;

和测试文件:

import request from "supertest";
import app from "../../../app";
import connectDB from "../../../dataBase/connectionDB";

describe("registerRoute", () => {
  beforeAll(async () => {
    console.log("Connecting to database...");
    await connectDB.initialize();
  });

  afterAll(async () => {
    console.log("Disconnecting from database...");
    await connectDB.destroy();
  });

  it("should return 200 and the response from AuthService.register", async () => {
    const response = await request(app).post("/api/register").send({
      user_name: "test",
      email: "testemail",
      password: "testpassword",
    });

    expect(response.status).toBe(200);
    // expect(response.body).toBe("User created successfully");
  });
});

我试过了:

  • 文件顶部的setTimeout
  • 配置文件中的测试超时
  • timer 作为 it 函数的参数

这是回购和分支 - https://github.com/AleBradC/authentication-app/pull/17

node.js jestjs supertest
© www.soinside.com 2019 - 2024. All rights reserved.