Skip to content

Commit 0cab928

Browse files
committed
test: Add shared.ts tests
1 parent ad92b95 commit 0cab928

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

constraints.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ gen_enforced_field(WorkspaceCwd, 'scripts.changelog:update', CorrectChangelogUpd
301301
% All non-root packages must have the same "test" script.
302302
gen_enforced_field(WorkspaceCwd, 'scripts.test', 'jest --reporters=jest-silent-reporter') :-
303303
WorkspaceCwd \= 'packages/extension',
304+
WorkspaceCwd \= 'packages/shims',
304305
WorkspaceCwd \= '.'.
305306

306307
% All non-root packages must have the same "test:clean" script.

packages/extension/src/shared.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { vi, describe, it, expect } from 'vitest';
2+
3+
import { isWrappedIframeMessage, makeHandledCallback } from './shared';
4+
5+
describe('shared', () => {
6+
describe('isWrappedIframeMessage', () => {
7+
it('returns true for valid messages', () => {
8+
expect(
9+
isWrappedIframeMessage({
10+
id: '1',
11+
message: { type: 'evaluate', data: '1 + 1' },
12+
}),
13+
).toBe(true);
14+
});
15+
16+
it('returns false for invalid messages', () => {
17+
const invalidMessages = [
18+
{},
19+
{ id: '1' },
20+
{ message: { type: 'evaluate' } },
21+
{ id: '1', message: { type: 'evaluate' } },
22+
{ id: '1', message: { type: 'evaluate', data: 1 } },
23+
];
24+
25+
invalidMessages.forEach((message) => {
26+
expect(isWrappedIframeMessage(message)).toBe(false);
27+
});
28+
});
29+
});
30+
31+
describe('makeHandledCallback', () => {
32+
it('returns a function', () => {
33+
const callback = makeHandledCallback(async () => Promise.resolve());
34+
expect(callback).toBeInstanceOf(Function);
35+
});
36+
37+
it('calls the original callback', () => {
38+
const originalCallback = vi.fn().mockResolvedValueOnce(undefined);
39+
const callback = makeHandledCallback(originalCallback);
40+
41+
// eslint-disable-next-line n/callback-return
42+
callback();
43+
44+
expect(originalCallback).toHaveBeenCalledOnce();
45+
});
46+
47+
it('throws an error if the original callback throws an error', async () => {
48+
const consoleErrorSpy = vi.spyOn(console, 'error');
49+
const error = new Error('test error');
50+
const originalCallback = vi.fn().mockRejectedValueOnce(error);
51+
const callback = makeHandledCallback(originalCallback);
52+
53+
// eslint-disable-next-line n/callback-return
54+
callback();
55+
// eslint-disable-next-line @typescript-eslint/await-thenable
56+
await null;
57+
58+
expect(consoleErrorSpy).toHaveBeenCalledOnce();
59+
expect(consoleErrorSpy).toHaveBeenCalledWith(
60+
expect.objectContaining({ message: error.message }),
61+
);
62+
});
63+
});
64+
});

packages/extension/src/shared.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ export const makeHandledCallback = <Args extends unknown[]>(
4646
) => {
4747
return (...args: Args): void => {
4848
// eslint-disable-next-line n/no-callback-literal, n/callback-return
49-
callback(...args).catch((error: Error) => {
50-
throw error;
51-
});
49+
callback(...args).catch(console.error);
5250
};
5351
};

packages/shims/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"changelog:validate": "../../scripts/validate-changelog.sh @ocap/shims",
2626
"clean": "rimraf ./dist",
2727
"publish:preview": "yarn npm publish --tag preview",
28-
"test": "jest --reporters=jest-silent-reporter",
28+
"test": "jest --reporters=jest-silent-reporter --passWithNoTests",
2929
"test:clean": "jest --clearCache",
3030
"test:dev": "jest --verbose --coverage false",
3131
"test:verbose": "jest --verbose",

0 commit comments

Comments
 (0)