Error on integration test login function
Only sometimes the integration tests fail on the login method in the beforeAll function, giving error 401 "Unauthorized" or giving error on a foreign key constant. Here is the code.
401 "Unauthorized
Login Function
const login = async (userInfo: any) => {
return new Promise<string>((resolve, reject) => {
request(app)
.post(``${routePath}/sessions``)
.send(userInfo)
.expect(200)
.end((err, res) => {
if (err) {
reject(err)
}
resolve(res.header["set-cookie"][0])
})
})
}
beforeAll
beforeAll(async () => {
cleanup()
await postUser(customer)
customerCookie = await login(customer)
await postUser(customer2)
customerCookie2 = await login(customer2)
await postUser(customer3)
customerCookie3 = await login(customer3)
await postUser(admin)
adminCookie = await login(admin)
})
Console log
POST /ezelectronics/users 200 110.568 ms - -
POST /ezelectronics/sessions 401 11.962 ms - 48
● Cart Integration Tests › GET /carts/all › Correct test cases › Should return an array with all the carts
expected 200 "OK", got 401 "Unauthorized"
36 | .post(``${routePath}/sessions``)
37 | .send(userInfo)
> 38 | .expect(200)
| ^
39 | .end((err, res) => {
40 | if (err) {
41 | reject(err)
at test_integration/car.test.ts:38:14
at test_integration/car.test.ts:34:12
at test_integration/car.test.ts:8:71
at Object.<anonymous>.__awaiter (test_integration/car.test.ts:4:12)
at login (test_integration/car.test.ts:33:39)
at test_integration/car.test.ts:51:28
at fulfilled (test_integration/car.test.ts:5:58)
----
at Test._assertStatus (node_modules/supertest/lib/test.js:252:14)
at node_modules/supertest/lib/test.js:308:13
at Test._assertFunction (node_modules/supertest/lib/test.js:285:13)
at Test.assert (node_modules/supertest/lib/test.js:164:23)
at Server.localAssert (node_modules/supertest/lib/test.js:120:14)
● Test suite failed to run
TypeError: Cannot read properties of undefined (reading '0')
41 | reject(err)
42 | }
> 43 | resolve(res.header["set-cookie"][0])
| ^
44 | })
45 | })
46 | }
at Test.<anonymous> (test_integration/car.test.ts:43:49)
at Test.assert (node_modules/supertest/lib/test.js:172:8)
at Server.localAssert (node_modules/supertest/lib/test.js:120:14)
Foreign key constraint
FAIL test_integration/prod.test.ts
● Test suite failed to run
SQLITE_CONSTRAINT: FOREIGN KEY constraint failed
● Test suite failed to run
SQLITE_CONSTRAINT: FOREIGN KEY constraint failed
Edited by Luca Giusiano