Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: errors in test scripts #3358

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ const TestResults = ({ results, assertionResults }) => {
<>
<span className="test-failure">&#x2718;&nbsp; {result.description}</span>
<br />
<span className="error-message pl-8">{result.error}</span>
{
Array.isArray(result?.error)?
result?.error?.map(error=><><span className="error-message pl-8">{error}</span><br/></>)
:
<span className="error-message pl-8">{result.error}</span>
}
</>
)}
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
resetRunResults,
responseReceived,
updateLastAction,
setCollectionSecurityConfig
setCollectionSecurityConfig,
responseCleared
} from './index';

import { each } from 'lodash';
Expand Down Expand Up @@ -235,6 +236,16 @@ export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
collectionCopy.globalEnvironmentVariables = globalEnvironmentVariables;

const environment = findEnvironmentInCollection(collectionCopy, collectionCopy.activeEnvironmentUid);

// clear the old response
dispatch(
responseCleared({
itemUid: item.uid,
collectionUid: collectionUid,
response: null
})
);

sendNetworkRequest(itemCopy, collectionCopy, environment, collectionCopy.runtimeVariables)
.then((response) => {
return dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ export const collectionsSlice = createSlice({
const item = findItemInCollection(collection, action.payload.itemUid);
if (item) {
item.response = null;
item.assertionResults = null;
item.testResults = null;
}
}
},
Expand Down
37 changes: 32 additions & 5 deletions packages/bruno-js/src/runtime/test-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions packages/bruno-js/src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const Test = (__brunoTestResults, chai) => async (description, callback) => {
await callback();
__brunoTestResults.addResult({ description, status: 'pass' });
} catch (error) {
console.log(chai.AssertionError);
if (error instanceof chai.AssertionError) {
const { message, actual, expected } = error;
__brunoTestResults.addResult({
Expand All @@ -14,10 +13,17 @@ const Test = (__brunoTestResults, chai) => async (description, callback) => {
expected
});
} else {
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,
status: 'fail',
error: error.message || 'An unexpected error occurred.'
error: [
`Error occurred at line ${lineNumber} and character ${columnNumber}`,
`${error.message || 'An unexpected error occurred.'}`
]
});
}
console.log(error);
Expand Down