Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions tests/server/routes/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ jest.mock('../../../server/models/User', () => ({
findRecent: jest.fn().mockResolvedValue([
{ username: 'testuser1', displayName: 'Test User 1' },
{ username: 'testuser2', displayName: 'Test User 2' }
]),
countDocuments: jest.fn().mockResolvedValue(2),
find: jest.fn().mockResolvedValue([
{ username: 'recentuser', lastActive: new Date().toISOString() }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of new Date().toISOString() directly in the test mock (line 15) can introduce flakiness in tests. If the test runs around midnight and the date changes, it could lead to inconsistent results.

Recommendation: Consider using a fixed timestamp or mocking the Date object to return a consistent value during tests to ensure test reliability.

])
Comment on lines +14 to 16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Consider using a fixed date string in User.find mock for test predictability.

Using a static date string for lastActive will make test results consistent and prevent flakiness from dynamic timestamps.

Suggested change
find: jest.fn().mockResolvedValue([
{ username: 'recentuser', lastActive: new Date().toISOString() }
])
find: jest.fn().mockResolvedValue([
{ username: 'recentuser', lastActive: '2023-01-01T00:00:00.000Z' }
])

}));

jest.mock('../../../server/models/Item', () => ({
jest.mock('../../../server/models/ScrapyardItem', () => ({
findRecent: jest.fn().mockResolvedValue([
{ id: 1, title: 'Test Item 1' },
{ id: 2, title: 'Test Item 2' }
]),
findFeatured: jest.fn().mockResolvedValue([
{ id: 3, title: 'Featured Item 1' },
{ id: 4, title: 'Featured Item 2' }
])
]),
countDocuments: jest.fn().mockResolvedValue(2)
}));

// Mock express-handlebars
Expand Down