Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 110 additions & 45 deletions packages/bruno-cli/src/runner/run-single-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,55 +170,80 @@ const runSingleRequest = async function (
const collectionName = collection?.brunoConfig?.name
if (requestScriptFile?.length) {
const scriptRuntime = new ScriptRuntime({ runtime: scriptingConfig?.runtime });
const result = await scriptRuntime.runRequestScript(
decomment(requestScriptFile),
request,
envVariables,
runtimeVariables,
collectionPath,
onConsoleLog,
processEnvVars,
scriptingConfig,
runSingleRequestByPathname,
collectionName
);
if (result?.nextRequestName !== undefined) {
nextRequestName = result.nextRequestName;
}
try {
const result = await scriptRuntime.runRequestScript(decomment(requestScriptFile),
request,
envVariables,
runtimeVariables,
collectionPath,
onConsoleLog,
processEnvVars,
scriptingConfig,
runSingleRequestByPathname,
collectionName);
if (result?.nextRequestName !== undefined) {
nextRequestName = result.nextRequestName;
}

if (result?.stopExecution) {
shouldStopRunnerExecution = true;
}
if (result?.stopExecution) {
shouldStopRunnerExecution = true;
}

if (result?.skipRequest) {
return {
test: {
filename: relativeItemPathname
},
request: {
method: request.method,
url: request.url,
headers: request.headers,
data: request.data
},
response: {
if (result?.skipRequest) {
return {
test: {
filename: relativeItemPathname
},
request: {
method: request.method,
url: request.url,
headers: request.headers,
data: request.data
},
response: {
status: 'skipped',
statusText: 'request skipped via pre-request script',
data: null,
responseTime: 0
},
error: null,
status: 'skipped',
statusText: 'request skipped via pre-request script',
data: null,
responseTime: 0
},
error: null,
status: 'skipped',
skipped: true,
assertionResults: [],
testResults: [],
preRequestTestResults: result?.results || [],
postResponseTestResults: [],
shouldStopRunnerExecution
};
}
skipped: true,
assertionResults: [],
testResults: [],
preRequestTestResults: result?.results || [],
postResponseTestResults: [],
shouldStopRunnerExecution
};
}

preRequestTestResults = result?.results || [];
} catch (error) {
console.error('Pre-request script execution error:', error);

// Extract partial results from the error (tests that passed before the error)
const partialResults = error?.partialResults?.results || [];
preRequestTestResults = [
...partialResults,
{
status: 'fail',
description: 'Pre-Request Script Error',
error: error.message || 'An error occurred while executing the pre-request script.'
}
];

preRequestTestResults = result?.results || [];
// Preserve nextRequestName if it was set before the error
if (error?.partialResults?.nextRequestName !== undefined) {
nextRequestName = error.partialResults.nextRequestName;
}

// Preserve stopExecution if it was set before the error
if (error?.partialResults?.stopExecution) {
shouldStopRunnerExecution = true;
}

logResults(preRequestTestResults, 'Pre-Request Tests');
}
}

// interpolate variables inside request
Expand Down Expand Up @@ -638,6 +663,26 @@ const runSingleRequest = async function (
logResults(postResponseTestResults, 'Post-Response Tests');
} catch (error) {
console.error('Post-response script execution error:', error);

const partialResults = error?.partialResults?.results || [];
postResponseTestResults = [
...partialResults,
{
status: 'fail',
description: 'Post-Response Script Error',
error: error.message || 'An error occurred while executing the post-response script.'
}
];

if (error?.partialResults?.nextRequestName !== undefined) {
nextRequestName = error.partialResults.nextRequestName;
}

if (error?.partialResults?.stopExecution) {
shouldStopRunnerExecution = true;
}

logResults(postResponseTestResults, 'Post-Response Tests');
}
}

Expand Down Expand Up @@ -687,6 +732,26 @@ const runSingleRequest = async function (
logResults(testResults, 'Tests');
} catch (error) {
console.error('Test script execution error:', error);

const partialResults = error?.partialResults?.results || [];
testResults = [
...partialResults,
{
status: 'fail',
description: 'Test Script Error',
error: error.message || 'An error occurred while executing the test script.'
}
];

if (error?.partialResults?.nextRequestName !== undefined) {
nextRequestName = error.partialResults.nextRequestName;
}

if (error?.partialResults?.stopExecution) {
shouldStopRunnerExecution = true;
}

logResults(testResults, 'Tests');
}
}

Expand Down
65 changes: 65 additions & 0 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,38 @@ const registerNetworkIpc = (mainWindow) => {
});
};

const appendScriptErrorResult = (scriptType, scriptResult, error) => {
if (!error) {
return scriptResult;
}

const descriptionMap = {
'test': 'Test Script Error',
'post-response': 'Post-Response Script Error',
'pre-request': 'Pre-Request Script Error'
};

const messageMap = {
'test': 'An error occurred while executing the test script.',
'post-response': 'An error occurred while executing the post-response script.',
'pre-request': 'An error occurred while executing the pre-request script.'
};

const results = [
...(scriptResult?.results || []),
{
status: 'fail',
description: descriptionMap[scriptType] || 'Script Error',
error: error.message || messageMap[scriptType] || 'An error occurred while executing the script.'
}
];

return {
...(scriptResult || {}),
results
};
};

const runPreRequest = async (
request,
requestUid,
Expand Down Expand Up @@ -658,6 +690,12 @@ const registerNetworkIpc = (mainWindow) => {
preRequestError = error;
}

if (preRequestError?.partialResults) {
preRequestScriptResult = preRequestError.partialResults;
}

preRequestScriptResult = appendScriptErrorResult('pre-request', preRequestScriptResult, preRequestError);

if (preRequestScriptResult?.results) {
mainWindow.webContents.send('main:run-request-event', {
type: 'test-results-pre-request',
Expand Down Expand Up @@ -823,6 +861,15 @@ const registerNetworkIpc = (mainWindow) => {
postResponseError = error;
}

// Extract partial results from error if available
// This preserves any test() calls that passed before the script errored
// (e.g., if 2 tests pass then script throws, we still want to show those 2 passing tests)
if (postResponseError?.partialResults) {
postResponseScriptResult = postResponseError.partialResults;
}

postResponseScriptResult = appendScriptErrorResult('post-response', postResponseScriptResult, postResponseError);

if (postResponseScriptResult?.results) {
mainWindow.webContents.send('main:run-request-event', {
type: 'test-results-post-response',
Expand Down Expand Up @@ -896,6 +943,8 @@ const registerNetworkIpc = (mainWindow) => {
}
}

testResults = appendScriptErrorResult('test', testResults, testError);

!runInBackground && mainWindow.webContents.send('main:run-request-event', {
type: 'test-results',
results: testResults.results,
Expand Down Expand Up @@ -1239,6 +1288,12 @@ const registerNetworkIpc = (mainWindow) => {
preRequestError = error;
}

if (preRequestError?.partialResults) {
preRequestScriptResult = preRequestError.partialResults;
}

preRequestScriptResult = appendScriptErrorResult('pre-request', preRequestScriptResult, preRequestError);

if (preRequestScriptResult?.results) {
mainWindow.webContents.send('main:run-folder-event', {
type: 'test-results-pre-request',
Expand Down Expand Up @@ -1461,6 +1516,14 @@ const registerNetworkIpc = (mainWindow) => {
postResponseError = error;
}

// Extract partial results from error if available
// (e.g., if 2 tests pass then script throws, we still want to show those 2 passing tests)
if (postResponseError?.partialResults) {
postResponseScriptResult = postResponseError.partialResults;
}

postResponseScriptResult = appendScriptErrorResult('post-response', postResponseScriptResult, postResponseError);

notifyScriptExecution({
channel: 'main:run-folder-event',
basePayload: eventData,
Expand Down Expand Up @@ -1547,6 +1610,8 @@ const registerNetworkIpc = (mainWindow) => {
}
}

testResults = appendScriptErrorResult('test', testResults, testError);

if (testResults?.nextRequestName !== undefined) {
nextRequestName = testResults.nextRequestName;
}
Expand Down
Loading