使用TypeORM数据源搭配Jest时遭遇内存泄漏问题求助
解决Jest+TypeORM内存泄漏的优化措施
复用全局测试数据源,避免每个测试文件重复初始化
每个测试文件单独创建DataSource会产生大量重复的连接池、元数据占用内存,改用Jest全局初始化/销毁逻辑:- 创建
global-setup.js:import { DataSource } from 'typeorm'; global.testDataSource = new DataSource({ type: 'sqlite', database: ':memory:', dropSchema: true, entities: [], synchronize: true, }); module.exports = async () => { await global.testDataSource.initialize(); }; - 创建
global-teardown.js:module.exports = async () => { await global.testDataSource.destroy(); delete global.testDataSource; if (global.gc) global.gc(); }; - 在
jest.config.js中配置:module.exports = { globalSetup: './global-setup.js', globalTeardown: './global-teardown.js', // 其他原有配置 }; - 测试文件中直接使用
global.testDataSource,移除每个文件的beforeAll/afterAll逻辑。
- 创建
完善DataSource销毁后的清理逻辑
即使调用destroy(),仍可能存在残留引用或元数据未被回收,调整销毁步骤:afterAll(async () => { // 先关闭所有活跃连接(部分TypeORM版本需要) await testDataSource.close(); // 销毁数据源实例 await testDataSource.destroy(); // 清除实体元数据缓存(针对TypeORM 0.3+) testDataSource.clearMetadata(); // 解除变量引用,帮助GC回收 testDataSource = null; // 手动触发GC(已开启--expose-gc) if (global.gc) global.gc(); });注意:内存SQLite的
dropDatabase()是多余操作,内存数据库在连接关闭后会自动销毁,无需手动调用。禁用TypeORM不必要的缓存与自动操作
针对单文件独立初始化场景,关闭非必要的缓存和自动同步,减少内存占用:const testDataSource = new DataSource({ type: 'sqlite', database: ':memory:', dropSchema: true, entities: [], synchronize: true, // 添加以下配置 cache: false, metadataCache: false, migrationsRun: false, });优化Jest的模块隔离逻辑
Jest默认会为每个测试文件创建独立模块上下文,可能导致模块级变量无法被GC回收,可通过以下方式优化:- 将
DataSource变量放在测试块内部,避免模块级残留:describe('example', () => { let dataSource; beforeAll(async () => { dataSource = new DataSource({ /* 配置 */ }); await dataSource.initialize(); }); afterAll(async () => { await dataSource.destroy(); dataSource = null; if (global.gc) global.gc(); }); it('should add some things', async () => { expect(1 + 1).toEqual(2); }); }); - 在Jest配置中启用
resetModules: true,确保每个测试文件后重置模块缓存:module.exports = { resetModules: true, // 其他原有配置 };
- 将
升级依赖版本
TypeORM 0.3.x早期版本存在已知内存泄漏问题,尝试升级到最新稳定版,同时同步升级SQLite3、Jest到兼容版本,部分泄漏问题在后续版本中已修复。
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

