在 jest.js 中无法识别编码
Encoding not recognized in jest.js
我在使用 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;
真的只需要加上:
require('iconv-lite').encodingExists('foo')
到您正在测试的任何文件的顶部,例如 factory.test.js
不确定为什么会这样,但上面的内容比所选答案更适合复制/粘贴。
我遇到了与 network
节点模块类似的问题。我通过模拟 network
模块而不使用 iconv-lite
来解决问题。以下是我的代码。
jest.mock('network', () => ({
get_active_interface: cb => {
cb?.(undefined, { type: 'Wired' });
},
}));
至于这种情况,也许您可以尝试 mock
进行调用的模块,像这样的代码。
jest.mock('mysql2',()=>({
someFuncThatCallsIcoveLite:(..._)=>{}
}))
我在使用 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;
真的只需要加上:
require('iconv-lite').encodingExists('foo')
到您正在测试的任何文件的顶部,例如 factory.test.js
不确定为什么会这样,但上面的内容比所选答案更适合复制/粘贴。
我遇到了与 network
节点模块类似的问题。我通过模拟 network
模块而不使用 iconv-lite
来解决问题。以下是我的代码。
jest.mock('network', () => ({
get_active_interface: cb => {
cb?.(undefined, { type: 'Wired' });
},
}));
至于这种情况,也许您可以尝试 mock
进行调用的模块,像这样的代码。
jest.mock('mysql2',()=>({
someFuncThatCallsIcoveLite:(..._)=>{}
}))