Integration test problem using Jest and ExpressJS

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.

New contributor

fr13nd230 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật