运行npm run test时出现SequelizeConnectionRefusedError错误求助
问题排查:Sequelize测试环境连接被拒绝
我使用Sequelize作为ORM、PostgreSQL作为后端,开发环境下所有REST API端点均可正常工作,但运行npm run test命令时,出现SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306错误。以下是相关配置、代码及完整报错信息:
配置文件(config.json)
{ "development": { "username": "xyz", "password": "password123", "database": "db1", "host": "localhost", "dialect": "postgres" }, "test": { "username": "root", "password": null, "database": "database_test", "host": "127.0.0.1", "dialect": "mysql" }, "production": { "username": "root", "password": null, "database": "database_production", "host": "127.0.0.1", "dialect": "mysql" } }
app.js代码片段
const useit = async () => { await sequelize.authenticate(); } // 启动服务 app.listen({ port: 8000 }, async () => { console.log('Server up on http://localhost:8000') useit(); console.log('Database Connected!') await sequelize.sync({alter:true}); }) module.exports = app;
完整报错信息
console.warn failed to find WebSocket at $.send (eval at oo_cm (app.js:386:57), <anonymous>:1:5120) console.warn failed to find WebSocket at $.send (eval at oo_cm (app.js:386:57), <anonymous>:1:5120) FAIL test/app.test.js ✕ GET random not found endpoint (33 ms) ● GET random not found endpoint SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 at Client._connectionCallback (node_modules/sequelize/src/dialects/postgres/connection-manager.js:179:24) at Client._handleErrorWhileConnecting (node_modules/pg/lib/client.js:316:19) at Client._handleErrorEvent (node_modules/pg/lib/client.js:326:19) at Socket.reportStreamError (node_modules/pg/lib/connection.js:57:12) ● GET random not found endpoint SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306 at Client._connectionCallback (node_modules/sequelize/src/dialects/postgres/connection-manager.js:179:24) at Client._handleErrorWhileConnecting (node_modules/pg/lib/client.js:316:19) at Client._handleErrorEvent (node_modules/pg/lib/client.js:326:19) at Socket.reportStreamError (node_modules/pg/lib/connection.js:57:12)
问题根源及修复方案
问题核心
测试环境配置错误:test环境的dialect被设为mysql,但实际使用的是PostgreSQL,3306是MySQL的默认端口,导致Sequelize在测试时错误尝试连接MySQL服务,因此被拒绝。同时测试环境的用户名、密码、数据库名也和实际使用的PostgreSQL不匹配。
修复步骤
修正测试环境配置
修改config.json的test节点,对齐PostgreSQL配置(可单独设置测试用数据库名):{ "development": { "username": "xyz", "password": "password123", "database": "db1", "host": "localhost", "dialect": "postgres" }, "test": { "username": "xyz", "password": "password123", "database": "db1_test", // 自定义测试数据库名 "host": "localhost", "dialect": "postgres" }, "production": { "username": "xyz", "password": "password123", "database": "db1_prod", "host": "localhost", "dialect": "postgres" } }准备测试数据库
在PostgreSQL中创建db1_test数据库,并确保配置的用户名xyz拥有该数据库的访问权限。确保测试脚本加载正确环境
检查package.json中的测试脚本,确保设置了NODE_ENV=test,让Sequelize加载测试环境配置:"scripts": { "test": "NODE_ENV=test jest" // 根据你的测试框架调整,比如mocha等 }修复异步逻辑问题(非当前错误根源,但优化代码)
app.js中useit()是异步函数,但未加await,会导致连接成功的提示提前输出,建议修改:app.listen({ port: 8000 }, async () => { console.log('Server up on http://localhost:8000') await useit(); // 添加await等待连接完成 console.log('Database Connected!') await sequelize.sync({alter:true}); })
内容的提问来源于stack exchange,提问作者beginner
相关产品推荐
相关产品推荐

