Node-mysql2: Wierd issue when running with jest

Created on 14 Jan 2017  路  11Comments  路  Source: sidorares/node-mysql2

I know this is very little information, but I've encountered the following when executing some tests with Jest, which touches code that connects to MySQL using node-mysql2, but the code makes no queries.

Any ideas on what causes this issue? The code is fine, when executed directly.

PROJECT_FOLDER/node_modules/jest-runtime/build/index.js:439
    const wrapper = this._environment.runScript(script)[
                                                       ^

TypeError: Cannot read property 'Object.<anonymous>' of null
    at Runtime._execModule (PROJECT_FOLDER/node_modules/jest-runtime/build/index.js:439:56)
    at Runtime.requireModule (PROJECT_FOLDER/node_modules/jest-runtime/build/index.js:296:14)
    at Runtime.requireModuleOrMock (PROJECT_FOLDER/node_modules/jest-runtime/build/index.js:365:19)
    at Object.getCodec (PROJECT_FOLDER/node_modules/iconv-lite/lib/index.js:61:27)
    at Object.getDecoder (PROJECT_FOLDER/node_modules/iconv-lite/lib/index.js:118:23)
    at Object.<anonymous>.exports.decode (PROJECT_FOLDER/node_modules/mysql2/lib/parsers/string.js:19:23)
    at Packet.Object.<anonymous>.Packet.readNullTerminatedString (PROJECT_FOLDER/node_modules/mysql2/lib/packets/packet.js:371:23)
    at Function.Object.<anonymous>.Handshake.fromPacket (PROJECT_FOLDER/node_modules/mysql2/lib/packets/handshake.js:19:31)
    at ClientHandshake.Object.<anonymous>.ClientHandshake.handshakeInit (PROJECT_FOLDER/node_modules/mysql2/lib/commands/client_handshake.js:83:38)
    at ClientHandshake.Object.<anonymous>.Command.execute (PROJECT_FOLDER/node_modules/mysql2/lib/commands/command.js:39:20)
needs investigation

Most helpful comment

I found a temporary workaround for now, add this to your setupTestFrameworkScriptFile:

// Hack to make iconv load the encodings module, otherwise jest crashes. Compare
// https://github.com/sidorares/node-mysql2/issues/489
require('mysql2/node_modules/iconv-lite').encodingExists('foo');

Might look a bit different depending on your setup ;)

All 11 comments

looks like jest module system does not like for some reason dynamic require there:

if (!iconv.encodings) {
         iconv.encodings = require("../encodings");
}

can you try as a work around to monkey patch iconv object? E.i somewhere earlier

var iconv = require('iconv-lite')
iconv.encodings = {}; // probably need to put something here, like simple 1:1 decoder or encoder

@sidorares I'll try that and get back

@sidorares Thanks! I added the following lines early in my code, and the error went away:

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

looks like this is something worth reporting to jest, I'm closing for now but feel free to post updates here

@joelchu I'm afraid it's still too little information to debug. Does the error completely go away if mysql2 is not used?

@joelchu then, why do you comment on an issue related to mysql2 for node? 馃槃

@joelchu this repo has "nothing" to do with jest. This is node-mysql2 and this issue is about a combination of jest and node-mysql2. You should probably create an issue in the jest repo.

I found a temporary workaround for now, add this to your setupTestFrameworkScriptFile:

// Hack to make iconv load the encodings module, otherwise jest crashes. Compare
// https://github.com/sidorares/node-mysql2/issues/489
require('mysql2/node_modules/iconv-lite').encodingExists('foo');

Might look a bit different depending on your setup ;)

@opatut s/o, is working for me :)

Thanks @opatut

I found a temporary workaround for now, add this to your setupTestFrameworkScriptFile:

// Hack to make iconv load the encodings module, otherwise jest crashes. Compare
// https://github.com/sidorares/node-mysql2/issues/489
require('mysql2/node_modules/iconv-lite').encodingExists('foo');

Might look a bit different depending on your setup ;)

In typescript, just do:

// Hack to make iconv load the encodings module, otherwise jest crashes. Compare
// https://github.com/sidorares/node-mysql2/issues/489
import * as iconv from 'iconv-lite';
iconv.encodingExists('foo');

In my case, foo was cp1252

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hezjing picture hezjing  路  3Comments

HulioEglesias picture HulioEglesias  路  4Comments

patrikx3 picture patrikx3  路  7Comments

vcorr picture vcorr  路  5Comments

jomel picture jomel  路  8Comments