diff --git a/packages/bruno-app/src/components/RunnerResults/index.jsx b/packages/bruno-app/src/components/RunnerResults/index.jsx index b35d366a6..cbf099e5b 100644 --- a/packages/bruno-app/src/components/RunnerResults/index.jsx +++ b/packages/bruno-app/src/components/RunnerResults/index.jsx @@ -27,10 +27,7 @@ const allTestsPassed = (item) => { item.testStatus === 'pass' && item.assertionStatus === 'pass' && item.preRequestTestStatus === 'pass' && - item.postResponseTestStatus === 'pass' && - !item.preRequestScriptErrorMessage && - !item.postResponseScriptErrorMessage && - !item.testScriptErrorMessage; + item.postResponseTestStatus === 'pass'; }; const anyTestFailed = (item) => { @@ -38,10 +35,7 @@ const anyTestFailed = (item) => { item.testStatus === 'fail' || item.assertionStatus === 'fail' || item.preRequestTestStatus === 'fail' || - item.postResponseTestStatus === 'fail' || - item.preRequestScriptErrorMessage || - item.postResponseScriptErrorMessage || - item.testScriptErrorMessage; + item.postResponseTestStatus === 'fail'; }; export default function RunnerResults({ collection }) { diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 50aaf823b..8cd54206e 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -495,29 +495,33 @@ const runSingleRequest = async function ( const responseScriptFile = get(request, 'script.res'); if (responseScriptFile?.length) { const scriptRuntime = new ScriptRuntime({ runtime: scriptingConfig?.runtime }); - const result = await scriptRuntime.runResponseScript( - decomment(responseScriptFile), - request, - response, - envVariables, - runtimeVariables, - collectionPath, - null, - processEnvVars, - scriptingConfig, - runSingleRequestByPathname, - collectionName - ); - if (result?.nextRequestName !== undefined) { - nextRequestName = result.nextRequestName; - } + try { + const result = await scriptRuntime.runResponseScript( + decomment(responseScriptFile), + request, + response, + envVariables, + runtimeVariables, + collectionPath, + null, + processEnvVars, + scriptingConfig, + runSingleRequestByPathname, + collectionName + ); + if (result?.nextRequestName !== undefined) { + nextRequestName = result.nextRequestName; + } - if (result?.stopExecution) { - shouldStopRunnerExecution = true; - } + if (result?.stopExecution) { + shouldStopRunnerExecution = true; + } - postResponseTestResults = result?.results || []; - logResults(postResponseTestResults, 'Post-Response Tests'); + postResponseTestResults = result?.results || []; + logResults(postResponseTestResults, 'Post-Response Tests'); + } catch (error) { + logResults(error); + } } let assertionResults = []; @@ -539,30 +543,34 @@ const runSingleRequest = async function ( const testFile = get(request, 'tests'); if (typeof testFile === 'string') { const testRuntime = new TestRuntime({ runtime: scriptingConfig?.runtime }); - const result = await testRuntime.runTests( - decomment(testFile), - request, - response, - envVariables, - runtimeVariables, - collectionPath, - null, - processEnvVars, - scriptingConfig, - runSingleRequestByPathname, - collectionName - ); - testResults = get(result, 'results', []); + try { + const result = await testRuntime.runTests( + decomment(testFile), + request, + response, + envVariables, + runtimeVariables, + collectionPath, + null, + processEnvVars, + scriptingConfig, + runSingleRequestByPathname, + collectionName + ); + testResults = get(result, 'results', []); - if (result?.nextRequestName !== undefined) { - nextRequestName = result.nextRequestName; + if (result?.nextRequestName !== undefined) { + nextRequestName = result.nextRequestName; + } + + if (result?.stopExecution) { + shouldStopRunnerExecution = true; + } + + logResults(testResults, 'Tests'); + } catch (error) { + logResults(error); } - - if (result?.stopExecution) { - shouldStopRunnerExecution = true; - } - - logResults(testResults, 'Tests'); } diff --git a/packages/bruno-js/src/runtime/script-runtime.js b/packages/bruno-js/src/runtime/script-runtime.js index a18c94917..3d1b0ec30 100644 --- a/packages/bruno-js/src/runtime/script-runtime.js +++ b/packages/bruno-js/src/runtime/script-runtime.js @@ -260,69 +260,65 @@ class ScriptRuntime { context.bru.runRequest = runRequestByItemPathname; } - if (this.runtime === 'quickjs') { - await executeQuickJsVmAsync({ - script: script, - context: context, - collectionPath - }); + let scriptError = null; - return { - response, - envVariables: cleanJson(envVariables), - runtimeVariables: cleanJson(runtimeVariables), - globalEnvironmentVariables: cleanJson(globalEnvironmentVariables), - results: cleanJson(__brunoTestResults.getResults()), - nextRequestName: bru.nextRequest, - skipRequest: bru.skipRequest, - stopExecution: bru.stopExecution - }; + try { + if (this.runtime === 'quickjs') { + await executeQuickJsVmAsync({ + script: script, + context: context, + collectionPath + }); + } else { + // default runtime is vm2 + const vm = new NodeVM({ + sandbox: context, + require: { + context: 'sandbox', + builtin: [ "*" ], + external: true, + root: [collectionPath, ...additionalContextRootsAbsolute], + mock: { + // node libs + path, + stream, + util, + url, + http, + https, + punycode, + zlib, + // 3rd party libs + ajv, + 'ajv-formats': addFormats, + atob, + btoa, + lodash, + moment, + uuid, + nanoid, + axios, + 'node-fetch': fetch, + 'crypto-js': CryptoJS, + 'xml2js': xml2js, + cheerio, + tv4, + ...whitelistedModules, + fs: allowScriptFilesystemAccess ? fs : undefined, + 'node-vault': NodeVault + } + } + }); + + const asyncVM = vm.run(`module.exports = async () => { ${script} }`, path.join(collectionPath, 'vm.js')); + await asyncVM(); + } + } catch (error) { + scriptError = error; + console.error('Post-response script execution error:', error); } - // default runtime is vm2 - const vm = new NodeVM({ - sandbox: context, - require: { - context: 'sandbox', - builtin: [ "*" ], - external: true, - root: [collectionPath, ...additionalContextRootsAbsolute], - mock: { - // node libs - path, - stream, - util, - url, - http, - https, - punycode, - zlib, - // 3rd party libs - ajv, - 'ajv-formats': addFormats, - atob, - btoa, - lodash, - moment, - uuid, - nanoid, - axios, - 'node-fetch': fetch, - 'crypto-js': CryptoJS, - 'xml2js': xml2js, - cheerio, - tv4, - ...whitelistedModules, - fs: allowScriptFilesystemAccess ? fs : undefined, - 'node-vault': NodeVault - } - } - }); - - const asyncVM = vm.run(`module.exports = async () => { ${script} }`, path.join(collectionPath, 'vm.js')); - await asyncVM(); - - return { + const result = { response, envVariables: cleanJson(envVariables), runtimeVariables: cleanJson(runtimeVariables), @@ -332,6 +328,13 @@ class ScriptRuntime { skipRequest: bru.skipRequest, stopExecution: bru.stopExecution }; + + if (scriptError) { + scriptError.partialResults = result; + throw scriptError; + } + + return result; } }