gpt4 book ai didi

node.js - fastify,在fastify实例上开 Jest : running processes after calling . close()

转载 作者:行者123 更新时间:2023-12-03 12:14:11 26 4
gpt4 key购买 nike

我具有以下功能来打开数据库连接并运行我的Fastify服务器实例(server.js)

import fastify from "fastify";
import fastifyCors from "fastify-cors";
import { MongoClient } from "mongodb";

import characterRoute from "./routes/character";
import gearRoute from "./routes/gear";

import CharacterDAO from "./dao/character";
import GearDAO from "./dao/gear";

import { seedData } from "./dataSeed";

let connection;
let serverInstance;

// server startup command
export const startServer = async port => {
try {
serverInstance = fastify({ logger: false });
serverInstance.register(fastifyCors, {
origin: "*"
});
serverInstance.register(characterRoute);
serverInstance.register(gearRoute);

await serverInstance.listen(port, "0.0.0.0");
} catch (err) {
serverInstance.log.error(err);
process.exit(1);
}
};

// server shutdown command
export const killServer = async () => {
await serverInstance.close();
console.log("server instance closed");
await connection.close();
console.log("db connection closed");
};

// connect mongoDb, seed data if needed, run fastify server
export const runServer = async ({ dbUrl, dbName, environment, port }) => {
// test seed data when starting server if running a test suite
if (environment === "test") {
await seedData({
hostUrl: dbUrl,
databaseName: dbName
});
}

connection = await MongoClient.connect(dbUrl, {
poolSize: 50,
useNewUrlParser: true,
useUnifiedTopology: true,
wtimeout: 2500
});
const database = await connection.db(dbName);
// inject database connection into DAO objects
CharacterDAO.injectDB(database);
GearDAO.injectDB(database);
// start the fastify server
await startServer(port);
};

现在,我想针对正在运行的服务器实例运行一些测试,然后退出运行器。我一直在阅读 fastify docs并尝试使设置适应笑话。这是我的测试文件:
import { killServer, runServer } from "../server";

const serverOptions = {
dbUrl: process.env.dbUrl,
dbName: process.env.dbName,
environment: process.env.environment,
port: process.env.port
};

beforeAll(async () => {
// run the server instance we are testing against
await runServer({
...serverOptions
});
});

afterAll(async () => {
// figure out a way to kill the test server here
await killServer();
});

但是,在我的测试完成之后,我在控制台中收到以下警告:
Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

而且我必须手动结束测试套件。

这可能是什么原因?测试套件完成后,我似乎已经正确调用了 .close()函数(我在获取console.logs之后)。

供引用,jestconfig.json
{
"transform": {
"^.+\\.(t|j)sx?$": "babel-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}

npm测试脚本(省略参数)
dbUrl=mongodb+srv://saw-dsadw-dsawad@cluster0-dsa.dsa.dsa  dbName=dsa-fwa- environment=test port=4000 npx jest --config jestconfig.json

使用 --detectOpenHandles运行相同的脚本不会提供任何其他输出。

最佳答案

对于发现此问题的任何人,解决方案是重构server.js并进行更详细的测试设置:

server.js

export const buildFastify = () => {
const serverInstance = fastify({ logger: false });
serverInstance.register(fastifyCors, {
origin: "*"
});
serverInstance.register(characterRoute);
serverInstance.register(gearRoute);
return serverInstance;
};

export const buildDbConnection = async ({ dbUrl }) => {
const connection = await MongoClient.connect(dbUrl, {
poolSize: 50,
useNewUrlParser: true,
useUnifiedTopology: true,
wtimeout: 2500
});
return connection;
};

.test.js
const supertest = require("supertest");

import CharacterDAO from "../dao/character";
import GearDAO from "../dao/gear";

import { buildDbConnection, buildFastify } from "../server";

let connection;
let database;
let server;

const serverOptions = {
dbUrl: process.env.dbUrl,
dbName: process.env.dbName,
environment: process.env.environment,
port: process.env.port
};

// supertest instance to make server requests
const supertestInstance = supertest("http://localhost:4000");

beforeAll(async () => {
// database connection to validate that response data comes from the database
connection = await buildDbConnection({ dbUrl: serverOptions.dbUrl });
database = await connection.db(serverOptions.dbName);

// inject db into our DAO classes needed for the server to function properly
CharacterDAO.injectDB(database);
GearDAO.injectDB(database);

// run the server instance we are testing against
server = await buildFastify();
await server.listen(serverOptions.port, "0.0.0.0");
});

afterAll(async () => {
await server.close();
await connection.close();
});

因此问题出在复杂的runServer函数中-在完成所有测试后,调用使数据库句柄保持打开状态。

关于node.js - fastify,在fastify实例上开 Jest : running processes after calling . close(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60399676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com