node.js - jest.js 无法识别编码

标签 node.js jestjs node-mysql2

我在使用 node-mysql2、react、sequelize 和 jest 测试项目时遇到问题。此问题仅在测试期间出现。

Encoding not recognized: 'cesu8' (searched as: 'cesu8')
    at Object.getCodec (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:106:23)
    at Object.getDecoder (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:122:23)
    at Object.<anonymous>.exports.decode (project/node_modules/mysql2/lib/parsers/string.js:9:23)
    at Packet.Object.<anonymous>.Packet.readNullTerminatedString (project/node_modules/mysql2/lib/packets/packet.js:373:23)
    at Function.Object.<anonymous>.Handshake.fromPacket (project/node_modules/mysql2/lib/packets/handshake.js:18:31)
    at ClientHandshake.Object.<anonymous>.ClientHandshake.handshakeInit (project/node_modules/mysql2/lib/commands/client_handshake.js:98:38)
    at ClientHandshake.Object.<anonymous>.Command.execute (project/node_modules/mysql2/lib/commands/command.js:40:20)
    at Connection.Object.<anonymous>.Connection.handlePacket (project/node_modules/mysql2/lib/connection.js:515:28)
    at PacketParser.onPacket (project/node_modules/mysql2/lib/connection.js:94:16)
    at PacketParser.executeStart (project/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (project/node_modules/mysql2/lib/connection.js:102:29)

最佳答案

这是由 mysql2 执行编码的动态惰性要求而 Jest 无法处理的问题。查看用户建议的一些解决方法 here :

将此代码段添加到 setupTestFrameworkScriptFile

require('mysql2/node_modules/iconv-lite').encodingExists('foo');

或者在您的代码的早期位置:

import iconv from 'iconv-lite';
import encodings from 'iconv-lite/encodings';
iconv.encodings = encodings;

关于node.js - jest.js 无法识别编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46227783/

相关文章:

node.js - 如何使用 Jest 模拟测试 Node.js CLI?

node.js - Nodejs - mysql2流连接和远程主机

node.js - Node -exceljs : writing to file breaks fomulas in the file

javascript - Jest : how to test console. 日志 haveBeenCalled 具有预期值的子集?

javascript - 错误 : spawn ENOENT for png files when using grunt-contrib-imagemin

typescript - NestJs + TypeScript + Jest - TypeError : Class extends value undefined is not a constructor or null

mysql - max_prepared_stmt_count错误,如何回收prepared statements?

javascript - 如何设置变量=异步函数中函数结果的值

javascript - Array 中时间序列对象的移动平均值