1
0
Fork 0
mirror of synced 2024-07-15 11:15:59 +12:00

Fix javacript runs

This commit is contained in:
Adria Navarro 2024-02-09 10:33:09 +01:00
parent 2cde033783
commit 7aee57eb6a

View file

@ -1,11 +1,31 @@
const { processStringSync, encodeJSBinding } = require("../src/index.js") const vm = require("vm")
const {
processStringSync,
encodeJSBinding,
setJSRunner,
} = require("../src/index.js")
const { UUID_REGEX } = require("./constants") const { UUID_REGEX } = require("./constants")
const processJS = (js, context) => { const processJS = (js, context) => {
return processStringSync(encodeJSBinding(js), context) return processStringSync(encodeJSBinding(js), context)
} }
describe("Test the JavaScript helper", () => { describe("Javascript", () => {
beforeAll(() => {
setJSRunner((js, context) => {
context = {
...context,
alert: undefined,
setInterval: undefined,
setTimeout: undefined,
}
vm.createContext(context)
return vm.runInNewContext(js, context, { timeout: 1000 })
})
})
describe("Test the JavaScript helper", () => {
it("should execute a simple expression", () => { it("should execute a simple expression", () => {
const output = processJS(`return 1 + 2`) const output = processJS(`return 1 + 2`)
expect(output).toBe(3) expect(output).toBe(3)
@ -129,9 +149,9 @@ describe("Test the JavaScript helper", () => {
) )
expect(output).toBe("Error while executing JS") expect(output).toBe("Error while executing JS")
}) })
}) })
describe("check JS helpers", () => { describe("check JS helpers", () => {
it("should error if using the format helper. not helpers.", () => { it("should error if using the format helper. not helpers.", () => {
const output = processJS(`return helper.toInt(4.3)`) const output = processJS(`return helper.toInt(4.3)`)
expect(output).toBe("Error while executing JS") expect(output).toBe("Error while executing JS")
@ -146,4 +166,5 @@ describe("check JS helpers", () => {
const output = processJS(`return helpers.uuid()`) const output = processJS(`return helpers.uuid()`)
expect(output).toMatch(UUID_REGEX) expect(output).toMatch(UUID_REGEX)
}) })
})
}) })