我有一个使用Redis适配器使用Socket.IO的Node.JS服务器。运行Jest测试时,所有测试都已运行后,Jest进程将挂起。我已将代码最小化为以下代码,以便能够重现。导致此问题的是Redis适配器。我还需要做什么?
app-test.ts
import express from 'express';
import fse from 'fs-extra';
import * as http from 'http';
import socketIo from 'socket.io';
import SocketIORedis from 'socket.io-redis';
import { CONFIG_TYPE, CONFIGS_PATHS } from '../../constants';
class Server {
// Bootstrap the application.
public static bootstrap(): Server {
return new Server();
}
public app: express.Application;
public server: http.Server;
private io: SocketIO.Server;
constructor() {
// Create expressjs application
this.app = express();
// Create server
this.createServer();
}
public shutdown(): void {
this.io.close();
this.server.close();
}
private async createServer() {
this.server = http.createServer(this.app);
this.io = socketIo(this.server, { transports: ['polling', 'websocket'] });
const redisConfig = await fse.readJson(CONFIGS_PATHS[CONFIG_TYPE].REDIS);
const redisAdapter = SocketIORedis(redisConfig);
this.io.adapter(redisAdapter);
}
}
// Bootstrap the server
const server = Server.bootstrap();
export default server;
app-test.spec.ts
import app from './app-test';
describe('test', () => {
afterAll((done) => {
app.shutdown();
done();
});
it('fake test', () => {
expect(true).toBeTruthy();
});
});