When trying to test with Jest on NodeJs backend, I get
Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
I have MySql as database.
I’m using jest, and trying to test login, but apparently, it also need to establish sequelize MySQL connection.
It’s about Encoding not recognized: ‘cesu8’ (searched as: ‘cesu8’)",
I’ve searched some “solutions”, but none of them work.
1, 2
In case, of trying this solution require('mysql2/node_modules/iconv-lite').encodingExists('foo');
, I don’t know what to put in “foo” ? If I put “cesu8”, it’s not supported, because truly there’s nothing in that node folder , I checked, that would support. It’s just utf16 , utf32 ,etc…
Neither this
-
Please help, without this, I’m completely unable to test anything in my app
-
Error after running
npx jest
Attempted to log "ErrorSequelizeConnectionError: Encoding not recognized: 'cesu8' (searched as: 'cesu8')".
23 |
24 |
> 25 |
| ^
26 |
27 |
28 | sequelize.authenticate()
at console.log (node_modules/@jest/console/build/CustomConsole.js:141:10)
at models/database.js:25:11
C:\Users\Igor\Downloads\Randolympics_backend\node_modules\sequelize\lib\dialects\mysql\connection-manager.js:102
throw new SequelizeErrors.ConnectionError(err);
^
ConnectionError [SequelizeConnectionError]: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at ConnectionManager.connect (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\sequelize\src\dialects\mysql\connection-manager.js:126:17)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at ConnectionManager._connect (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\sequelize\src\dialects\abstract\connection-manager.js:332:24)
at C:\Users\Igor\Downloads\Randolympics_backend\node_modules\sequelize\src\dialects\abstract\connection-manager.js:250:32 {
parent: Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at Object.getCodec (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:104:23)
at Object.getDecoder (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:125:23)
at Object.<anonymous>.exports.decode (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\parsers\string.js:20:23)
at Packet.readNullTerminatedString (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\packets\packet.js:412:25)
at Function.fromPacket (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\packets\handshake.js:62:33)
at ClientHandshake.handshakeInit (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\commands\client_handshake.js:112:40)
at ClientHandshake.execute (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\commands\command.js:45:22) at Connection.handlePacket (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\connection.js:481:34)
at PacketParser.onPacket (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\connection.js:97:12)
at PacketParser.executeStart (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\connection.js:104:25)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Socket.Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:191:23) {
fatal: true
},
original: Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at Object.getCodec (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:104:23)
at Object.getDecoder (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\node_modules\iconv-lite\lib\index.js:125:23)
at Object.<anonymous>.exports.decode (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\parsers\string.js:20:23)
at Packet.readNullTerminatedString (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\packets\packet.js:412:25)
at Function.fromPacket (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\packets\handshake.js:62:33)
at ClientHandshake.handshakeInit (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\commands\client_handshake.js:112:40)
at ClientHandshake.execute (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\commands\command.js:45:22) at Connection.handlePacket (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\connection.js:481:34)
at PacketParser.onPacket (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\connection.js:97:12)
at PacketParser.executeStart (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\Users\Igor\Downloads\Randolympics_backend\node_modules\mysql2\lib\connection.js:104:25)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Socket.Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:191:23) {
fatal: true
}
}
Node.js v20.16.0
- And this is my
__tests__/account.test.js
const request = require("supertest");
const db = require("../models/database");
const app = require("../server");
describe("login", () => {
describe("correct username and password", () => {
test("should respond with 200 status code", async () => {
const response = await request(app).post("/auth/login").send({
email: "ah-1-F-us@gmail.com",
password: "12345678",
});
expect(response.statusCode).resolves.toBe(200);
});
});
describe("incorrect username and password", () => {});
});
- And
models/database.js
const dbConfig = require("../data/config");
const { Sequelize, DataTypes } = require("sequelize");
const sequelize = new Sequelize(
dbConfig.database,
dbConfig.user,
dbConfig.password,
{
host: dbConfig.host,
dialect: "mysql",
operatorsAliases: false,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
dialectOptions: {
charset: "utf8mb4",
},
}
);
sequelize
.authenticate()
.then(() => {
console.log("connected..");
})
.catch((err) => {
console.log("Error" + err);
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.users = require("./users")(sequelize, DataTypes);
db.traffic = require("./traffic")(sequelize, DataTypes);
db.upcominggames = require("./upcominggames")(sequelize, DataTypes);
db.news = require("./news")(sequelize, DataTypes);
db.economics = require("./economics")(sequelize, DataTypes);
db.campaign = require("./campaign")(sequelize, DataTypes);
db.statscampaign = require("./statscampaign")(sequelize, DataTypes);
db.couponcode = require("./couponcode")(sequelize, DataTypes);
// model for News
db.sequelize.sync({ force: false }).then(() => {
console.log("yes re-sync is done!");
});
module.exports = db;