UnitTest with Jest throw me Error: Encoding not recognized: 'cesu8' trying connect mysql database

Viewed 397

i have a problem with my unit tests. I'm using TypeORM with Mysql and Jest for unit tests.

Here is the log when execute 'npm run test'

ReferenceError: You are trying to import a file after the Jest environment has been torn down.

   at Object.getCodec (node_modules/mysql2/node_modules/iconv-lite/lib/index.js:63:27)
      at Object.getDecoder (node_modules/mysql2/node_modules/iconv-lite/lib/index.js:125:23)
      at Object.<anonymous>.exports.decode (node_modules/mysql2/lib/parsers/string.js:10:25)
      at Packet.readNullTerminatedString (node_modules/mysql2/lib/packets/packet.js:398:25)
      at Function.fromPacket (node_modules/mysql2/lib/packets/handshake.js:62:33)
/home/juan/projects/panel-uifra/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104
                throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
                ^

Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
    at Object.getCodec (/home/juan/projects/panel-uifra/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
    at Object.getDecoder (/home/juan/projects/panel-uifra/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:125:23)
    at Object.<anonymous>.exports.decode (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/parsers/string.js:10:25)
    at Packet.readNullTerminatedString (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/packets/packet.js:398:25)
    at Function.fromPacket (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/packets/handshake.js:62:33)
    at ClientHandshake.handshakeInit (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/commands/client_handshake.js:93:40)
    at ClientHandshake.execute (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/commands/command.js:39:22)
    at PoolConnection.handlePacket (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/connection.js:425:32)
    at PacketParser.onPacket (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/connection.js:75:12)
    at PacketParser.executeStart (/home/juan/projects/panel-uifra/node_modules/mysql2/lib/packet_parser.js:75:16)

The weird thing is than this error only happen with tests, when i'm running the server the connections made successfully.

A solution i found is explicitly set the encoders to iconv-lite like this:

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

But the problem here is typescript catch me because encodings props is not defined in the type declaration:

Property 'encodings' does not exist on type.

0 Answers
Related