From be7f92d77ff2429277fff2c0a4481691bd8ebdd6 Mon Sep 17 00:00:00 2001 From: Chirag Chandrashekhar Date: Mon, 27 Oct 2025 16:26:49 +0530 Subject: [PATCH] Resolved issue: https://github.com/usebruno/bruno/issues/4672 (#5900) - Added interpolation to setVar method's value field. - Added playwright test to test the fix. - Added jest test to test out the fix. --- Playwright - PASS Jest - PASS --- --- packages/bruno-js/src/bru.js | 2 +- packages/bruno-js/tests/runtime.spec.js | 12 +++++ .../dynamic-variable/collection/bruno.json | 9 ++++ .../collection/set-var-dynamic-variable.bru | 25 ++++++++++ .../init-user-data/preferences.json | 6 +++ .../set-var-dynamic-variable.spec.ts | 47 +++++++++++++++++++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/interpolation/dynamic-variable/collection/bruno.json create mode 100644 tests/interpolation/dynamic-variable/collection/set-var-dynamic-variable.bru create mode 100644 tests/interpolation/dynamic-variable/init-user-data/preferences.json create mode 100644 tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index 680fe7c27..9e66ef654 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -228,7 +228,7 @@ class Bru { ); } - this.runtimeVariables[key] = value; + this.runtimeVariables[key] = this.interpolate(value); } getVar(key) { diff --git a/packages/bruno-js/tests/runtime.spec.js b/packages/bruno-js/tests/runtime.spec.js index 64f0d2589..3e4bbc7a0 100644 --- a/packages/bruno-js/tests/runtime.spec.js +++ b/packages/bruno-js/tests/runtime.spec.js @@ -246,4 +246,16 @@ describe('runtime', () => { expect(result.envVariables.number).toBe(42); }); }); + + describe('bru.setVar random variable', () => { + it('should not be equal to {{$randomFirstName}}', async () => { + const script = `bru.setVar('title', '{{$randomFirstName}}')`; + + const runtime = new ScriptRuntime({ runtime: 'vm2' }); + + const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env); + + expect(result.runtimeVariables.title).not.toBe('{{$randomFirstName}}'); + }); + }); }); diff --git a/tests/interpolation/dynamic-variable/collection/bruno.json b/tests/interpolation/dynamic-variable/collection/bruno.json new file mode 100644 index 000000000..f261500e7 --- /dev/null +++ b/tests/interpolation/dynamic-variable/collection/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "dynamic-variable-interpolation", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} diff --git a/tests/interpolation/dynamic-variable/collection/set-var-dynamic-variable.bru b/tests/interpolation/dynamic-variable/collection/set-var-dynamic-variable.bru new file mode 100644 index 000000000..b5dd6ab51 --- /dev/null +++ b/tests/interpolation/dynamic-variable/collection/set-var-dynamic-variable.bru @@ -0,0 +1,25 @@ +meta { + name: set-var-dynamic-variable + type: http + seq: 1 +} + +post { + url: https://echo.usebruno.com + body: json + auth: none +} + +headers { + Content-Type: application/json +} + +script:pre-request { + bru.setVar("title", "{{$randomFirstName}}"); +} + +body:json { + { + "title": "{{title}}" + } +} diff --git a/tests/interpolation/dynamic-variable/init-user-data/preferences.json b/tests/interpolation/dynamic-variable/init-user-data/preferences.json new file mode 100644 index 000000000..3b5b1cb6f --- /dev/null +++ b/tests/interpolation/dynamic-variable/init-user-data/preferences.json @@ -0,0 +1,6 @@ +{ + "maximized": true, + "lastOpenedCollections": [ + "{{projectRoot}}/tests/interpolation/dynamic-variable/collection" + ] +} diff --git a/tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts b/tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts new file mode 100644 index 000000000..4eb57fa6f --- /dev/null +++ b/tests/interpolation/dynamic-variable/set-var-dynamic-variable.spec.ts @@ -0,0 +1,47 @@ +import { test, expect } from '../../../playwright'; +import { closeAllCollections, openCollectionAndAcceptSandbox } from '../../utils/page'; + +test.describe.serial('Dynamic Variable Interpolation', () => { + test.afterAll(async ({ pageWithUserData: page }) => { + // cleanup: close all collections + await closeAllCollections(page); + }); + + test('Verifying if the bru.setVar method interpolates random generator functions properly', async ({ pageWithUserData: page }) => { + // Open collection and accept sandbox mode + await openCollectionAndAcceptSandbox(page, 'dynamic-variable-interpolation', 'safe'); + + // Navigate to the request + await page.getByRole('complementary').getByText('set-var-dynamic-variable').click(); + + // Send the request + await page.getByTestId('send-arrow-icon').click(); + + // Wait for the response and verify status code + await expect(page.getByTestId('response-status-code')).toHaveText(/200/); + + // Verify response contains the title field and that it's not the literal interpolation string + const responsePane = page.locator('.response-pane'); + + // Check that the response contains a title field + await expect(responsePane).toContainText('"title":'); + + // Get the response body text to extract the actual title value + const responseBodyText = await responsePane.innerText(); + + // Extract the title value from the JSON response + const titleMatch = responseBodyText.match(/"title":\s*"([^"]+)"/) ?? []; + expect(titleMatch).toBeTruthy(); + + const actualTitle = titleMatch[1]; + + // Verify that the title is not the literal interpolation string + // This ensures that the randomFirstName function was properly interpolated + expect(actualTitle).not.toEqual('{{$randomFirstName}}'); + + // Additional verification: ensure the title is a string and not empty + expect(actualTitle).toBeDefined(); + expect(typeof actualTitle).toBe('string'); + expect(actualTitle.length).toBeGreaterThan(0); + }); +});