Fix javacript runs
This commit is contained in:
parent
2cde033783
commit
7aee57eb6a
|
@ -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 processJS = (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", () => {
|
||||
const output = processJS(`return 1 + 2`)
|
||||
expect(output).toBe(3)
|
||||
|
@ -129,9 +149,9 @@ describe("Test the JavaScript helper", () => {
|
|||
)
|
||||
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.", () => {
|
||||
const output = processJS(`return helper.toInt(4.3)`)
|
||||
expect(output).toBe("Error while executing JS")
|
||||
|
@ -146,4 +166,5 @@ describe("check JS helpers", () => {
|
|||
const output = processJS(`return helpers.uuid()`)
|
||||
expect(output).toMatch(UUID_REGEX)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue