@ricsam/isolate-test-environment 0.0.1 → 0.1.1

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/src/index.ts ADDED
@@ -0,0 +1,655 @@
1
+ import type ivm from "isolated-vm";
2
+
3
+ export interface TestEnvironmentHandle {
4
+ dispose(): void;
5
+ }
6
+
7
+ export interface TestResults {
8
+ passed: number;
9
+ failed: number;
10
+ total: number;
11
+ results: TestResult[];
12
+ }
13
+
14
+ export interface TestResult {
15
+ name: string;
16
+ passed: boolean;
17
+ error?: string;
18
+ duration: number;
19
+ skipped?: boolean;
20
+ }
21
+
22
+ const testEnvironmentCode = `
23
+ (function() {
24
+ // ============================================================
25
+ // Internal State
26
+ // ============================================================
27
+
28
+ function createSuite(name, skip = false, only = false) {
29
+ return {
30
+ name,
31
+ tests: [],
32
+ children: [],
33
+ beforeAll: [],
34
+ afterAll: [],
35
+ beforeEach: [],
36
+ afterEach: [],
37
+ skip,
38
+ only,
39
+ };
40
+ }
41
+
42
+ const rootSuite = createSuite('root');
43
+ let currentSuite = rootSuite;
44
+ const suiteStack = [rootSuite];
45
+
46
+ // ============================================================
47
+ // Deep Equality Helper
48
+ // ============================================================
49
+
50
+ function deepEqual(a, b) {
51
+ if (a === b) return true;
52
+ if (typeof a !== typeof b) return false;
53
+ if (typeof a !== 'object' || a === null || b === null) return false;
54
+
55
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
56
+
57
+ const keysA = Object.keys(a);
58
+ const keysB = Object.keys(b);
59
+ if (keysA.length !== keysB.length) return false;
60
+
61
+ for (const key of keysA) {
62
+ if (!keysB.includes(key)) return false;
63
+ if (!deepEqual(a[key], b[key])) return false;
64
+ }
65
+ return true;
66
+ }
67
+
68
+ function strictDeepEqual(a, b) {
69
+ if (a === b) return true;
70
+ if (typeof a !== typeof b) return false;
71
+ if (typeof a !== 'object' || a === null || b === null) return false;
72
+
73
+ // Check prototypes
74
+ if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false;
75
+
76
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
77
+
78
+ // For arrays, check sparse arrays (holes)
79
+ if (Array.isArray(a)) {
80
+ if (a.length !== b.length) return false;
81
+ for (let i = 0; i < a.length; i++) {
82
+ const aHasIndex = i in a;
83
+ const bHasIndex = i in b;
84
+ if (aHasIndex !== bHasIndex) return false;
85
+ if (aHasIndex && !strictDeepEqual(a[i], b[i])) return false;
86
+ }
87
+ return true;
88
+ }
89
+
90
+ // Check for undefined properties vs missing properties
91
+ const keysA = Object.keys(a);
92
+ const keysB = Object.keys(b);
93
+ if (keysA.length !== keysB.length) return false;
94
+
95
+ for (const key of keysA) {
96
+ if (!keysB.includes(key)) return false;
97
+ if (!strictDeepEqual(a[key], b[key])) return false;
98
+ }
99
+
100
+ // Check for symbol properties
101
+ const symbolsA = Object.getOwnPropertySymbols(a);
102
+ const symbolsB = Object.getOwnPropertySymbols(b);
103
+ if (symbolsA.length !== symbolsB.length) return false;
104
+
105
+ for (const sym of symbolsA) {
106
+ if (!symbolsB.includes(sym)) return false;
107
+ if (!strictDeepEqual(a[sym], b[sym])) return false;
108
+ }
109
+
110
+ return true;
111
+ }
112
+
113
+ function getNestedProperty(obj, path) {
114
+ const parts = path.split('.');
115
+ let current = obj;
116
+ for (const part of parts) {
117
+ if (current == null || !(part in current)) {
118
+ return { exists: false };
119
+ }
120
+ current = current[part];
121
+ }
122
+ return { exists: true, value: current };
123
+ }
124
+
125
+ function formatValue(val) {
126
+ if (val === null) return 'null';
127
+ if (val === undefined) return 'undefined';
128
+ if (typeof val === 'string') return JSON.stringify(val);
129
+ if (typeof val === 'object') {
130
+ try {
131
+ return JSON.stringify(val);
132
+ } catch {
133
+ return String(val);
134
+ }
135
+ }
136
+ return String(val);
137
+ }
138
+
139
+ // ============================================================
140
+ // expect() Implementation
141
+ // ============================================================
142
+
143
+ function expect(actual) {
144
+ function createMatchers(negated = false) {
145
+ const assert = (condition, message) => {
146
+ const pass = negated ? !condition : condition;
147
+ if (!pass) {
148
+ throw new Error(message);
149
+ }
150
+ };
151
+
152
+ const matchers = {
153
+ toBe(expected) {
154
+ assert(
155
+ actual === expected,
156
+ negated
157
+ ? \`Expected \${formatValue(actual)} not to be \${formatValue(expected)}\`
158
+ : \`Expected \${formatValue(actual)} to be \${formatValue(expected)}\`
159
+ );
160
+ },
161
+
162
+ toEqual(expected) {
163
+ assert(
164
+ deepEqual(actual, expected),
165
+ negated
166
+ ? \`Expected \${formatValue(actual)} not to equal \${formatValue(expected)}\`
167
+ : \`Expected \${formatValue(actual)} to equal \${formatValue(expected)}\`
168
+ );
169
+ },
170
+
171
+ toStrictEqual(expected) {
172
+ assert(
173
+ strictDeepEqual(actual, expected),
174
+ negated
175
+ ? \`Expected \${formatValue(actual)} not to strictly equal \${formatValue(expected)}\`
176
+ : \`Expected \${formatValue(actual)} to strictly equal \${formatValue(expected)}\`
177
+ );
178
+ },
179
+
180
+ toBeTruthy() {
181
+ assert(
182
+ !!actual,
183
+ negated
184
+ ? \`Expected \${formatValue(actual)} not to be truthy\`
185
+ : \`Expected \${formatValue(actual)} to be truthy\`
186
+ );
187
+ },
188
+
189
+ toBeFalsy() {
190
+ assert(
191
+ !actual,
192
+ negated
193
+ ? \`Expected \${formatValue(actual)} not to be falsy\`
194
+ : \`Expected \${formatValue(actual)} to be falsy\`
195
+ );
196
+ },
197
+
198
+ toBeNull() {
199
+ assert(
200
+ actual === null,
201
+ negated
202
+ ? \`Expected \${formatValue(actual)} not to be null\`
203
+ : \`Expected \${formatValue(actual)} to be null\`
204
+ );
205
+ },
206
+
207
+ toBeUndefined() {
208
+ assert(
209
+ actual === undefined,
210
+ negated
211
+ ? \`Expected \${formatValue(actual)} not to be undefined\`
212
+ : \`Expected \${formatValue(actual)} to be undefined\`
213
+ );
214
+ },
215
+
216
+ toBeDefined() {
217
+ assert(
218
+ actual !== undefined,
219
+ negated
220
+ ? \`Expected \${formatValue(actual)} not to be defined\`
221
+ : \`Expected \${formatValue(actual)} to be defined\`
222
+ );
223
+ },
224
+
225
+ toContain(item) {
226
+ let contains = false;
227
+ if (Array.isArray(actual)) {
228
+ contains = actual.includes(item);
229
+ } else if (typeof actual === 'string') {
230
+ contains = actual.includes(item);
231
+ }
232
+ assert(
233
+ contains,
234
+ negated
235
+ ? \`Expected \${formatValue(actual)} not to contain \${formatValue(item)}\`
236
+ : \`Expected \${formatValue(actual)} to contain \${formatValue(item)}\`
237
+ );
238
+ },
239
+
240
+ toThrow(expected) {
241
+ if (typeof actual !== 'function') {
242
+ throw new Error('toThrow requires a function');
243
+ }
244
+
245
+ let threw = false;
246
+ let error = null;
247
+ try {
248
+ actual();
249
+ } catch (e) {
250
+ threw = true;
251
+ error = e;
252
+ }
253
+
254
+ if (expected !== undefined) {
255
+ const matches = threw && (
256
+ (typeof expected === 'string' && error.message.includes(expected)) ||
257
+ (expected instanceof RegExp && expected.test(error.message)) ||
258
+ (typeof expected === 'function' && error instanceof expected)
259
+ );
260
+ assert(
261
+ matches,
262
+ negated
263
+ ? \`Expected function not to throw \${formatValue(expected)}\`
264
+ : \`Expected function to throw \${formatValue(expected)}, but \${threw ? \`threw: \${error.message}\` : 'did not throw'}\`
265
+ );
266
+ } else {
267
+ assert(
268
+ threw,
269
+ negated
270
+ ? \`Expected function not to throw\`
271
+ : \`Expected function to throw\`
272
+ );
273
+ }
274
+ },
275
+
276
+ toBeInstanceOf(cls) {
277
+ assert(
278
+ actual instanceof cls,
279
+ negated
280
+ ? \`Expected \${formatValue(actual)} not to be instance of \${cls.name || cls}\`
281
+ : \`Expected \${formatValue(actual)} to be instance of \${cls.name || cls}\`
282
+ );
283
+ },
284
+
285
+ toHaveLength(length) {
286
+ const actualLength = actual?.length;
287
+ assert(
288
+ actualLength === length,
289
+ negated
290
+ ? \`Expected length not to be \${length}, but got \${actualLength}\`
291
+ : \`Expected length to be \${length}, but got \${actualLength}\`
292
+ );
293
+ },
294
+
295
+ toMatch(pattern) {
296
+ let matches = false;
297
+ if (typeof pattern === 'string') {
298
+ matches = actual.includes(pattern);
299
+ } else if (pattern instanceof RegExp) {
300
+ matches = pattern.test(actual);
301
+ }
302
+ assert(
303
+ matches,
304
+ negated
305
+ ? \`Expected \${formatValue(actual)} not to match \${pattern}\`
306
+ : \`Expected \${formatValue(actual)} to match \${pattern}\`
307
+ );
308
+ },
309
+
310
+ toHaveProperty(path, value) {
311
+ const prop = getNestedProperty(actual, path);
312
+ const hasProperty = prop.exists;
313
+ const valueMatches = arguments.length < 2 || deepEqual(prop.value, value);
314
+
315
+ assert(
316
+ hasProperty && valueMatches,
317
+ negated
318
+ ? \`Expected \${formatValue(actual)} not to have property \${path}\${arguments.length >= 2 ? \` with value \${formatValue(value)}\` : ''}\`
319
+ : \`Expected \${formatValue(actual)} to have property \${path}\${arguments.length >= 2 ? \` with value \${formatValue(value)}\` : ''}\`
320
+ );
321
+ },
322
+ };
323
+
324
+ return matchers;
325
+ }
326
+
327
+ const matchers = createMatchers(false);
328
+ matchers.not = createMatchers(true);
329
+
330
+ return matchers;
331
+ }
332
+
333
+ // ============================================================
334
+ // Test Registration Functions
335
+ // ============================================================
336
+
337
+ function describe(name, fn) {
338
+ const suite = createSuite(name);
339
+ currentSuite.children.push(suite);
340
+
341
+ const parentSuite = currentSuite;
342
+ currentSuite = suite;
343
+ suiteStack.push(suite);
344
+
345
+ fn();
346
+
347
+ suiteStack.pop();
348
+ currentSuite = parentSuite;
349
+ }
350
+
351
+ describe.skip = function(name, fn) {
352
+ const suite = createSuite(name, true, false);
353
+ currentSuite.children.push(suite);
354
+
355
+ const parentSuite = currentSuite;
356
+ currentSuite = suite;
357
+ suiteStack.push(suite);
358
+
359
+ fn();
360
+
361
+ suiteStack.pop();
362
+ currentSuite = parentSuite;
363
+ };
364
+
365
+ describe.only = function(name, fn) {
366
+ const suite = createSuite(name, false, true);
367
+ currentSuite.children.push(suite);
368
+
369
+ const parentSuite = currentSuite;
370
+ currentSuite = suite;
371
+ suiteStack.push(suite);
372
+
373
+ fn();
374
+
375
+ suiteStack.pop();
376
+ currentSuite = parentSuite;
377
+ };
378
+
379
+ function test(name, fn) {
380
+ currentSuite.tests.push({
381
+ name,
382
+ fn,
383
+ skip: false,
384
+ only: false,
385
+ });
386
+ }
387
+
388
+ test.skip = function(name, fn) {
389
+ currentSuite.tests.push({
390
+ name,
391
+ fn,
392
+ skip: true,
393
+ only: false,
394
+ });
395
+ };
396
+
397
+ test.only = function(name, fn) {
398
+ currentSuite.tests.push({
399
+ name,
400
+ fn,
401
+ skip: false,
402
+ only: true,
403
+ });
404
+ };
405
+
406
+ test.todo = function(name) {
407
+ currentSuite.tests.push({
408
+ name,
409
+ fn: null,
410
+ skip: false,
411
+ only: false,
412
+ todo: true,
413
+ });
414
+ };
415
+
416
+ const it = test;
417
+ it.skip = test.skip;
418
+ it.only = test.only;
419
+ it.todo = test.todo;
420
+
421
+ // ============================================================
422
+ // Lifecycle Hooks
423
+ // ============================================================
424
+
425
+ function beforeEach(fn) {
426
+ currentSuite.beforeEach.push(fn);
427
+ }
428
+
429
+ function afterEach(fn) {
430
+ currentSuite.afterEach.push(fn);
431
+ }
432
+
433
+ function beforeAll(fn) {
434
+ currentSuite.beforeAll.push(fn);
435
+ }
436
+
437
+ function afterAll(fn) {
438
+ currentSuite.afterAll.push(fn);
439
+ }
440
+
441
+ // ============================================================
442
+ // Test Runner
443
+ // ============================================================
444
+
445
+ function checkForOnly(suite) {
446
+ if (suite.only) return true;
447
+ for (const t of suite.tests) {
448
+ if (t.only) return true;
449
+ }
450
+ for (const child of suite.children) {
451
+ if (checkForOnly(child)) return true;
452
+ }
453
+ return false;
454
+ }
455
+
456
+ function suiteHasOnly(suite) {
457
+ if (suite.only) return true;
458
+ for (const t of suite.tests) {
459
+ if (t.only) return true;
460
+ }
461
+ for (const child of suite.children) {
462
+ if (suiteHasOnly(child)) return true;
463
+ }
464
+ return false;
465
+ }
466
+
467
+ async function __runAllTests() {
468
+ const results = [];
469
+ const hasOnly = checkForOnly(rootSuite);
470
+
471
+ async function runSuite(suite, parentHooks, namePath) {
472
+ // Skip if this suite doesn't have any .only when .only exists elsewhere
473
+ if (hasOnly && !suiteHasOnly(suite)) return;
474
+
475
+ // Skip if suite is marked as skip
476
+ if (suite.skip) {
477
+ // Mark all tests in this suite as skipped
478
+ for (const t of suite.tests) {
479
+ results.push({
480
+ name: namePath ? namePath + ' > ' + t.name : t.name,
481
+ passed: true,
482
+ skipped: true,
483
+ duration: 0,
484
+ });
485
+ }
486
+ return;
487
+ }
488
+
489
+ // Run beforeAll hooks
490
+ for (const hook of suite.beforeAll) {
491
+ await hook();
492
+ }
493
+
494
+ // Run tests
495
+ for (const t of suite.tests) {
496
+ const testName = namePath ? namePath + ' > ' + t.name : t.name;
497
+
498
+ // Skip if .only is used and this test isn't .only AND the suite doesn't have .only
499
+ if (hasOnly && !t.only && !suite.only) continue;
500
+
501
+ // Skip if test is marked as skip
502
+ if (t.skip) {
503
+ results.push({
504
+ name: testName,
505
+ passed: true,
506
+ skipped: true,
507
+ duration: 0,
508
+ });
509
+ continue;
510
+ }
511
+
512
+ // Handle todo tests (no function provided)
513
+ if (t.todo) {
514
+ results.push({
515
+ name: testName,
516
+ passed: true,
517
+ skipped: true,
518
+ duration: 0,
519
+ });
520
+ continue;
521
+ }
522
+
523
+ const start = Date.now();
524
+ try {
525
+ // Run all beforeEach hooks (parent first, then current)
526
+ for (const hook of [...parentHooks.beforeEach, ...suite.beforeEach]) {
527
+ await hook();
528
+ }
529
+
530
+ // Run test
531
+ await t.fn();
532
+
533
+ // Run all afterEach hooks (current first, then parent)
534
+ for (const hook of [...suite.afterEach, ...parentHooks.afterEach]) {
535
+ await hook();
536
+ }
537
+
538
+ results.push({
539
+ name: testName,
540
+ passed: true,
541
+ duration: Date.now() - start,
542
+ });
543
+ } catch (err) {
544
+ results.push({
545
+ name: testName,
546
+ passed: false,
547
+ error: err.message || String(err),
548
+ duration: Date.now() - start,
549
+ });
550
+ }
551
+ }
552
+
553
+ // Run child suites
554
+ for (const child of suite.children) {
555
+ const childPath = namePath ? namePath + ' > ' + child.name : child.name;
556
+ await runSuite(child, {
557
+ beforeEach: [...parentHooks.beforeEach, ...suite.beforeEach],
558
+ afterEach: [...suite.afterEach, ...parentHooks.afterEach],
559
+ }, childPath);
560
+ }
561
+
562
+ // Run afterAll hooks
563
+ for (const hook of suite.afterAll) {
564
+ await hook();
565
+ }
566
+ }
567
+
568
+ await runSuite(rootSuite, { beforeEach: [], afterEach: [] }, '');
569
+
570
+ const passed = results.filter(r => r.passed && !r.skipped).length;
571
+ const failed = results.filter(r => !r.passed).length;
572
+ const skipped = results.filter(r => r.skipped).length;
573
+
574
+ return JSON.stringify({
575
+ passed,
576
+ failed,
577
+ skipped,
578
+ total: results.length,
579
+ results,
580
+ });
581
+ }
582
+
583
+ // Reset function to clear state between runs
584
+ function __resetTestEnvironment() {
585
+ rootSuite.tests = [];
586
+ rootSuite.children = [];
587
+ rootSuite.beforeAll = [];
588
+ rootSuite.afterAll = [];
589
+ rootSuite.beforeEach = [];
590
+ rootSuite.afterEach = [];
591
+ currentSuite = rootSuite;
592
+ suiteStack.length = 0;
593
+ suiteStack.push(rootSuite);
594
+ }
595
+
596
+ // ============================================================
597
+ // Expose Globals
598
+ // ============================================================
599
+
600
+ globalThis.describe = describe;
601
+ globalThis.test = test;
602
+ globalThis.it = it;
603
+ globalThis.expect = expect;
604
+ globalThis.beforeEach = beforeEach;
605
+ globalThis.afterEach = afterEach;
606
+ globalThis.beforeAll = beforeAll;
607
+ globalThis.afterAll = afterAll;
608
+ globalThis.__runAllTests = __runAllTests;
609
+ globalThis.__resetTestEnvironment = __resetTestEnvironment;
610
+ })();
611
+ `;
612
+
613
+ /**
614
+ * Setup test environment primitives in an isolated-vm context
615
+ *
616
+ * Provides Jest/Vitest-compatible test primitives:
617
+ * - describe, test, it
618
+ * - beforeEach, afterEach, beforeAll, afterAll
619
+ * - expect matchers
620
+ *
621
+ * @example
622
+ * const handle = await setupTestEnvironment(context);
623
+ *
624
+ * await context.eval(`
625
+ * describe("my tests", () => {
626
+ * test("example", () => {
627
+ * expect(1 + 1).toBe(2);
628
+ * });
629
+ * });
630
+ * `);
631
+ */
632
+ export async function setupTestEnvironment(
633
+ context: ivm.Context
634
+ ): Promise<TestEnvironmentHandle> {
635
+ context.evalSync(testEnvironmentCode);
636
+
637
+ return {
638
+ dispose() {
639
+ // Reset the test environment state
640
+ try {
641
+ context.evalSync("__resetTestEnvironment()");
642
+ } catch {
643
+ // Context may already be released
644
+ }
645
+ },
646
+ };
647
+ }
648
+
649
+ /**
650
+ * Run tests in the context and return results
651
+ */
652
+ export async function runTests(context: ivm.Context): Promise<TestResults> {
653
+ const resultJson = await context.eval("__runAllTests()", { promise: true });
654
+ return JSON.parse(resultJson as string);
655
+ }