Skip to content

Commit 15a95d5

Browse files
committed
fix: flaky tests
1 parent 615197f commit 15a95d5

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

app/components/UI/Perps/hooks/useHasNewMarkets.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ const mockUsePerpsMarkets = usePerpsMarkets as jest.MockedFunction<
99
>;
1010

1111
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
12-
const RECENT_LISTED_AT = Date.now() - 5 * ONE_DAY_MS;
13-
const OLD_LISTED_AT = Date.now() - 45 * ONE_DAY_MS;
12+
const NOW = 1_700_000_000_000; // fixed epoch ms for determinism
13+
const RECENT_LISTED_AT = NOW - 5 * ONE_DAY_MS;
14+
const OLD_LISTED_AT = NOW - 45 * ONE_DAY_MS;
1415

1516
function mockMarkets(markets: { listedAt?: number }[]) {
1617
mockUsePerpsMarkets.mockReturnValue({
@@ -19,6 +20,14 @@ function mockMarkets(markets: { listedAt?: number }[]) {
1920
}
2021

2122
describe('useHasNewMarkets', () => {
23+
beforeEach(() => {
24+
jest.spyOn(Date, 'now').mockReturnValue(NOW);
25+
});
26+
27+
afterEach(() => {
28+
jest.restoreAllMocks();
29+
});
30+
2231
it('returns true when at least one market was listed within the last 30 days', () => {
2332
mockMarkets([{ listedAt: OLD_LISTED_AT }, { listedAt: RECENT_LISTED_AT }]);
2433

app/components/UI/Perps/hooks/usePerpsMarketListView.test.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,13 @@ const mockAllMarkets = [
108108
...mockMarketsWithInvalidVolume,
109109
];
110110

111+
const NOW = 1_700_000_000_000; // fixed epoch ms for deterministic listedAt math
112+
const DAYS_AGO = (days: number) => NOW - days * 24 * 60 * 60 * 1000;
113+
111114
describe('usePerpsMarketListView', () => {
112115
beforeEach(() => {
113116
jest.clearAllMocks();
117+
jest.spyOn(Date, 'now').mockReturnValue(NOW);
114118

115119
// Reset sortMarkets mock to pass through by default
116120
mockSortMarkets.mockImplementation(({ markets }) => markets);
@@ -158,6 +162,10 @@ describe('usePerpsMarketListView', () => {
158162
mockSelectorState({ watchlist: ['BTC', 'ETH'] });
159163
});
160164

165+
afterEach(() => {
166+
jest.restoreAllMocks();
167+
});
168+
161169
describe('Initial State', () => {
162170
it('returns correct initial state with default parameters', () => {
163171
const { result } = renderHook(() => usePerpsMarketListView());
@@ -664,18 +672,18 @@ describe('usePerpsMarketListView', () => {
664672
{
665673
...createMockMarket('BTC', '$1B'),
666674
isHip3: false,
667-
listedAt: Date.now() - 5 * 24 * 60 * 60 * 1000, // 5 days ago
675+
listedAt: DAYS_AGO(5), // 5 days ago
668676
},
669677
{
670678
...createMockMarket('AAPL', '$2B'),
671679
marketType: 'stock' as const,
672680
isHip3: true,
673-
listedAt: Date.now() - 10 * 24 * 60 * 60 * 1000, // 10 days ago
681+
listedAt: DAYS_AGO(10), // 10 days ago
674682
},
675683
{
676684
...createMockMarket('OLDCOIN', '$500M'),
677685
isHip3: false,
678-
listedAt: Date.now() - 45 * 24 * 60 * 60 * 1000, // 45 days ago, not new
686+
listedAt: DAYS_AGO(45), // 45 days ago, not new
679687
},
680688
];
681689

@@ -989,19 +997,19 @@ describe('usePerpsMarketListView', () => {
989997
{
990998
...createMockMarket('SHIB2', '$100M'),
991999
isHip3: false,
992-
listedAt: Date.now() - 3 * 24 * 60 * 60 * 1000, // 3 days ago, new
1000+
listedAt: DAYS_AGO(3), // 3 days ago, new
9931001
},
9941002
{
9951003
...createMockMarket('CASHCAT', '$50M'),
9961004
marketType: 'stock' as const,
9971005
isHip3: true,
998-
listedAt: Date.now() - 20 * 24 * 60 * 60 * 1000, // 20 days ago, new
1006+
listedAt: DAYS_AGO(20), // 20 days ago, new
9991007
},
10001008
{
10011009
...createMockMarket('OLDIPO', '$30M'),
10021010
marketType: 'pre-ipo' as const,
10031011
isHip3: true,
1004-
listedAt: Date.now() - 60 * 24 * 60 * 60 * 1000, // 60 days ago, not new
1012+
listedAt: DAYS_AGO(60), // 60 days ago, not new
10051013
},
10061014
];
10071015

0 commit comments

Comments
 (0)