UNPKG

1.39 kBPlain TextView Raw
1import { assert, Has, NotHas, IsExact } from "conditional-type-checks";
2
3import * as Comlink from "../src/comlink.js";
4
5async function test1() {
6 function simpleNumberFunction() {
7 return 4;
8 }
9
10 const proxy = Comlink.wrap<typeof simpleNumberFunction>(0 as any);
11 const v = proxy();
12 assert<IsExact<typeof v, Promise<number>>>(true);
13}
14
15async function test2() {
16 function simpleObjectFunction() {
17 return { a: 3 };
18 }
19
20 const proxy = Comlink.wrap<typeof simpleObjectFunction>(0 as any);
21 const v = await proxy();
22 assert<IsExact<typeof v, { a: number }>>(true);
23}
24
25async function test3() {
26 function functionWithProxy() {
27 return Comlink.proxy({ a: 3 });
28 }
29
30 const proxy = Comlink.wrap<typeof functionWithProxy>(0 as any);
31 const subproxy = await proxy();
32 const prop = subproxy.a;
33 assert<IsExact<typeof prop, Promise<number>>>(true);
34}
35
36async function test4() {
37 class X {
38 static staticFunc() {
39 return 4;
40 }
41 private f = 4;
42 public g = 9;
43 sayHi() {
44 return "hi";
45 }
46 }
47
48 const proxy = Comlink.wrap<typeof X>(0 as any);
49 assert<Has<typeof proxy, { staticFunc: () => Promise<number> }>>(true);
50 const instance = await new proxy();
51 assert<Has<typeof instance, { sayHi: () => Promise<string> }>>(true);
52 assert<Has<typeof instance, { g: Promise<number> }>>(true);
53 assert<NotHas<typeof instance, { f: Promise<number> }>>(true);
54}