diff --git a/packages/bruno-js/src/runtime/test-runtime.js b/packages/bruno-js/src/runtime/test-runtime.js index 5391f7e10..9dd23b03b 100644 --- a/packages/bruno-js/src/runtime/test-runtime.js +++ b/packages/bruno-js/src/runtime/test-runtime.js @@ -114,10 +114,21 @@ class TestRuntime { } if (this.runtime === 'quickjs') { - await executeQuickJsVmAsync({ - script: testsFile, - context: context - }); + try { + await executeQuickJsVmAsync({ + script: testsFile, + context: context + }); + } + catch(error) { + __brunoTestResults.addResult({ + description: 'Invalid test script', + status: 'fail', + error: [ + `${error.message || 'An unexpected error occurred.'}` + ] + }); + } } else { // default runtime is vm2 const vm = new NodeVM({ @@ -156,7 +167,23 @@ class TestRuntime { } }); const asyncVM = vm.run(`module.exports = async () => { ${testsFile}}`, path.join(collectionPath, 'vm.js')); - await asyncVM(); + try { + await asyncVM(); + } + catch(error) { + const errorStackLines = error?.stack?.split?.("\n"); + const lineInfo = errorStackLines?.[1]; + const lineNumber = lineInfo?.split(':')?.at?.(-2); + const columnNumber = lineInfo?.split(':')?.at?.(-1); + __brunoTestResults.addResult({ + description: 'Invalid test script', + status: 'fail', + error: [ + `Error occurred at line ${lineNumber} and character ${columnNumber}`, + `${error.message || 'An unexpected error occurred.'}` + ] + }); + } } return { diff --git a/packages/bruno-js/src/test.js b/packages/bruno-js/src/test.js index e4fe65b69..bc7ef375c 100644 --- a/packages/bruno-js/src/test.js +++ b/packages/bruno-js/src/test.js @@ -13,8 +13,8 @@ const Test = (__brunoTestResults, chai) => async (description, callback) => { expected }); } else { - const errorStackLines = error.stack.split("\n"); - const lineInfo = errorStackLines[1]; + const errorStackLines = error?.stack?.split?.("\n"); + const lineInfo = errorStackLines?.[1]; const lineNumber = lineInfo?.split(':')?.at?.(-2); const columnNumber = lineInfo?.split(':')?.at?.(-1); __brunoTestResults.addResult({