mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-08 22:45:25 +00:00
feat: assert runtime
This commit is contained in:
@@ -4,6 +4,7 @@ const path = require('path');
|
||||
const { exists, isFile } = require('../utils/filesystem');
|
||||
const { runSingleRequest } = require('../runner/run-single-request');
|
||||
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
||||
const { rpad } = require('../utils/common');
|
||||
|
||||
const command = 'run <filename>';
|
||||
const desc = 'Run a request';
|
||||
@@ -55,11 +56,41 @@ const handler = async function (argv) {
|
||||
const _isFile = await isFile(filename);
|
||||
if(_isFile) {
|
||||
console.log(chalk.yellow('Running Request \n'));
|
||||
await runSingleRequest(filename, collectionPath, collectionVariables, envVars);
|
||||
console.log(chalk.green('\nDone!'));
|
||||
const {
|
||||
assertionResults,
|
||||
testResults
|
||||
} = await runSingleRequest(filename, collectionPath, collectionVariables, envVars);
|
||||
|
||||
// display assertion results and test results summary
|
||||
const totalAssertions = assertionResults.length;
|
||||
const passedAssertions = assertionResults.filter((result) => result.status === 'pass').length;
|
||||
const failedAssertions = totalAssertions - passedAssertions;
|
||||
|
||||
const totalTests = testResults.length;
|
||||
const passedTests = testResults.filter((result) => result.status === 'pass').length;
|
||||
const failedTests = totalTests - passedTests;
|
||||
const maxLength = 12;
|
||||
|
||||
let assertSummary = `${rpad('Tests:', maxLength)} ${chalk.green(`${passedTests} passed`)}`;
|
||||
if (failedTests > 0) {
|
||||
assertSummary += `, ${chalk.red(`${failedTests} failed`)}`;
|
||||
}
|
||||
assertSummary += `, ${totalTests} total`;
|
||||
|
||||
let testSummary = `${rpad('Assertions:', maxLength)} ${chalk.green(`${passedAssertions} passed`)}`;
|
||||
if (failedAssertions > 0) {
|
||||
testSummary += `, ${chalk.red(`${failedAssertions} failed`)}`;
|
||||
}
|
||||
testSummary += `, ${totalAssertions} total`;
|
||||
|
||||
console.log("\n" + chalk.bold(assertSummary));
|
||||
console.log(chalk.bold(testSummary));
|
||||
|
||||
console.log(chalk.dim(chalk.grey('Ran all requests.')));
|
||||
}
|
||||
} catch (err) {
|
||||
// console.error(err.message);
|
||||
console.log("Something went wrong");
|
||||
console.error(chalk.red(err.message));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ const FormData = require('form-data');
|
||||
const axios = require('axios');
|
||||
const prepareRequest = require('./prepare-request');
|
||||
const interpolateVars = require('./interpolate-vars');
|
||||
const { ScriptRuntime, TestRuntime, VarsRuntime } = require('@usebruno/js');
|
||||
const { ScriptRuntime, TestRuntime, VarsRuntime, AssertRuntime } = require('@usebruno/js');
|
||||
const { bruToJson } = require('../utils/bru');
|
||||
const { stripExtension } = require('../utils/filesystem');
|
||||
|
||||
@@ -47,6 +47,8 @@ const runSingleRequest = async function (filename, collectionPath, collectionVar
|
||||
// run request
|
||||
const response = await axios(request);
|
||||
|
||||
console.log(chalk.green(stripExtension(filename)) + chalk.dim(` (${response.status} ${response.statusText})`));
|
||||
|
||||
// run post-response vars
|
||||
const postResponseVars = get(bruJson, 'request.vars.res');
|
||||
if(postResponseVars && postResponseVars.length) {
|
||||
@@ -58,7 +60,24 @@ const runSingleRequest = async function (filename, collectionPath, collectionVar
|
||||
const responseScriptFile = get(bruJson, 'request.script.res');
|
||||
if(responseScriptFile && responseScriptFile.length) {
|
||||
const scriptRuntime = new ScriptRuntime();
|
||||
scriptRuntime.runResponseScript(responseScriptFile, response, envVariables, collectionVariables, collectionPath);
|
||||
scriptRuntime.runResponseScript(responseScriptFile, request, response, envVariables, collectionVariables, collectionPath);
|
||||
}
|
||||
|
||||
// run assertions
|
||||
let assertionResults = [];
|
||||
const assertions = get(bruJson, 'request.assert');
|
||||
if(assertions && assertions.length) {
|
||||
const assertRuntime = new AssertRuntime();
|
||||
assertionResults = assertRuntime.runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath);
|
||||
|
||||
each(assertionResults, (r) => {
|
||||
if(r.status === 'pass') {
|
||||
console.log(chalk.green(` ✓ `) + chalk.dim(`assert: ${r.lhsExpr}: ${r.rhsExpr}`));
|
||||
} else {
|
||||
console.log(chalk.red(` ✕ `) + chalk.red(`assert: ${r.lhsExpr}: ${r.rhsExpr}`));
|
||||
console.log(chalk.red(` ${r.error}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// run tests
|
||||
@@ -70,7 +89,6 @@ const runSingleRequest = async function (filename, collectionPath, collectionVar
|
||||
testResults = get(result, 'results', []);
|
||||
}
|
||||
|
||||
console.log(chalk.green(stripExtension(filename)) + chalk.dim(` (${response.status} ${response.statusText})`));
|
||||
if(testResults && testResults.length) {
|
||||
each(testResults, (testResult) => {
|
||||
if(testResult.status === 'pass') {
|
||||
@@ -80,6 +98,11 @@ const runSingleRequest = async function (filename, collectionPath, collectionVar
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
assertionResults,
|
||||
testResults
|
||||
};
|
||||
} catch (err) {
|
||||
console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`));
|
||||
}
|
||||
|
||||
20
packages/bruno-cli/src/utils/common.js
Normal file
20
packages/bruno-cli/src/utils/common.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const lpad = (str, width) => {
|
||||
let paddedStr = str;
|
||||
while (paddedStr.length < width) {
|
||||
paddedStr = ' ' + paddedStr;
|
||||
}
|
||||
return paddedStr;
|
||||
};
|
||||
|
||||
const rpad = (str, width) => {
|
||||
let paddedStr = str;
|
||||
while (paddedStr.length < width) {
|
||||
paddedStr = paddedStr + ' ';
|
||||
}
|
||||
return paddedStr;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
lpad,
|
||||
rpad
|
||||
};
|
||||
Reference in New Issue
Block a user