Hello there
Concerning the testing with Jest and ExpressJS, “undefined” issues is happening, resulting the failing of the test.
Test file:
import request from 'supertest'
const fetchUrl = "http://localhost:8080/api/v1"
const budgetMatch = ( budget: string | null ): boolean => ["0$ - 500$", "501$ - 1500$", "> 1500$", null].includes(budget)
const statusMatch = ( status: string | null ): boolean => ["created", "end", "confirmed", "done", "processed", "finished", "deleted", null].includes(status)
const isStringOrNull = ( value: any ): boolean => (typeof value === 'string' || value === null )
const isNumberOrNull = ( value: any ): boolean => (typeof value === 'number' || value === null )
describe('Jobs Route Integration Tests', () => {
describe('GET /api/v1/jobs', () => {
it('> Should Return An Array Of Items', async () => {
const res = await request( fetchUrl ).get(`/jobs`)
console.log(budgetMatch(res.body[0].budget))
expect(res.headers['content-type']?.split(';')[0]).toEqual('application/json')
expect(res.status).toEqual(200)
expect(typeof res.body).toBe('object')
expect(res.body.length).toBeGreaterThanOrEqual(1)
res.body.forEach((singleItem: any) => {
let testAgainst = {
"budget": expect(budgetMatch(singleItem.budget)).toBeTruthy(),
"Client_Feedback": expect(isStringOrNull(singleItem.Client_Feedback)).toBeTruthy(),
"client_id": expect(isStringOrNull(singleItem.client_id)).toBeTruthy(),
"clock": expect(isStringOrNull(singleItem.clock)).toBeTruthy(),
"country": expect(isStringOrNull(singleItem.country)).toBeTruthy(),
"created_at": expect(isStringOrNull(singleItem.created_at)).toBeTruthy(),
"description": expect(isStringOrNull(singleItem.description)).toBeTruthy(),
"endDate": expect(isStringOrNull(singleItem.endDate)).toBeTruthy(),
"endTime": expect(isStringOrNull(singleItem.endTime)).toBeTruthy(),
"estimated_time": expect(isStringOrNull(singleItem.estimated_time)).toBeTruthy(),
"FreeLancer_feedback": expect(isStringOrNull(singleItem.FreeLancer_feedback)).toBeTruthy(),
"id": expect.any(String),
"image1_url": expect(isStringOrNull(singleItem.image1_url)).toBeTruthy(),
"image2_url": expect(isStringOrNull(singleItem.image2_url)).toBeTruthy(),
"image3_url": expect(isStringOrNull(singleItem.image3_url)).toBeTruthy(),
"location_la": expect(isNumberOrNull(singleItem.location_la)).toBeTruthy(),
"location_lo": expect(isNumberOrNull(singleItem.location_lo)).toBeTruthy(),
"memo_url": expect(isStringOrNull(singleItem.memo_url)).toBeTruthy(),
"num_shares": expect.any(Number),
"startDate": expect(isStringOrNull(singleItem.startDate)).toBeTruthy(),
"startTime": expect(isStringOrNull(singleItem.startTime)).toBeTruthy(),
"status": expect(statusMatch(singleItem.status)).toBeTruthy(),
"title": expect.any(String),
"video_url": expect(isStringOrNull(singleItem.video_url)).toBeTruthy()
}
expect(singleItem).toMatchObject(testAgainst)
expect(new Date(singleItem.created_at)).not.toBe("Invalid Date")
})
})
})
})
And shall the response be always:
FAIL test/integration/jobs.integration.test.ts
Jobs Route Integration Tests
GET /api/v1/jobs
✕ > Should Return An Array Of Items (535 ms)
● Jobs Route Integration Tests › GET /api/v1/jobs › > Should Return An Array Of Items
expect(received).toMatchObject(expected)
- Expected - 21
+ Received + 21
Object {
- "Client_Feedback": undefined,
- "FreeLancer_feedback": undefined,
- "budget": undefined,
- "client_id": undefined,
- "clock": undefined,
- "country": undefined,
- "created_at": undefined,
- "description": undefined,
- "endDate": undefined,
- "endTime": undefined,
- "estimated_time": undefined,
+ "Client_Feedback": null,
+ "FreeLancer_feedback": null,
+ "budget": "> 1500$",
+ "client_id": "77239440-62ff-4870-a08e-a074524397f2",
+ "clock": null,
+ "country": null,
+ "created_at": "2024-06-10T17:53:38.229088+00:00",
+ "description": "jdjdjdjd",
+ "endDate": "2024-06-28",
+ "endTime": "4:51 AM",
+ "estimated_time": "2024-06-10",
"id": Any<String>,
- "image1_url": undefined,
- "image2_url": undefined,
- "image3_url": undefined,
- "location_la": undefined,
- "location_lo": undefined,
- "memo_url": undefined,
+ "image1_url": "https://firebasestorage.googleapis.com/v0/b/onlyskillsapp.appspot.com/o/77239440-62ff-4870-a08e-a074524397f2%2F5465.jpeg?alt=media&token=079f85a1-36db-4bfc-9e19-da6b06342868",
+ "image2_url": null,
+ "image3_url": null,
+ "location_la": 36.7629,
+ "location_lo": 2.9604,
+ "memo_url": null,
"num_shares": Any<Number>,
- "startDate": undefined,
- "startTime": undefined,
- "status": undefined,
+ "startDate": "2024-06-20",
+ "startTime": "4:51 AM",
+ "status": "deleted",
"title": Any<String>,
- "video_url": undefined,
+ "video_url": null,
}
51 | "video_url": expect(isStringOrNull(singleItem.video_url)).toBeTruthy()
52 | }
> 53 | expect(singleItem).toMatchObject(testAgainst)
| ^
54 | expect(new Date(singleItem.created_at)).not.toBe("Invalid Date")
55 | })
56 | })
at test/integration/jobs.integration.test.ts:53:36
at Array.forEach (<anonymous>)
at Object.<anonymous> (test/integration/jobs.integration.test.ts:25:22)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.338 s
Ran all test suites.
So any suggestions, also i really appreciate if anyone can guide me through the best practices of integration testing.
I tried to resolve this thing but i couldn’t except trying to expect every property in a line.
fr13nd230 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.