Skip to content

Commit 3df0bc6

Browse files
nicohrubecclaude
andauthored
tests(core): Fix flaky metric sequence number test (#19754)
Mock `timestampInSeconds` in the "increments the sequence number across consecutive metrics" test to return a fixed value. The test was flaky because consecutive calls could land on different milliseconds, causing the sequence counter to reset unexpectedly. Closes #19749 --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 10b6d86 commit 3df0bc6

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

packages/core/test/lib/metrics/internal.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '../../../src/metrics/internal';
88
import type { Metric } from '../../../src/types-hoist/metric';
99
import * as loggerModule from '../../../src/utils/debug-logger';
10+
import * as timeModule from '../../../src/utils/time';
1011
import { _INTERNAL_resetSequenceNumber } from '../../../src/utils/timestampSequence';
1112
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';
1213

@@ -1080,6 +1081,10 @@ describe('_INTERNAL_captureMetric', () => {
10801081

10811082
describe('sentry.timestamp.sequence', () => {
10821083
it('increments the sequence number across consecutive metrics', () => {
1084+
// Mock timestampInSeconds to return a fixed value so the sequence number
1085+
// does not reset due to millisecond boundary crossings between calls.
1086+
const timestampSpy = vi.spyOn(timeModule, 'timestampInSeconds').mockReturnValue(1234.567);
1087+
10831088
const options = getDefaultTestClientOptions({ dsn: PUBLIC_DSN });
10841089
const client = new TestClient(options);
10851090
const scope = new Scope();
@@ -1093,6 +1098,8 @@ describe('_INTERNAL_captureMetric', () => {
10931098
expect(buffer?.[0]?.attributes?.['sentry.timestamp.sequence']).toEqual({ value: 0, type: 'integer' });
10941099
expect(buffer?.[1]?.attributes?.['sentry.timestamp.sequence']).toEqual({ value: 1, type: 'integer' });
10951100
expect(buffer?.[2]?.attributes?.['sentry.timestamp.sequence']).toEqual({ value: 2, type: 'integer' });
1101+
1102+
timestampSpy.mockRestore();
10961103
});
10971104

10981105
it('resets the sequence number via _INTERNAL_resetSequenceNumber', () => {

0 commit comments

Comments
 (0)