|
| 1 | +import { preventNativeAwait } from './proxy' |
| 2 | + |
| 3 | +describe('preventNativeAwait', () => { |
| 4 | + it('should work normally if not awaited', () => { |
| 5 | + const obj = { value: 42 } |
| 6 | + const proxy = preventNativeAwait(obj) |
| 7 | + expect(proxy).toEqual(obj) |
| 8 | + |
| 9 | + const obj2 = { then: 323, catch: 123 } |
| 10 | + const proxy2 = preventNativeAwait(obj2) |
| 11 | + expect(proxy2).toEqual(obj2) |
| 12 | + |
| 13 | + const obj3 = { then: (...args: any[]) => ({ args }), catch: (...args: any[]) => ({ args }) } |
| 14 | + const proxy3 = preventNativeAwait(obj3) |
| 15 | + expect(proxy3.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 16 | + expect(proxy3.catch(4, 5, 6)).toEqual({ args: [4, 5, 6] }) |
| 17 | + }) |
| 18 | + |
| 19 | + it('returns itself if awaited', async () => { |
| 20 | + const obj = { value: 42 } |
| 21 | + const proxy = preventNativeAwait(obj) |
| 22 | + expect(await proxy).toEqual(obj) |
| 23 | + |
| 24 | + const obj2 = { then: 323, catch: 123 } |
| 25 | + const proxy2 = preventNativeAwait(obj2) |
| 26 | + expect(await proxy2).toEqual(obj2) |
| 27 | + expect(await proxy2).toEqual(obj2) |
| 28 | + |
| 29 | + const obj3 = { then: (...args: any[]) => ({ args }), catch: (...args: any[]) => ({ args }) } |
| 30 | + const proxy3 = preventNativeAwait(obj3) |
| 31 | + const result3 = await (proxy3 as any) |
| 32 | + expect(result3.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 33 | + expect(result3.catch(3, 4, 5)).toEqual({ args: [3, 4, 5] }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('returns itself if multiple awaited times', async () => { |
| 37 | + const obj = { value: 42 } |
| 38 | + const proxy = preventNativeAwait(obj) |
| 39 | + const result = await (proxy as any) |
| 40 | + expect(result).toEqual(obj) |
| 41 | + await new Promise(resolve => setTimeout(resolve, 1)) |
| 42 | + expect(await proxy).toEqual(obj) |
| 43 | + |
| 44 | + const obj2 = { then: 323, catch: 123 } |
| 45 | + const proxy2 = preventNativeAwait(obj2) |
| 46 | + const result2 = await (proxy2 as any) |
| 47 | + expect(result2).toEqual(obj2) |
| 48 | + await new Promise(resolve => setTimeout(resolve, 1)) |
| 49 | + expect(await proxy2).toEqual(obj2) |
| 50 | + |
| 51 | + const obj3 = { then: (...args: any[]) => ({ args }), catch: (...args: any[]) => ({ args }) } |
| 52 | + const proxy3 = preventNativeAwait(obj3) |
| 53 | + const result3 = await (proxy3 as any) |
| 54 | + expect(result3.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 55 | + expect(result3.catch(4, 5, 6)).toEqual({ args: [4, 5, 6] }) |
| 56 | + await new Promise(resolve => setTimeout(resolve, 1)) |
| 57 | + const result3_2 = await (proxy3 as any) |
| 58 | + expect(result3_2.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 59 | + expect(result3_2.catch(4, 5, 6)).toEqual({ args: [4, 5, 6] }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('returns itself if nested awaited times', async () => { |
| 63 | + const obj = { value: 42 } |
| 64 | + const proxy = preventNativeAwait(obj) |
| 65 | + const result = await (proxy as any) |
| 66 | + expect(result).toEqual(obj) |
| 67 | + await new Promise(resolve => setTimeout(resolve, 1)) |
| 68 | + expect(await result).toEqual(obj) |
| 69 | + |
| 70 | + const obj2 = { then: 323, catch: 123 } |
| 71 | + const proxy2 = preventNativeAwait(obj2) |
| 72 | + const result2 = await (proxy2 as any) |
| 73 | + expect(result2).toEqual(obj2) |
| 74 | + await new Promise(resolve => setTimeout(resolve, 1)) |
| 75 | + expect(await result2).toEqual(obj2) |
| 76 | + |
| 77 | + const obj3 = { then: (...args: any[]) => ({ args }), catch: (...args: any[]) => ({ args }) } |
| 78 | + const proxy3 = preventNativeAwait(obj3) |
| 79 | + const result3 = await (proxy3 as any) |
| 80 | + expect(result3.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 81 | + expect(result3.catch(4, 5, 6)).toEqual({ args: [4, 5, 6] }) |
| 82 | + await new Promise(resolve => setTimeout(resolve, 1)) |
| 83 | + const result3_2 = await (result3 as any) |
| 84 | + expect(result3_2.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 85 | + expect(result3_2.catch(4, 5, 6)).toEqual({ args: [4, 5, 6] }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('resolves via Promise.resolve without triggering thenable assimilation', async () => { |
| 89 | + const obj = { value: 42 } |
| 90 | + const proxy = preventNativeAwait(obj) |
| 91 | + await expect(Promise.resolve(proxy)).resolves.toEqual(obj) |
| 92 | + |
| 93 | + const obj2 = { then: 123 } |
| 94 | + const proxy2 = preventNativeAwait(obj2) |
| 95 | + await expect(Promise.resolve(proxy2)).resolves.toEqual(obj2) |
| 96 | + |
| 97 | + const obj3 = { then: (...args: any[]) => ({ args }) } |
| 98 | + const proxy3 = preventNativeAwait(obj3) |
| 99 | + const resolved = await Promise.resolve(proxy3 as any) |
| 100 | + expect(resolved.then(1, 2, 3)).toEqual({ args: [1, 2, 3] }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments