|
| 1 | +import { type Mocked, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { copyExecutionContext } from '../src/utils/copyExecutionContext'; |
| 3 | + |
| 4 | +describe('Copy of the execution context', () => { |
| 5 | + describe.for([ |
| 6 | + 'waitUntil', |
| 7 | + 'passThroughOnException', |
| 8 | + 'acceptWebSocket', |
| 9 | + 'blockConcurrencyWhile', |
| 10 | + 'getWebSockets', |
| 11 | + 'arbitraryMethod', |
| 12 | + 'anythingElse', |
| 13 | + ])('%s', method => { |
| 14 | + it('Override without changing original', async () => { |
| 15 | + const context = { |
| 16 | + [method]: vi.fn(), |
| 17 | + } as any; |
| 18 | + const copy = copyExecutionContext(context); |
| 19 | + copy[method] = vi.fn(); |
| 20 | + expect(context[method]).not.toBe(copy[method]); |
| 21 | + }); |
| 22 | + |
| 23 | + it('Overridden method was called', async () => { |
| 24 | + const context = { |
| 25 | + [method]: vi.fn(), |
| 26 | + } as any; |
| 27 | + const copy = copyExecutionContext(context); |
| 28 | + const overridden = vi.fn(); |
| 29 | + copy[method] = overridden; |
| 30 | + copy[method](); |
| 31 | + expect(overridden).toBeCalled(); |
| 32 | + expect(context[method]).not.toBeCalled(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('No side effects', async () => { |
| 37 | + const context = makeExecutionContextMock(); |
| 38 | + expect(() => copyExecutionContext(Object.freeze(context))).not.toThrow( |
| 39 | + /Cannot define property \w+, object is not extensible/, |
| 40 | + ); |
| 41 | + }); |
| 42 | + it('Respects symbols', async () => { |
| 43 | + const s = Symbol('test'); |
| 44 | + const context = makeExecutionContextMock<ExecutionContext & { [s]: unknown }>(); |
| 45 | + context[s] = {}; |
| 46 | + const copy = copyExecutionContext(context); |
| 47 | + expect(copy[s]).toBe(context[s]); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +function makeExecutionContextMock<T extends ExecutionContext>() { |
| 52 | + return { |
| 53 | + waitUntil: vi.fn(), |
| 54 | + passThroughOnException: vi.fn(), |
| 55 | + } as unknown as Mocked<T>; |
| 56 | +} |
0 commit comments