@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.
package/package.json ADDED
@@ -0,0 +1,97 @@
1
+ {
2
+ "name": "@omnitron-dev/testing",
3
+ "version": "0.4.0",
4
+ "description": "Cross-runtime testing utilities",
5
+ "author": "LuxQuant",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
+ },
17
+ "./runtime/jest": {
18
+ "types": "./runtime/jest.d.ts",
19
+ "import": "./runtime/jest.js",
20
+ "require": "./runtime/jest.js"
21
+ },
22
+ "./runtime/bun": {
23
+ "types": "./runtime/bun.d.ts",
24
+ "import": "./runtime/bun.js",
25
+ "require": "./runtime/bun.js"
26
+ },
27
+ "./runtime/deno": {
28
+ "types": "./dist/runtime/deno-adapter.d.ts",
29
+ "import": "./dist/runtime/deno-adapter.js",
30
+ "require": "./dist/runtime/deno-adapter.cjs"
31
+ },
32
+ "./runtime/node": {
33
+ "types": "./dist/runtime/node-adapter.d.ts",
34
+ "import": "./dist/runtime/node-adapter.js",
35
+ "require": "./dist/runtime/node-adapter.cjs"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist",
40
+ "src",
41
+ "runtime"
42
+ ],
43
+ "dependencies": {
44
+ "@types/node": "^22.10.5"
45
+ },
46
+ "devDependencies": {
47
+ "@types/bun": "^1.2.3",
48
+ "@types/jest": "^29.5.14",
49
+ "jest": "^30.1.0",
50
+ "ts-jest": "^29.2.5",
51
+ "ts-node": "^10.9.2",
52
+ "typescript": "~5.8.3"
53
+ },
54
+ "peerDependencies": {
55
+ "bun": "*",
56
+ "jest": "^30.0.0 || ^29.0.0"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "bun": {
60
+ "optional": true
61
+ },
62
+ "jest": {
63
+ "optional": true
64
+ }
65
+ },
66
+ "engines": {
67
+ "node": ">=22.0.0",
68
+ "bun": ">=1.2.0"
69
+ },
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "git+https://github.com/devgrid/devgrid.git",
73
+ "directory": "packages/testing"
74
+ },
75
+ "keywords": [
76
+ "testing",
77
+ "cross-runtime",
78
+ "bun",
79
+ "deno",
80
+ "node",
81
+ "jest",
82
+ "test-utilities"
83
+ ],
84
+ "publishConfig": {
85
+ "access": "public"
86
+ },
87
+ "scripts": {
88
+ "build": "tsc -p tsconfig.json",
89
+ "clean": "rm -rf dist *.tsbuildinfo",
90
+ "lint": "eslint src --max-warnings=0",
91
+ "lint:fix": "eslint src --fix",
92
+ "test": "jest",
93
+ "test:bun": "bun test src/**/*.test.ts",
94
+ "test:deno": "deno test --allow-all src/**/*.test.ts",
95
+ "typecheck": "tsc --noEmit"
96
+ }
97
+ }
@@ -0,0 +1,2 @@
1
+ // Type definitions for Bun runtime adapter
2
+ export {};
package/runtime/bun.js ADDED
@@ -0,0 +1,5 @@
1
+ // Bun runtime adapter
2
+ // This file sets up global test functions for Bun runtime
3
+
4
+ // Bun already provides describe, test, expect globally
5
+ // No additional setup needed
package/runtime/bun.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Bun runtime adapter
3
+ * This file is imported in Bun tests to provide Jest compatibility
4
+ */
5
+
6
+ // Import the Bun adapter which sets up global Jest-like APIs
7
+ import '../src/runtime/bun-adapter';
@@ -0,0 +1,2 @@
1
+ // Type definitions for Deno runtime adapter
2
+ export {};
@@ -0,0 +1,4 @@
1
+ // Deno runtime adapter
2
+ // This file sets up global test functions for Deno runtime
3
+
4
+ // Deno uses built-in test functions, no additional setup needed
@@ -0,0 +1,2 @@
1
+ // Type definitions for Jest runtime adapter
2
+ export {};
@@ -0,0 +1,16 @@
1
+ // Jest runtime adapter
2
+ // Jest provides all globals automatically
3
+
4
+ // Re-export jest globals if needed
5
+ if (typeof global !== 'undefined' && global.jest) {
6
+ module.exports = {
7
+ describe: global.describe,
8
+ test: global.test,
9
+ it: global.it,
10
+ expect: global.expect,
11
+ beforeAll: global.beforeAll,
12
+ beforeEach: global.beforeEach,
13
+ afterEach: global.afterEach,
14
+ afterAll: global.afterAll,
15
+ };
16
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Jest runtime adapter
3
+ * This file is imported in jest.setup.ts for Node.js tests
4
+ */
5
+
6
+ // Jest is already available in Node.js environment
7
+ // This file exists for consistency across runtimes
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Global type declarations for cross-runtime testing
3
+ */
4
+
5
+ declare global {
6
+ // Bun global
7
+ const Bun: any;
8
+
9
+ // Deno global
10
+ const Deno: any;
11
+
12
+ // Jest globals (for Node.js)
13
+ const jest: any;
14
+ const describe: any;
15
+ const it: any;
16
+ const test: any;
17
+ const expect: any;
18
+ const beforeEach: any;
19
+ const afterEach: any;
20
+ const beforeAll: any;
21
+ const afterAll: any;
22
+ }
23
+
24
+ export {};
package/src/index.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @devgrid/testing - Cross-runtime testing utilities
3
+ */
4
+
5
+ export * from './runtime/test-adapter.js';
6
+
7
+ // Re-export specific runtime adapters based on environment
8
+ export { RUNTIME } from './runtime/test-adapter.js';
9
+
10
+ // Export utility types
11
+ export type MockFunction<T extends (...args: any[]) => any> = T & {
12
+ mock: {
13
+ calls: Parameters<T>[];
14
+ results: ReturnType<T>[];
15
+ lastCall: () => Parameters<T> | undefined;
16
+ };
17
+ mockImplementation: (impl: T) => MockFunction<T>;
18
+ mockReturnValue: (value: ReturnType<T>) => MockFunction<T>;
19
+ mockResolvedValue: (value: Awaited<ReturnType<T>>) => MockFunction<T>;
20
+ mockRejectedValue: (value: any) => MockFunction<T>;
21
+ mockClear: () => void;
22
+ mockReset: () => void;
23
+ };
24
+
25
+ // Helper function to import the correct adapter based on runtime
26
+ export function loadRuntimeAdapter() {
27
+ const runtime = (() => {
28
+ if (typeof (globalThis as any).Bun !== 'undefined') return 'bun';
29
+ if (typeof (globalThis as any).Deno !== 'undefined') return 'deno';
30
+ return 'node';
31
+ })();
32
+
33
+ switch (runtime) {
34
+ case 'bun':
35
+ return import('./runtime/bun-adapter.js');
36
+ case 'deno':
37
+ return import('./runtime/deno-adapter.js');
38
+ default:
39
+ return import('./runtime/node-adapter.js');
40
+ }
41
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Bun adapter for running Jest tests
3
+ * Provides compatibility layer for Jest API
4
+ */
5
+
6
+ import {
7
+ mock,
8
+ test as bunTest,
9
+ expect as bunExpect,
10
+ describe as bunDescribe,
11
+ afterAll as bunAfterAll,
12
+ afterEach as bunAfterEach,
13
+ beforeAll as bunBeforeAll,
14
+ beforeEach as bunBeforeEach,
15
+ } from 'bun:test';
16
+
17
+ import { timerUtils } from './test-adapter.js';
18
+
19
+ // Make Jest-compatible APIs globally available
20
+ (global as any).describe = bunDescribe;
21
+ (global as any).test = bunTest;
22
+ (global as any).it = bunTest;
23
+ (global as any).expect = bunExpect;
24
+ (global as any).beforeEach = bunBeforeEach;
25
+ (global as any).afterEach = bunAfterEach;
26
+ (global as any).beforeAll = bunBeforeAll;
27
+ (global as any).afterAll = bunAfterAll;
28
+
29
+ // Use timer utilities from shared adapter
30
+ const fakeTimers = {
31
+ useFakeTimers: () => {
32
+ timerUtils.useFakeTimers();
33
+ return fakeTimers;
34
+ },
35
+ useRealTimers: () => {
36
+ timerUtils.useRealTimers();
37
+ return fakeTimers;
38
+ },
39
+ advanceTimersByTime: (ms: number) => {
40
+ timerUtils.advanceTimersByTime(ms);
41
+ },
42
+ runAllTimers: () => {
43
+ timerUtils.runAllTimers();
44
+ },
45
+ clearAllTimers: () => {
46
+ timerUtils.clearAllTimers();
47
+ },
48
+ };
49
+
50
+ // Mock Jest APIs
51
+ (global as any).jest = {
52
+ fn: mock,
53
+ mock,
54
+ useFakeTimers: fakeTimers.useFakeTimers,
55
+ useRealTimers: fakeTimers.useRealTimers,
56
+ advanceTimersByTime: fakeTimers.advanceTimersByTime,
57
+ runAllTimers: fakeTimers.runAllTimers,
58
+ clearAllTimers: fakeTimers.clearAllTimers,
59
+ };
60
+
61
+ // Add missing expect matchers if needed
62
+ const originalExpect = bunExpect as any;
63
+ if (!originalExpect.toBeInstanceOf) {
64
+ originalExpect.extend({
65
+ toBeInstanceOf(received: any, constructor: any) {
66
+ const pass = received instanceof constructor;
67
+ return {
68
+ pass,
69
+ message: () =>
70
+ pass
71
+ ? `expected ${received} not to be instance of ${constructor.name}`
72
+ : `expected ${received} to be instance of ${constructor.name}`,
73
+ };
74
+ },
75
+ });
76
+ }
77
+
78
+ if (!originalExpect.toStrictEqual) {
79
+ // toStrictEqual is an alias for toEqual in Bun
80
+ originalExpect.toStrictEqual = originalExpect.toEqual;
81
+ }
82
+
83
+ if (!originalExpect.toThrowError) {
84
+ originalExpect.extend({
85
+ toThrowError(received: any, expected?: any) {
86
+ let error: any;
87
+ let pass = false;
88
+
89
+ try {
90
+ if (typeof received === 'function') {
91
+ received();
92
+ }
93
+ } catch (e) {
94
+ error = e;
95
+ pass = true;
96
+
97
+ if (expected !== undefined) {
98
+ if (typeof expected === 'string') {
99
+ pass = error.message.includes(expected);
100
+ } else if (expected instanceof RegExp) {
101
+ pass = expected.test(error.message);
102
+ } else if (typeof expected === 'function') {
103
+ pass = error instanceof expected;
104
+ }
105
+ }
106
+ }
107
+
108
+ return {
109
+ pass,
110
+ message: () =>
111
+ pass
112
+ ? `expected function not to throw${expected ? ` ${expected}` : ''}`
113
+ : `expected function to throw${expected ? ` ${expected}` : ''}`,
114
+ };
115
+ },
116
+ });
117
+ }
@@ -0,0 +1,272 @@
1
+ /**
2
+ * Deno adapter for running Jest tests
3
+ * Provides compatibility layer for Jest API
4
+ */
5
+
6
+ // @ts-expect-error - Deno specific imports
7
+ import { FakeTime } from 'https://deno.land/std@0.208.0/testing/time.ts';
8
+ // @ts-expect-error - Deno specific imports
9
+ import { expect as denoExpect } from 'https://deno.land/x/expect@v0.3.0/mod.ts';
10
+ // @ts-expect-error - Deno specific imports
11
+ import { it as denoIt, describe as denoDescribe } from 'https://deno.land/std@0.208.0/testing/bdd.ts';
12
+
13
+ // Polyfill for node:util inherits
14
+ function inherits(ctor: any, superCtor: any) {
15
+ if (ctor === undefined || ctor === null) {
16
+ throw new TypeError('The constructor to "inherits" must not be null or undefined');
17
+ }
18
+ if (superCtor === undefined || superCtor === null) {
19
+ throw new TypeError('The super constructor to "inherits" must not be null or undefined');
20
+ }
21
+ if (superCtor.prototype === undefined) {
22
+ throw new TypeError('The super constructor to "inherits" must have a prototype property');
23
+ }
24
+ ctor.super_ = superCtor;
25
+ Object.setPrototypeOf(ctor.prototype, superCtor.prototype);
26
+ }
27
+
28
+ // Store global state
29
+ let fakeTime: FakeTime | null = null;
30
+ const beforeEachCallbacks: Array<() => void | Promise<void>> = [];
31
+ const afterEachCallbacks: Array<() => void | Promise<void>> = [];
32
+ const beforeAllCallbacks: Array<() => void | Promise<void>> = [];
33
+ const afterAllCallbacks: Array<() => void | Promise<void>> = [];
34
+
35
+ // Custom describe that handles lifecycle hooks
36
+ function describe(name: string, fn: () => void) {
37
+ denoDescribe(name, () => {
38
+ // Clear callbacks for this describe block
39
+ // Note: These are preserved for potential future use in nested describe blocks
40
+ // const localBeforeEach = [...beforeEachCallbacks];
41
+ // const localAfterEach = [...afterEachCallbacks];
42
+ const localBeforeAll = [...beforeAllCallbacks];
43
+ const localAfterAll = [...afterAllCallbacks];
44
+
45
+ // Run beforeAll hooks
46
+ if (localBeforeAll.length > 0) {
47
+ denoIt('beforeAll', async () => {
48
+ for (const cb of localBeforeAll) {
49
+ await cb();
50
+ }
51
+ });
52
+ }
53
+
54
+ fn();
55
+
56
+ // Run afterAll hooks
57
+ if (localAfterAll.length > 0) {
58
+ denoIt('afterAll', async () => {
59
+ for (const cb of localAfterAll) {
60
+ await cb();
61
+ }
62
+ });
63
+ }
64
+ });
65
+ }
66
+
67
+ // Custom it/test that runs lifecycle hooks
68
+ function it(name: string, fn: () => void | Promise<void>) {
69
+ denoIt(name, async () => {
70
+ // Run beforeEach hooks
71
+ for (const cb of beforeEachCallbacks) {
72
+ await cb();
73
+ }
74
+
75
+ try {
76
+ await fn();
77
+ } finally {
78
+ // Run afterEach hooks
79
+ for (const cb of afterEachCallbacks) {
80
+ await cb();
81
+ }
82
+ }
83
+ });
84
+ }
85
+
86
+ // Lifecycle hooks
87
+ function beforeEach(fn: () => void | Promise<void>) {
88
+ beforeEachCallbacks.push(fn);
89
+ }
90
+
91
+ function afterEach(fn: () => void | Promise<void>) {
92
+ afterEachCallbacks.push(fn);
93
+ }
94
+
95
+ function beforeAll(fn: () => void | Promise<void>) {
96
+ beforeAllCallbacks.push(fn);
97
+ }
98
+
99
+ function afterAll(fn: () => void | Promise<void>) {
100
+ afterAllCallbacks.push(fn);
101
+ }
102
+
103
+ // Mock function implementation
104
+ function mockFn(implementation?: (...args: any[]) => any) {
105
+ const calls: any[][] = [];
106
+ const results: any[] = [];
107
+
108
+ const fn = (...args: any[]) => {
109
+ calls.push(args);
110
+ const result = implementation ? implementation(...args) : undefined;
111
+ results.push(result);
112
+ return result;
113
+ };
114
+
115
+ fn.mock = {
116
+ calls,
117
+ results,
118
+ lastCall: () => calls[calls.length - 1],
119
+ };
120
+
121
+ fn.mockImplementation = (impl: (...args: any[]) => any) => {
122
+ implementation = impl;
123
+ return fn;
124
+ };
125
+
126
+ fn.mockReturnValue = (value: any) => {
127
+ implementation = () => value;
128
+ return fn;
129
+ };
130
+
131
+ fn.mockResolvedValue = (value: any) => {
132
+ implementation = () => Promise.resolve(value);
133
+ return fn;
134
+ };
135
+
136
+ fn.mockRejectedValue = (value: any) => {
137
+ implementation = () => Promise.reject(value);
138
+ return fn;
139
+ };
140
+
141
+ fn.mockClear = () => {
142
+ calls.length = 0;
143
+ results.length = 0;
144
+ };
145
+
146
+ fn.mockReset = () => {
147
+ calls.length = 0;
148
+ results.length = 0;
149
+ implementation = undefined;
150
+ };
151
+
152
+ return fn;
153
+ }
154
+
155
+ // Mock timer functions
156
+ const fakeTimers = {
157
+ useFakeTimers: () => {
158
+ fakeTime = new FakeTime();
159
+ return fakeTimers;
160
+ },
161
+ useRealTimers: () => {
162
+ if (fakeTime) {
163
+ fakeTime.restore();
164
+ fakeTime = null;
165
+ }
166
+ return fakeTimers;
167
+ },
168
+ advanceTimersByTime: (ms: number) => {
169
+ if (fakeTime) {
170
+ fakeTime.tick(ms);
171
+ }
172
+ },
173
+ runAllTimers: () => {
174
+ if (fakeTime) {
175
+ fakeTime.runAll();
176
+ }
177
+ },
178
+ clearAllTimers: () => {
179
+ if (fakeTime) {
180
+ fakeTime.restore();
181
+ fakeTime = new FakeTime();
182
+ }
183
+ },
184
+ };
185
+
186
+ // Custom expect wrapper that adds missing methods
187
+ const expect = (value: any) => {
188
+ const matcher = denoExpect(value);
189
+
190
+ // Add missing matcher methods to the chain
191
+ matcher.toBeInstanceOf = function toBeInstanceOf(constructor: any) {
192
+ const pass = value instanceof constructor;
193
+ if (!pass) {
194
+ throw new Error(`expected ${value} to be instance of ${constructor.name}`);
195
+ }
196
+ return { pass };
197
+ };
198
+
199
+ matcher.toStrictEqual = function toStrictEqual(expected: any) {
200
+ return matcher.toEqual(expected);
201
+ };
202
+
203
+ matcher.toThrowError = function toThrowError(expected?: any) {
204
+ let error: any;
205
+ let thrown = false;
206
+
207
+ try {
208
+ if (typeof value === 'function') {
209
+ value();
210
+ }
211
+ } catch (e) {
212
+ error = e;
213
+ thrown = true;
214
+ }
215
+
216
+ if (!thrown) {
217
+ throw new Error('expected function to throw');
218
+ }
219
+
220
+ if (expected !== undefined) {
221
+ if (typeof expected === 'string' && !error.message.includes(expected)) {
222
+ throw new Error(`expected error message to include "${expected}"`);
223
+ }
224
+ if (expected instanceof RegExp && !expected.test(error.message)) {
225
+ throw new Error(`expected error message to match ${expected}`);
226
+ }
227
+ if (typeof expected === 'function' && !(error instanceof expected)) {
228
+ throw new Error(`expected error to be instance of ${expected.name}`);
229
+ }
230
+ }
231
+
232
+ return { pass: true };
233
+ };
234
+
235
+ return matcher;
236
+ };
237
+
238
+ // Add static methods
239
+ expect.extend = (matchers: Record<string, any>) => {
240
+ Object.assign(expect, matchers);
241
+ };
242
+
243
+ // Make APIs globally available
244
+ (globalThis as any).describe = describe;
245
+ (globalThis as any).test = it;
246
+ (globalThis as any).it = it;
247
+ (globalThis as any).expect = expect;
248
+ (globalThis as any).beforeEach = beforeEach;
249
+ (globalThis as any).afterEach = afterEach;
250
+ (globalThis as any).beforeAll = beforeAll;
251
+ (globalThis as any).afterAll = afterAll;
252
+
253
+ // Mock Jest APIs
254
+ (globalThis as any).jest = {
255
+ fn: mockFn,
256
+ mock: mockFn,
257
+ useFakeTimers: fakeTimers.useFakeTimers,
258
+ useRealTimers: fakeTimers.useRealTimers,
259
+ advanceTimersByTime: fakeTimers.advanceTimersByTime,
260
+ runAllTimers: fakeTimers.runAllTimers,
261
+ clearAllTimers: fakeTimers.clearAllTimers,
262
+ };
263
+
264
+ // Make Node.js APIs available globally for tests
265
+ (globalThis as any).node = {
266
+ util: {
267
+ inherits,
268
+ },
269
+ };
270
+
271
+ // Export everything
272
+ export { it, expect, mockFn, describe, afterAll, inherits, afterEach, beforeAll, beforeEach };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Node.js adapter for Jest tests
3
+ * This is mostly a passthrough since Jest natively runs in Node.js
4
+ */
5
+
6
+ import { timerUtils } from './test-adapter.js';
7
+
8
+ // Export timer utilities for consistency
9
+ export const fakeTimers = {
10
+ useFakeTimers: () => {
11
+ timerUtils.useFakeTimers();
12
+ return fakeTimers;
13
+ },
14
+ useRealTimers: () => {
15
+ timerUtils.useRealTimers();
16
+ return fakeTimers;
17
+ },
18
+ advanceTimersByTime: (ms: number) => {
19
+ timerUtils.advanceTimersByTime(ms);
20
+ },
21
+ runAllTimers: () => {
22
+ timerUtils.runAllTimers();
23
+ },
24
+ clearAllTimers: () => {
25
+ timerUtils.clearAllTimers();
26
+ },
27
+ };
28
+
29
+ // Re-export Jest globals for consistency with other adapters
30
+ export const describe = (global as any).describe;
31
+ export const it = (global as any).it;
32
+ export const test = (global as any).test;
33
+ export const expect = (global as any).expect;
34
+ export const beforeEach = (global as any).beforeEach;
35
+ export const afterEach = (global as any).afterEach;
36
+ export const beforeAll = (global as any).beforeAll;
37
+ export const afterAll = (global as any).afterAll;
38
+
39
+ // Re-export Jest object
40
+ export const jest = (global as any).jest;