Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wierd issue when running with jest #489

Closed
danieljuhl opened this issue Jan 14, 2017 · 13 comments
Closed

Wierd issue when running with jest #489

danieljuhl opened this issue Jan 14, 2017 · 13 comments

Comments

@danieljuhl
Copy link
Contributor

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)
@sidorares
Copy link
Owner

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

@danieljuhl
Copy link
Contributor Author

@sidorares I'll try that and get back

@danieljuhl
Copy link
Contributor Author

danieljuhl commented Jan 15, 2017

@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;

@sidorares
Copy link
Owner

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

@sidorares
Copy link
Owner

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

@danieljuhl
Copy link
Contributor Author

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

@danieljuhl
Copy link
Contributor Author

danieljuhl commented Jun 27, 2017

@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.

@opatut
Copy link

opatut commented Jul 6, 2017

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 ;)

@tobiaslins
Copy link

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

@rimiti
Copy link

rimiti commented Jul 19, 2018

Thanks @opatut

@v1d3rm3
Copy link

v1d3rm3 commented Dec 22, 2018

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

ryantzor pushed a commit to ryantzor/bazu-codetest that referenced this issue Nov 16, 2021
- Add promise for connections, getConnection, endConnection
- Add temporary hack for jest typing issues sidorares/node-mysql2#489
ryantzor pushed a commit to ryantzor/bazu-codetest that referenced this issue Nov 16, 2021
- Add promise for connections, getConnection, endConnection
- Add temporary hack for jest typing issues sidorares/node-mysql2#489
@JeganMurugan01
Copy link

JeganMurugan01 commented Aug 29, 2023

i am facing same issue any one have a solution for these
image

@cvanputt
Copy link

As of version 2.x and later for mysql2:
require('iconv-lite').encodingExists('foo');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants