@omnitron-dev/testing 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Tests for test-adapter runtime detection and utilities
3
+ */
4
+
5
+ import {
6
+ isBun,
7
+ sleep,
8
+ isDeno,
9
+ isNode,
10
+ RUNTIME,
11
+ timerUtils,
12
+ supportsFakeTimers,
13
+ skipIfNoFakeTimers,
14
+ normalizeGlobalChecks,
15
+ } from './test-adapter';
16
+
17
+ describe('test-adapter runtime detection', () => {
18
+ it('should detect the current runtime', () => {
19
+ const validRuntimes = ['bun', 'deno', 'node'];
20
+ expect(validRuntimes).toContain(RUNTIME);
21
+ });
22
+
23
+ it('should have exactly one runtime flag set to true', () => {
24
+ const flags = [isBun, isDeno, isNode];
25
+ const trueFlags = flags.filter((flag) => flag === true);
26
+ expect(trueFlags.length).toBe(1);
27
+ });
28
+
29
+ it('should correctly detect Node.js runtime', () => {
30
+ if (typeof process !== 'undefined' && process.versions?.node) {
31
+ expect(isNode).toBe(true);
32
+ }
33
+ });
34
+
35
+ it('should correctly detect Bun runtime', () => {
36
+ if (typeof Bun !== 'undefined') {
37
+ expect(isBun).toBe(true);
38
+ }
39
+ });
40
+
41
+ it('should correctly detect Deno runtime', () => {
42
+ if (typeof Deno !== 'undefined') {
43
+ expect(isDeno).toBe(true);
44
+ }
45
+ });
46
+ });
47
+
48
+ describe('timer utilities', () => {
49
+ it('should report fake timers support correctly', () => {
50
+ const hasFakeTimers = supportsFakeTimers();
51
+ if (isNode && typeof (global as any).jest !== 'undefined') {
52
+ expect(hasFakeTimers).toBe(true);
53
+ } else {
54
+ expect(hasFakeTimers).toBe(false);
55
+ }
56
+ });
57
+
58
+ it('should provide timer utility methods', () => {
59
+ expect(typeof timerUtils.useFakeTimers).toBe('function');
60
+ expect(typeof timerUtils.useRealTimers).toBe('function');
61
+ expect(typeof timerUtils.advanceTimersByTime).toBe('function');
62
+ expect(typeof timerUtils.runAllTimers).toBe('function');
63
+ expect(typeof timerUtils.clearAllTimers).toBe('function');
64
+ expect(typeof timerUtils.canUseFakeTimers).toBe('function');
65
+ });
66
+
67
+ it('should correctly report skipIfNoFakeTimers', () => {
68
+ const shouldSkip = skipIfNoFakeTimers();
69
+ const hasFakeTimers = timerUtils.canUseFakeTimers();
70
+
71
+ // skipIfNoFakeTimers returns true if no fake timers, false if fake timers available
72
+ if (hasFakeTimers) {
73
+ expect(shouldSkip).toBe(false); // Don't skip if we have fake timers
74
+ } else {
75
+ expect(shouldSkip).toBe(true); // Skip if no fake timers
76
+ }
77
+ });
78
+ });
79
+
80
+ describe('utility functions', () => {
81
+ it('should provide sleep utility', async () => {
82
+ const start = Date.now();
83
+ await sleep(10);
84
+ const elapsed = Date.now() - start;
85
+ expect(elapsed).toBeGreaterThanOrEqual(9); // Allow for small timing variations
86
+ });
87
+
88
+ it('should handle normalizeGlobalChecks correctly', () => {
89
+ const result = normalizeGlobalChecks({});
90
+ expect(result).toBe(true);
91
+
92
+ if (isBun) {
93
+ expect(normalizeGlobalChecks(globalThis)).toBe(false);
94
+ } else {
95
+ expect(normalizeGlobalChecks(globalThis)).toBe(true);
96
+ }
97
+ });
98
+ });
@@ -0,0 +1,124 @@
1
+ /**
2
+ * Common test adapter utilities for cross-runtime testing
3
+ */
4
+
5
+ // Detect runtime environment
6
+ export const RUNTIME = (() => {
7
+ if (typeof (globalThis as any).Bun !== 'undefined') return 'bun';
8
+ if (typeof (globalThis as any).Deno !== 'undefined') return 'deno';
9
+ return 'node';
10
+ })();
11
+
12
+ // Timer utilities that work across runtimes
13
+ export const timerUtils = {
14
+ useFakeTimers() {
15
+ // Only Node.js with Jest supports fake timers
16
+ if (RUNTIME === 'node' && typeof (global as any).jest !== 'undefined') {
17
+ (global as any).jest.useFakeTimers();
18
+ }
19
+ },
20
+
21
+ useRealTimers() {
22
+ if (RUNTIME === 'node' && typeof (global as any).jest !== 'undefined') {
23
+ (global as any).jest.useRealTimers();
24
+ }
25
+ },
26
+
27
+ advanceTimersByTime(ms: number) {
28
+ if (RUNTIME === 'node' && typeof (global as any).jest !== 'undefined') {
29
+ (global as any).jest.advanceTimersByTime(ms);
30
+ } else {
31
+ // For Bun and Deno, we can't advance timers
32
+ // Tests that rely on this should be skipped or use real timers
33
+ }
34
+ },
35
+
36
+ runAllTimers() {
37
+ if (RUNTIME === 'node' && typeof (global as any).jest !== 'undefined') {
38
+ (global as any).jest.runAllTimers();
39
+ }
40
+ },
41
+
42
+ clearAllTimers() {
43
+ if (RUNTIME === 'node' && typeof (global as any).jest !== 'undefined') {
44
+ (global as any).jest.clearAllTimers();
45
+ }
46
+ },
47
+
48
+ canUseFakeTimers(): boolean {
49
+ return RUNTIME === 'node';
50
+ },
51
+ };
52
+
53
+ // Helper to skip tests that require features not available in current runtime
54
+ export function skipIfNoFakeTimers() {
55
+ if (!timerUtils.canUseFakeTimers()) {
56
+ return true; // Skip the test
57
+ }
58
+ return false;
59
+ }
60
+
61
+ // Normalize global object checks for different runtimes
62
+ export function normalizeGlobalChecks(value: any): boolean {
63
+ // In Bun, globalThis is considered a plain object, which differs from Node/Deno
64
+ if (RUNTIME === 'bun' && value === globalThis) {
65
+ return false;
66
+ }
67
+ return true;
68
+ }
69
+
70
+ // Export runtime detection utilities
71
+ export const isBun = RUNTIME === 'bun';
72
+ export const isDeno = RUNTIME === 'deno';
73
+ export const isNode = RUNTIME === 'node';
74
+ export const isJest =
75
+ isNode &&
76
+ typeof (globalThis as any).jest !== 'undefined' &&
77
+ typeof (globalThis as any).jest.useFakeTimers === 'function';
78
+
79
+ // Check if fake timers are supported
80
+ export const supportsFakeTimers = () => isJest;
81
+
82
+ // Timer utilities
83
+ export const setupFakeTimers = () => {
84
+ timerUtils.useFakeTimers();
85
+ };
86
+
87
+ export const teardownFakeTimers = () => {
88
+ timerUtils.useRealTimers();
89
+ };
90
+
91
+ export const advanceTimersByTime = (ms: number) => {
92
+ timerUtils.advanceTimersByTime(ms);
93
+ };
94
+
95
+ export const clearAllTimers = () => {
96
+ timerUtils.clearAllTimers();
97
+ };
98
+
99
+ // Promise utilities for cross-runtime compatibility
100
+ export const expectAsync = async (fn: () => Promise<any>) => {
101
+ if (isBun) {
102
+ // Bun has different handling for async expectations
103
+ try {
104
+ await fn();
105
+ throw new Error('Expected promise to reject but it resolved');
106
+ } catch (error) {
107
+ return {
108
+ toThrow: (expectedError?: any) => {
109
+ if (expectedError) {
110
+ (globalThis as any).expect(error).toEqual(expectedError);
111
+ } else {
112
+ (globalThis as any).expect(error).toBeDefined();
113
+ }
114
+ },
115
+ };
116
+ }
117
+ } else {
118
+ // Jest/Node.js
119
+ return (globalThis as any).expect(fn()).rejects;
120
+ }
121
+ };
122
+
123
+ // Sleep utility for real timer tests
124
+ export const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));