Add evaluate
This commit is contained in:
39
packages/server/test/evaluate.test.ts
Normal file
39
packages/server/test/evaluate.test.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import { createClient } from "./helpers";
|
||||
|
||||
describe("Evaluate", () => {
|
||||
const client = createClient();
|
||||
|
||||
it("should transfer string", async () => {
|
||||
const value = await client.evaluate(function () {
|
||||
return "hi";
|
||||
});
|
||||
|
||||
expect(value).toEqual("hi");
|
||||
}, 100);
|
||||
|
||||
it("should compute from object", async () => {
|
||||
const value = await client.evaluate((arg) => {
|
||||
return arg.bananas * 2;
|
||||
}, { bananas: 1 });
|
||||
|
||||
expect(value).toEqual(2);
|
||||
}, 100);
|
||||
|
||||
it("should transfer object", async () => {
|
||||
const value = await client.evaluate(() => {
|
||||
return { alpha: "beta" };
|
||||
});
|
||||
|
||||
expect(value.alpha).toEqual("beta");
|
||||
}, 100);
|
||||
|
||||
it("should require", async () => {
|
||||
const value = await client.evaluate(() => {
|
||||
const fs = require("fs") as typeof import("fs");
|
||||
|
||||
return Object.keys(fs).filter((f) => f === "readFileSync");
|
||||
});
|
||||
|
||||
expect(value[0]).toEqual("readFileSync");
|
||||
}, 100);
|
||||
});
|
28
packages/server/test/helpers.ts
Normal file
28
packages/server/test/helpers.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Emitter } from "@coder/events";
|
||||
import { Client } from "../src/browser/client";
|
||||
import { Server } from "../src/node/server";
|
||||
|
||||
export const createClient = (): Client => {
|
||||
const s2c = new Emitter<Uint8Array | Buffer>();
|
||||
const c2s = new Emitter<Uint8Array | Buffer>();
|
||||
|
||||
new Server({
|
||||
close: () => undefined,
|
||||
onClose: () => undefined,
|
||||
onMessage: (cb) => {
|
||||
c2s.event((d) => cb(d));
|
||||
},
|
||||
send: (data) => setTimeout(() => s2c.emit(data), 0),
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
close: () => undefined,
|
||||
onClose: () => undefined,
|
||||
onMessage: (cb) => {
|
||||
s2c.event((d) => cb(d));
|
||||
},
|
||||
send: (data) => setTimeout(() => c2s.emit(data), 0),
|
||||
});
|
||||
|
||||
return client;
|
||||
};
|
Reference in New Issue
Block a user