Using node vm for string-template test cases.

This commit is contained in:
mike12345567 2025-01-23 17:23:08 +00:00
parent a3605922ee
commit afb9f86bf7
1 changed files with 8 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import vm from "@budibase/vm-browserify"
import browserVM from "@budibase/vm-browserify"
import vm from "vm"
import { create, TemplateDelegate } from "handlebars"
import { registerAll, registerMinimum } from "./helpers/index"
import { postprocess, postprocessWithLogs, preprocess } from "./processors"
@ -14,10 +15,10 @@ import {
} from "./utilities"
import { convertHBSBlock } from "./conversion"
import { removeJSRunner, setJSRunner } from "./helpers/javascript"
import manifest from "./manifest.json"
import { Log, ProcessOptions } from "./types"
import { UserScriptError } from "./errors"
import { isTest } from "./environment"
export type { Log, LogType } from "./types"
export { setTestingBackendJS } from "./environment"
@ -507,15 +508,15 @@ export function convertToJS(hbs: string) {
export { JsTimeoutError, UserScriptError } from "./errors"
export function browserJSSetup() {
/**
* Use polyfilled vm to run JS scripts in a browser Env
*/
// tests are in jest - we need to use node VM for these
const jsSandbox = isTest() ? vm : browserVM
// Use polyfilled vm to run JS scripts in a browser Env
setJSRunner((js: string, context: Record<string, any>) => {
vm.createContext(context)
jsSandbox.createContext(context)
const wrappedJs = frontendWrapJS(js)
const result = vm.runInNewContext(wrappedJs, context)
const result = jsSandbox.runInNewContext(wrappedJs, context)
if (result.error) {
throw new UserScriptError(result.error)
}