Skip to content

Commit f192614

Browse files
author
John Doe
committed
refactor: add int-test
1 parent 8c21478 commit f192614

File tree

3 files changed

+108
-13
lines changed

3 files changed

+108
-13
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import process from 'node:process';
2+
import { threadId } from 'node:worker_threads';
3+
import { describe, expect, it } from 'vitest';
4+
import { defaultClock, epochClock } from './clock-epoch.js';
5+
6+
describe('epochClock', () => {
7+
it('should create epoch clock with defaults', () => {
8+
const c = epochClock();
9+
expect(c).toStrictEqual(
10+
expect.objectContaining({
11+
tid: threadId,
12+
pid: process.pid,
13+
timeOriginMs: performance.timeOrigin,
14+
}),
15+
);
16+
expect(typeof c.fromEpochMs).toBe('function');
17+
expect(typeof c.fromEpochUs).toBe('function');
18+
expect(typeof c.fromPerfMs).toBe('function');
19+
expect(typeof c.fromEntryStartTimeMs).toBe('function');
20+
expect(typeof c.fromDateNowMs).toBe('function');
21+
});
22+
23+
it('should support performance clock by default for epochNowUs', () => {
24+
const c = epochClock();
25+
expect(c.timeOriginMs).toBe(performance.timeOrigin);
26+
const nowUs = c.epochNowUs();
27+
expect(nowUs).toBe(Math.round(nowUs));
28+
const expectedUs = Date.now() * 1000;
29+
30+
expect(nowUs).toBeGreaterThan(expectedUs - 1000);
31+
expect(nowUs).toBeLessThan(expectedUs + 1000);
32+
});
33+
34+
it('should convert epoch milliseconds to microseconds correctly', () => {
35+
const c = epochClock();
36+
const epochMs = Date.now();
37+
38+
const result = c.fromEpochMs(epochMs);
39+
expect(result).toBe(Math.round(epochMs * 1000));
40+
expect(result).toBe(Math.round(result));
41+
});
42+
43+
it('should convert epoch microseconds to microseconds correctly', () => {
44+
const c = epochClock();
45+
const epochUs = Date.now() * 1000;
46+
const expectedUs = Math.round(epochUs);
47+
48+
const result = c.fromEpochUs(epochUs);
49+
expect(result).toBe(expectedUs);
50+
expect(result).toBe(Math.round(result));
51+
});
52+
53+
it('should convert performance milliseconds to epoch microseconds correctly', () => {
54+
const c = epochClock();
55+
const perfMs = performance.now();
56+
const expectedUs = Math.round((c.timeOriginMs + perfMs) * 1000);
57+
58+
const result = c.fromPerfMs(perfMs);
59+
expect(result).toBe(expectedUs);
60+
expect(result).toBe(Math.round(result));
61+
});
62+
63+
it('should convert entry start time milliseconds to epoch microseconds correctly', () => {
64+
const c = epochClock();
65+
const entryStartMs = performance.mark('fromPerfMs').startTime;
66+
const expectedUs = Math.round((c.timeOriginMs + entryStartMs) * 1000);
67+
68+
const result = c.fromEntryStartTimeMs(entryStartMs);
69+
expect(result).toBe(expectedUs);
70+
expect(result).toBe(Math.round(result));
71+
});
72+
73+
it('should convert Date.now milliseconds to epoch microseconds correctly', () => {
74+
const c = epochClock();
75+
const dateNowMs = Date.now();
76+
77+
const result = c.fromDateNowMs(dateNowMs);
78+
expect(result).toBe(Math.round(dateNowMs * 1000));
79+
expect(result).toBe(Math.round(result));
80+
});
81+
});
82+
83+
describe('defaultClock', () => {
84+
it('should have valid defaultClock export', () => {
85+
const c = defaultClock;
86+
expect(c).toStrictEqual(
87+
expect.objectContaining({
88+
tid: threadId,
89+
pid: process.pid,
90+
timeOriginMs: performance.timeOrigin,
91+
}),
92+
);
93+
94+
expect(typeof c.fromEpochMs).toBe('function');
95+
expect(typeof c.fromEpochUs).toBe('function');
96+
expect(typeof c.fromPerfMs).toBe('function');
97+
expect(typeof c.fromEntryStartTimeMs).toBe('function');
98+
expect(typeof c.fromDateNowMs).toBe('function');
99+
});
100+
});

packages/utils/src/lib/clock-epoch.unit.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { afterEach, describe, expect, it, vi } from 'vitest';
1+
import { describe, expect, it } from 'vitest';
22
import { defaultClock, epochClock } from './clock-epoch.js';
33

44
describe('epochClock', () => {
5-
afterEach(() => {
6-
vi.unstubAllGlobals();
7-
});
8-
95
it('should create epoch clock with defaults', () => {
106
const c = epochClock();
117
expect(c.timeOriginMs).toBe(500_000);
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import process from 'node:process';
2-
import workerThreadsModule from 'node:worker_threads';
32
import { afterEach, beforeEach, vi } from 'vitest';
43

54
export const MOCK_PID = 10_001;
65
export const MOCK_TID = 2;
76

8-
// Mock immediately when the setup file loads for default exports using process.pid or threadId
9-
const processMock = vi.spyOn(process, 'pid', 'get').mockReturnValue(MOCK_PID);
10-
const threadIdMock = vi
11-
.spyOn(workerThreadsModule, 'threadId', 'get')
12-
.mockReturnValue(MOCK_TID);
7+
vi.mock('node:worker_threads', () => ({
8+
get threadId() {
9+
return MOCK_TID;
10+
},
11+
}));
12+
13+
const processMock = vi.spyOn(process, 'pid', 'get');
1314

1415
beforeEach(() => {
1516
processMock.mockReturnValue(MOCK_PID);
16-
threadIdMock.mockReturnValue(MOCK_TID);
1717
});
1818

1919
afterEach(() => {
2020
processMock.mockClear();
21-
threadIdMock.mockClear();
2221
});

0 commit comments

Comments
 (0)