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

Pre-request variables are not being interpolated within request body when running tests via CLI #2871

Open
2 tasks done
greaterking opened this issue Aug 20, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@greaterking
Copy link

I have checked the following:

  • I use the newest version of bruno.
  • I've searched existing issues and found nothing related to my issue.

Describe the bug

When running tests via CLI if pre-request variables used within the request body(json) never get interpolated and are submitted as such {{some_variable}} in your request.

This does not happen within the bruno the app this ONLY happens when running test via bruno CLI

Might be slightly related to...

Issues:

PRs:

Environment

Screenshot 2024-08-20 at 11 17 02 AM Screenshot 2024-08-20 at 11 17 26 AM Screenshot 2024-08-20 at 11 18 35 AM

.bru file to reproduce the bug

No response

Screenshots/Live demo link

Create a test and set some vars
Screenshot 2024-08-20 at 11 09 43 AM

Use them with in the body
Screenshot 2024-08-20 at 11 09 31 AM

Define some assertions using those variables
image

Every tests using those variables will fail
image

Instead of the variable values being sent with the request the template literal is what ends up being saved (in my create example anyway)

By the time assertions are run the variable are working properly again but whats returned as the response will be wrong since the string literal was saved and not it's value.

Currently I'm working around this with this script in my collection so its run with every request

// Check if this is running via command line
// (This property only exists when tests are run via bruno app)
// note: wish there was a native way of doing this ... bru.executionMode() ..'CLI' || 'App'
function isCLI() {
  return typeof req.historyLogger === "undefined"; 
}

// When running tests from the command line ensure
// all variables are set before any requests are made
function initializeCliPreRequestVars() {
  
  if (!isCLI()) return;
  
  const {req: {requestVariables}} = req;

  for (const variableKey in requestVariables) {
      bru.setVar(variableKey, requestVariables[variableKey]);
  }
}

initializeCliPreRequestVars();
@greaterking greaterking added the bug Something isn't working label Aug 20, 2024
@Nightbr
Copy link

Nightbr commented Aug 22, 2024

Same here and also Vars from Pre Request are now (it was working before 1.20) not accessible in Pre Request Script.

const myVar = bru.getVar('my_var'); -> always return undefined

Related: #2759

Fixed: replace bru.getVar('my_var') with bru.getRequestVar('my_var').

Just need to update the doc: https://docs.usebruno.com/scripting/javascript-reference#collection-variables and everything will be clear ;)

@HenriqueRech
Copy link

I'm having the same issue.
I'm trying to use the collections our team built in the Bruno App to automate some tests using the cli and its reporters, but right now it doesn't seem to be interpolating any of the collection.bru, folder.bru or the Request vars:pre-request.

I noticed that the bruno-cli var interpolation doesn't seem to use any of the request object variables into account, like the bruno-electron interpolation does.

const interpolateVars = (request, envVars = {}, runtimeVariables = {}, processEnvVars = {}) => {
// we clone envVars because we don't want to modify the original object
envVars = cloneDeep(envVars);

const interpolateVars = (request, envVariables = {}, runtimeVariables = {}, processEnvVars = {}) => {
const globalEnvironmentVariables = request?.globalEnvironmentVariables || {};
const collectionVariables = request?.collectionVariables || {};
const folderVariables = request?.folderVariables || {};
const requestVariables = request?.requestVariables || {};
// we clone envVars because we don't want to modify the original object
envVariables = cloneDeep(envVariables);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants