From 093e058d0dd1ed71421ae52cad6bd15985d7b094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Fri, 21 Jul 2023 21:37:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20add=20more=20tests=20around=20uncau?= =?UTF-8?q?ght=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/worker/src/boot/startWorker.spec.ts | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/worker/src/boot/startWorker.spec.ts b/packages/worker/src/boot/startWorker.spec.ts index f3db79b058..e6108c7a7e 100644 --- a/packages/worker/src/boot/startWorker.spec.ts +++ b/packages/worker/src/boot/startWorker.spec.ts @@ -170,4 +170,38 @@ describe('startWorker', () => { streamId: undefined, }) }) + + it('reports an error when an unexpected exception occurs while writing on a stream', () => { + if (!window.TextEncoder) { + pending('No TextEncoder support') + } + spyOn(TextEncoder.prototype, 'encode').and.callFake(() => { + throw new Error('Something went wrong!') + }) + expect( + emulateAction({ + id: 2, + streamId: TEST_STREAM_ID, + action: 'write', + data: 'baz', + }) + ).toEqual({ + type: 'errored', + error: new Error('Something went wrong!'), + streamId: TEST_STREAM_ID, + }) + }) + + it('use the string representation of the error when it fails to send it through postMessage', () => { + workerScope.postMessage.and.callFake((response) => { + if (response.type === 'errored' && response.error instanceof Error) { + throw new DOMException("Failed to execute 'postMessage' on 'WorkerScope'") + } + }) + expect(emulateAction(null as any)).toEqual({ + type: 'errored', + error: jasmine.stringContaining('TypeError'), + streamId: undefined, + }) + }) })