Node-mysql2: Error running tests with Jest using mysql2 1.7.0

Created on 20 Oct 2019  路  6Comments  路  Source: sidorares/node-mysql2

I get the following error after updating from mysql2@^1.6.5 to mysql2@^1.7.0.

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:65:27)
      at Object.getDecoder (node_modules/mysql2/node_modules/iconv-lite/lib/index.js:127: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:384:25)
      at Function.fromPacket (node_modules/mysql2/lib/packets/handshake.js:58:33)
C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:106
                throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
                ^

Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
    at Object.getCodec (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:106:23)
    at Object.getDecoder (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:127:23)
    at Object.<anonymous>.exports.decode (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\parsers\string.js:10:25)
    at Packet.readNullTerminatedString (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\packets\packet.js:384:25)
siroc\repositories\sphinxql\node_modules\mysql2\lib\packets\handshake.js:58:33)
    at ClientHandshake.handshakeInit (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\commands\client_handshake.js:92:40)
    at ClientHandshake.execute (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\commands\command.js:39:22)
    at Connection.handlePacket (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\connection.js:449:32)
    at PacketParser.Connection.packetParser.p [as onPacket] (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\connection.js:72:12)
    at PacketParser.executeStart (C:\Users\siroc\repositories\sphinxql\node_modules\mysql2\lib\packet_parser.js:75:16)

If you want to see the project with all updated (except the mysql2 package) to replicate the problem use https://github.com/SiroDiaz/sphinxql. Note that it is under develop branch. Just try to update mysql2 to latest version(1.7.0), start Manticore search 3.1.0 and run jest tests.

Start Manticore search daemon:

cd __tests__
searchd --config sphinx.conf

Most helpful comment

There's two errors here. The second one:

Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')

This can be resolved using this code (tests/setup.js):

// Little fix for Jest, see https://stackoverflow.com/a/54175600
require('mysql2/node_modules/iconv-lite').encodingExists('foo');

Then wire it in using jest.config.json:

{
  "setupFilesAfterEnv": ["<rootDir>/tests/setup.js"]
}

All 6 comments

How do I run tests? I guess I need some preparation step, getting Failed: "no such local index 'rt' error.

My steps:

  • git clone https://github.com/SiroDiaz/sphinxql && cd sphinxql && git co develop & npm install
  • start Manticore with docker run -it -p 9307:9306 manticoresearch/manticore
  • npm test

I'm trying to run docker run to follow your steps and Manticore container doesn't find the local sphinx.conf file for testing. So i recommend you to install Manticore without Docker this time and run searchd. I have reported the issue with the Manticore Docker image (for Windows).

If you are using other OS different to Windows 10 Home, you can try to run the following command changing paths
docker run --name manticore -v ~/sphinxql/__tests__/:/etc/sphinxsearch/ -v ~/sphinxql/__tests__/data/:/var/lib/manticore/data -v ~/sphinxql/__tests__/:/var/log/manticore -p 9307:9306 -d manticoresearch/manticore

The sphinx.conf is the configuration file and that file for tests lives in /sphinxql/__tests__ directory and logs and data folders are relative to the project location. So, don't forget to use searchd being inside __tests__ directory.

I started it with docker run -it -v $PWD/__tests__:/etc/sphinxsearch -p 9307:9306 manticoresearch/manticore and now some other tests fail

There's two errors here. The second one:

Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')

This can be resolved using this code (tests/setup.js):

// Little fix for Jest, see https://stackoverflow.com/a/54175600
require('mysql2/node_modules/iconv-lite').encodingExists('foo');

Then wire it in using jest.config.json:

{
  "setupFilesAfterEnv": ["<rootDir>/tests/setup.js"]
}

/

Thanks for this hack.

Thank you!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abumusamq picture abumusamq  路  5Comments

hezjing picture hezjing  路  3Comments

sidorares picture sidorares  路  7Comments

sidorares picture sidorares  路  6Comments

HulioEglesias picture HulioEglesias  路  4Comments