mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-09 06:55:03 +00:00
feat: assert runtime
This commit is contained in:
@@ -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})`));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user