feat(#190): Console Logs in scripts should be written into developer console

This commit is contained in:
Anoop M D
2023-09-14 00:20:04 +05:30
parent d3a56fdc82
commit f9ed68843d
4 changed files with 78 additions and 11 deletions

View File

@@ -26,13 +26,29 @@ class ScriptRuntime {
constructor() {
}
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath){
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath, onConsoleLog){
const bru = new Bru(envVariables, collectionVariables);
const req = new BrunoRequest(request);
const context = {
bru,
req
};
if(onConsoleLog && typeof onConsoleLog === 'function') {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, args);
}
};
context.console = {
log: customLogger('log'),
info: customLogger('info'),
warn: customLogger('warn'),
error: customLogger('error')
}
}
const vm = new NodeVM({
sandbox: context,
require: {
@@ -71,7 +87,7 @@ class ScriptRuntime {
};
}
async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath) {
async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog){
const bru = new Bru(envVariables, collectionVariables);
const req = new BrunoRequest(request);
const res = new BrunoResponse(response);
@@ -81,6 +97,21 @@ class ScriptRuntime {
req,
res
};
if(onConsoleLog && typeof onConsoleLog === 'function') {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, args);
}
};
context.console = {
log: customLogger('log'),
info: customLogger('info'),
warn: customLogger('warn'),
error: customLogger('error')
}
}
const vm = new NodeVM({
sandbox: context,
require: {

View File

@@ -20,7 +20,7 @@ class TestRuntime {
constructor() {
}
runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath) {
runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog){
const bru = new Bru(envVariables, collectionVariables);
const req = new BrunoRequest(request);
const res = new BrunoResponse(response);
@@ -47,6 +47,20 @@ class TestRuntime {
__brunoTestResults: __brunoTestResults
};
if(onConsoleLog && typeof onConsoleLog === 'function') {
const customLogger = (type) => {
return (...args) => {
onConsoleLog(type, args);
}
};
context.console = {
log: customLogger('log'),
info: customLogger('info'),
warn: customLogger('warn'),
error: customLogger('error')
}
}
const vm = new NodeVM({
sandbox: context,
require: {