I have this problem when running npm test
through jest.
The application is the backend of the MongoDB database.
It runs normally, but the only test fails.
Error
npm test
> [email protected] test
> jest
(node:7280) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version`
(Use node --trace-warnings ... to show where the warning was created)`
(node:7280) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology
has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
console.log
✅ MongoDB Conectado: aula-shard-00-00.7obon.mongodb
at log (src/db.js:9:13)
console.log
Resposta do login: { message: 'Usuário não encontrado' }
at Object.log (src/tests/user.test.js:30:13)
console.log
Token recebido: undefined
at Object.log (src/tests/user.test.js:33:13)
console.log
Resposta ao criar usuário: {
message: 'Erro ao criar usuário',
error: {
errors: { name: [Object], password: [Object] },
_message: 'User validation failed',
name: 'ValidationError',
message: 'User validation failed: name: Path name is required., password: Path password
is required.'
}
}
at Object.log (src/tests/user.test.js:49:13)
FAIL src/tests/user.test.js (5.537 s)
Testes da API de Usuários
× Deve criar um usuário (220 ms)
× Deve buscar usuários (106 ms)
× Deve criar um usuário (88 ms)
● Testes da API de Usuários › Deve criar um usuário
expect(received).toBe(expected) // Object.is equality
Expected: 201
Received: 500
18 | senha: "senha123",
19 | });
> 20 | expect(res.statusCode).toBe(201);
| ^
21 | expect(res.body).toHaveProperty("token");
22 | });
23 |
at Object.toBe (src/tests/user.test.js:20:28)
● Testes da API de Usuários › Deve buscar usuários
expect(received).toBe(expected) // Object.is equality
Expected: 200
Received: 401
37 | .set("Authorization", `Bearer ${token}`);
38 |
> 39 | expect(res.statusCode).toBe(200);
| ^
40 | expect(Array.isArray(res.body)).toBeTruthy();
41 | });
42 | it("Deve criar um usuário", async () => {
at Object.toBe (src/tests/user.test.js:39:28)
● Testes da API de Usuários › Deve criar um usuário
expect(received).toBe(expected) // Object.is equality
Expected: 201
Received: 500
49 | console.log("Resposta ao criar usuário:", res.body); //