> nodejs-app@1.0.0 test
> jest
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Server running on http://localhost:3000".
14 |
15 | require('./formData')(app); // Handle form submission logic
> 16 |
| ^
17 | app.listen(PORT, () => {
18 | console.log(`Server running on http://localhost:${PORT}`);
19 | });
at console.log (node_modules/@jest/console/build/CustomConsole.js:141:10)
at Server. (src/server.js:16:11)
FAIL tests/form.test.js POST /submit-form
✕ should submit form data successfully (3 ms)
● POST /submit-form › should submit form data successfully
TypeError: app.address is not a function
5 | it('should submit form data successfully', async () => {
6 | const res = await request(app)
> 7 | .post('/submit-form')
| ^
8 | .send({
9 | name: 'John Doe',
10 | place: 'New York',
at Test.serverAddress (node_modules/supertest/lib/test.js:46:22)
at new Test (node_modules/supertest/lib/test.js:34:14)
at Object.obj. [as post] (node_modules/supertest/index.js:43:18)
at Object.post (tests/form.test.js:7:14)
Test Suites: 1 failed, 1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 0.413 s, estimated 1 s Ran all test suites.
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From tests/form.test.js.
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..exports.decode (node_modules/mysql2/lib/parsers/string.js:20:23)
at Packet.readNullTerminatedString (node_modules/mysql2/lib/packets/packet.js:414:25)
at Function.fromPacket (node_modules/mysql2/lib/packets/handshake.js:62:33)
at ClientHandshake.handshakeInit (node_modules/mysql2/lib/commands/client_handshake.js:112:40)
at ClientHandshake.execute (node_modules/mysql2/lib/commands/command.js:45:22)
at Connection.handlePacket (node_modules/mysql2/lib/base/connection.js:475:34)
at PacketParser.onPacket (node_modules/mysql2/lib/base/connection.js:93:12)
at PacketParser.executeStart (node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket. (node_modules/mysql2/lib/base/connection.js
throw err; // Unhandled 'error' event
^
Error: Unhandled error. (Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at Object.getCodec (/home/node/nodejs-app/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
at Object.getDecoder (/home/node/nodejs-app/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:125:23)
at Object..exports.decode (/home/node/nodejs-app/node_modules/mysql2/lib/parsers/string.js:20:23)
at Packet.readNullTerminatedString (/home/node/nodejs-app/node_modules/mysql2/lib/packets/packet.js:414:25)
at Function.fromPacket (/home/node/nodejs-app/node_modules/mysql2/lib/packets/handshake.js:62:33)
at ClientHandshake.handshakeInit (/home/node/nodejs-app/node_modules/mysql2/lib/commands/client_handshake.js:112:40)
at ClientHandshake.execute (/home/node/nodejs-app/node_modules/mysql2/lib/commands/command.js:45:22)
at Connection.handlePacket (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:475:34)
at PacketParser.onPacket (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:93:12)
at PacketParser.executeStart (/home/node/nodejs-app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket. (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js
at Socket.emit (node:events:524:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Socket.Readable.push (node:internal/streams/readable:392:5)
at TCP.onStreamRead (node:internal/stream_base_commons:189:23) { fatal: true })
at Connection.emit (node:events:513:17)
at Connection._notifyError (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:247:12)
at Connection._handleFatalError (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:178:10)
at Connection.handlePacket (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:485:12)
at PacketParser.onPacket (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:93:12)
at PacketParser.executeStart (/home/node/nodejs-app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket. (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js
at Socket.emit (node:events:524:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Socket.Readable.push (node:internal/streams/readable:392:5)
at TCP.onStreamRead (node:internal/stream_base_commons:189:23) { code: 'ERR_UNHANDLED_ERROR', context: Error: Encoding not recognized: 'cesu8' (searched as: 'cesu8')
at Object.getCodec (/home/node/nodejs-app/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:104:23)
at Object.getDecoder (/home/node/nodejs-app/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:125:23)
at Object..exports.decode (/home/node/nodejs-app/node_modules/mysql2/lib/parsers/string.js:20:23)
at Packet.readNullTerminatedString (/home/node/nodejs-app/node_modules/mysql2/lib/packets/packet.js:414:25)
at Function.fromPacket (/home/node/nodejs-app/node_modules/mysql2/lib/packets/handshake.js:62:33)
at ClientHandshake.handshakeInit (/home/node/nodejs-app/node_modules/mysql2/lib/commands/client_handshake.js:112:40)
at ClientHandshake.execute (/home/node/nodejs-app/node_modules/mysql2/lib/commands/command.js:45:22)
at Connection.handlePacket (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:475:34)
at PacketParser.onPacket (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js:93:12)
at PacketParser.executeStart (/home/node/nodejs-app/node_modules/mysql2/lib/packet_parser.js:75:16)
at Socket. (/home/node/nodejs-app/node_modules/mysql2/lib/base/connection.js
at Socket.emit (node:events:524:28)
at addChunk (node:internal/streams/readable:561:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
at Socket.Readable.push (node:internal/streams/readable:392:5)
at TCP.onStreamRead (node:internal/stream_base_commons:189:23) {
fatal: true } }
Node.js v22.13.0
Я создал тестовый код:
const request = require('supertest'); const app = require('../src/server');
describe('POST /submit-form', () => {
it('should submit form data successfully', async () => {
const res = await request(app)
.post('/submit-form')
.send({
name: 'John Doe',
place: 'New York',
email: 'john@example.com',
phone: '1234567890',
age: 30,
technology: 'Node.js',
});
expect(res.text).toBe('Form data submitted successfully!');
}); });
Подробнее здесь: https://stackoverflow.com/questions/793 ... pplication
Мобильная версия