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