Skip to content
File

Blob: src/workerd/api/tests/reporterror-test.js

javascript46 lines
1// Copyright (c) 2024 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4import { mock } from 'node:test';
5import { strictEqual, throws } from 'node:assert';
6 
7const boom = new Error('boom');
8 
9const handler = mock.fn((event) => {
10 if (event.error instanceof Error) {
11 strictEqual(event.message, 'Uncaught Error: boom');
12 strictEqual(event.colno, 13);
13 strictEqual(event.lineno, 7);
14 strictEqual(event.filename, 'worker');
15 strictEqual(event.error, boom);
16 } else {
17 strictEqual(event.message, 'Uncaught boom');
18 strictEqual(event.colno, 0);
19 strictEqual(event.lineno, 28);
20 strictEqual(event.filename, 'worker');
21 strictEqual(event.error, 'boom');
22 }
23 return true;
24});
25 
26addEventListener('error', handler);
27 
28reportError('boom');
29 
30throws(() => reportError(), {
31 message:
32 "Failed to execute 'reportError' on 'ServiceWorkerGlobalScope': " +
33 "parameter 1 is not of type 'JsValue'.",
34});
35 
36export const reportErrorTest = {
37 test() {
38 // TODO(soon): We are limited in what we can test here because we cannot
39 // inspect the log output and workerd does not implement that WorkerTracer
40 // used for collecting data for tail workers. The best we can currently do
41 // is make sure the basic API is working and that the mock fn was called.
42 reportError(boom);
43 strictEqual(handler.mock.calls.length, 2);
44 },
45};