Created
October 19, 2020 17:24
-
-
Save sibelius/ffa492c5b08eb11d1d818374150a3295 to your computer and use it in GitHub Desktop.
Using deferred promise to test websockets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const createDeferred = () => { | |
| let d = {}; | |
| const promise = new Promise((resolve, reject) => { | |
| d = { resolve, reject }; | |
| }); | |
| return { ...d, promise } as { | |
| resolve: () => void; | |
| reject: (err: unknown) => void; | |
| promise: Promise<void>; | |
| }; | |
| }; | |
| await makeServer(); | |
| const d = createDeferred() | |
| let client = new WebSocket(url); | |
| client.onclose = (event) => { | |
| expect(event.code).toBe(1002); | |
| expect(event.reason).toBe('Protocol Error'); | |
| expect(event.wasClean).toBeTruthy(); | |
| d.resolve() | |
| }; | |
| await d.promise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another options: