From 8ed940ac09f19c1d37da2cf190a2a9af20515b69 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Fri, 11 Jan 2019 13:46:00 -0600 Subject: [PATCH] Add test for promises --- packages/server/src/node/evaluate.ts | 8 ++------ packages/server/test/evaluate.test.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/server/src/node/evaluate.ts b/packages/server/src/node/evaluate.ts index 73de9cb1..540c4ba1 100644 --- a/packages/server/src/node/evaluate.ts +++ b/packages/server/src/node/evaluate.ts @@ -51,14 +51,10 @@ export const evaluate = async (connection: SendableConnection, message: NewEvalM connection.send(serverMsg.serializeBinary()); }; try { - const value = vm.runInNewContext(`(${message.getFunction()})(${argStr.join(",")})`, { require }, { + const value = vm.runInNewContext(`(${message.getFunction()})(${argStr.join(",")})`, { require, setTimeout }, { timeout: message.getTimeout() || 30000, }); - let responder: any = value; - if (value instanceof Promise) { - responder = await value; - } - sendResp(responder); + sendResp(await value); } catch (ex) { sendErr(EvalFailedMessage.Reason.EXCEPTION, ex.toString()); } diff --git a/packages/server/test/evaluate.test.ts b/packages/server/test/evaluate.test.ts index b7755a31..384b6025 100644 --- a/packages/server/test/evaluate.test.ts +++ b/packages/server/test/evaluate.test.ts @@ -36,4 +36,14 @@ describe("Evaluate", () => { expect(value[0]).toEqual("readFileSync"); }, 100); + + it("should resolve with promise", async () => { + const value = await client.evaluate(async () => { + await new Promise((r) => setTimeout(r, 100)); + + return "donkey"; + }); + + expect(value).toEqual("donkey"); + }, 250); }); \ No newline at end of file