Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding not recognized in jest.js

I have a problem testing a project using node-mysql2, react, sequelize and jest. This problem only occurs during testing.

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)
like image 614
Marc Avatar asked Sep 14 '17 20:09

Marc


1 Answers

This is problem caused by mysql2 doing dynamic lazy require of encodings and Jest not being able to handle this. Have a look at few workarounds users suggested here:

add this snippet to setupTestFrameworkScriptFile

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

or this somewhere early to your code:

import iconv from 'iconv-lite';
import encodings from 'iconv-lite/encodings';
iconv.encodings = encodings;
like image 170
Andrey Sidorov Avatar answered Sep 18 '22 19:09

Andrey Sidorov