diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index 1113ed6f0..8c9daa437 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -143,24 +143,18 @@ const mergeScripts = (collection, request, requestTreePath, scriptFlow) => { } } - if (combinedPreReqScript.length) { - request.script.req = compact([collectionPreReqScript, ...combinedPreReqScript, request?.script?.req || '']).join(os.EOL); + request.script.req = compact([collectionPreReqScript, ...combinedPreReqScript, request?.script?.req || '']).join(os.EOL); + + if (scriptFlow === 'sequential') { + request.script.res = compact([collectionPostResScript, ...combinedPostResScript, request?.script?.res || '']).join(os.EOL); + } else { + request.script.res = compact([request?.script?.res || '', ...combinedPostResScript.reverse(), collectionPostResScript]).join(os.EOL); } - if (combinedPostResScript.length) { - if (scriptFlow === 'sequential') { - request.script.res = compact([collectionPostResScript, ...combinedPostResScript, request?.script?.res || '']).join(os.EOL); - } else { - request.script.res = compact([request?.script?.res || '', ...combinedPostResScript.reverse(), collectionPostResScript]).join(os.EOL); - } - } - - if (combinedTests.length) { - if (scriptFlow === 'sequential') { - request.tests = compact([collectionTests, ...combinedTests, request?.tests || '']).join(os.EOL); - } else { - request.tests = compact([request?.tests || '', ...combinedTests.reverse(), collectionTests]).join(os.EOL); - } + if (scriptFlow === 'sequential') { + request.tests = compact([collectionTests, ...combinedTests, request?.tests || '']).join(os.EOL); + } else { + request.tests = compact([request?.tests || '', ...combinedTests.reverse(), collectionTests]).join(os.EOL); } }; diff --git a/packages/bruno-tests/collection/collection.bru b/packages/bruno-tests/collection/collection.bru index 32eb8a7af..6d84c2abe 100644 --- a/packages/bruno-tests/collection/collection.bru +++ b/packages/bruno-tests/collection/collection.bru @@ -1,6 +1,7 @@ headers { check: again token: {{collection_pre_var_token}} + collection-header: collection-header-value } auth { @@ -17,6 +18,27 @@ vars:pre-request { collection-var: collection-var-value } +script:pre-request { + // used by `scripting/js/folder-collection script-tests` + const shouldTestCollectionScripts = bru.getVar('should-test-collection-scripts'); + if(shouldTestCollectionScripts) { + bru.setVar('collection-var-set-by-collection-script', 'collection-var-value-set-by-collection-script'); + } +} + +tests { + // used by `scripting/js/folder-collection script-tests` + const shouldTestCollectionScripts = bru.getVar('should-test-collection-scripts'); + const collectionVar = bru.getVar("collection-var-set-by-collection-script"); + if (shouldTestCollectionScripts && collectionVar) { + test("collection level test - should get the var that was set by the collection script", function() { + expect(collectionVar).to.equal("collection-var-value-set-by-collection-script"); + }); + bru.setVar('collection-var-set-by-collection-script', null); + bru.setVar('should-test-collection-scripts', null); + } +} + docs { # bruno-testbench 🐶 diff --git a/packages/bruno-tests/collection/scripting/js/folder-collection script-tests pre.bru b/packages/bruno-tests/collection/scripting/js/folder-collection script-tests pre.bru new file mode 100644 index 000000000..52911a98d --- /dev/null +++ b/packages/bruno-tests/collection/scripting/js/folder-collection script-tests pre.bru @@ -0,0 +1,16 @@ +meta { + name: folder-collection script-tests pre + type: http + seq: 4 +} + +post { + url: {{echo-host}} + body: none + auth: none +} + +script:pre-request { + bru.setVar('should-test-collection-scripts', true); + bru.setVar('should-test-folder-scripts', true); +} diff --git a/packages/bruno-tests/collection/scripting/js/folder-collection script-tests.bru b/packages/bruno-tests/collection/scripting/js/folder-collection script-tests.bru new file mode 100644 index 000000000..7a892104f --- /dev/null +++ b/packages/bruno-tests/collection/scripting/js/folder-collection script-tests.bru @@ -0,0 +1,28 @@ +meta { + name: folder-collection script-tests + type: http + seq: 5 +} + +post { + url: {{echo-host}} + body: none + auth: none +} + +script:pre-request { + // do not delete - the collection/folder scripts/tests run during this request execution +} + +tests { + const collectionHeader = req.getHeader("collection-header"); + const folderHeader = req.getHeader("folder-header"); + + test("should get the header value set at collection level", function() { + expect(collectionHeader).to.equal("collection-header-value"); + }); + + test("should get the header value set at folder level", function() { + expect(folderHeader).to.equal("folder-header-value"); + }); +} diff --git a/packages/bruno-tests/collection/scripting/js/folder.bru b/packages/bruno-tests/collection/scripting/js/folder.bru new file mode 100644 index 000000000..bab33e1ba --- /dev/null +++ b/packages/bruno-tests/collection/scripting/js/folder.bru @@ -0,0 +1,28 @@ +meta { + name: js +} + +headers { + folder-header: folder-header-value +} + +script:pre-request { + // used by `scripting/js/folder-collection script-tests` + const shouldTestFolderScripts = bru.getVar('should-test-folder-scripts'); + if(shouldTestFolderScripts) { + bru.setVar('folder-var-set-by-folder-script', 'folder-var-value-set-by-folder-script'); + } +} + +tests { + // used by `scripting/js/folder-collection script-tests` + const shouldTestFolderScripts = bru.getVar('should-test-folder-scripts'); + const folderVar = bru.getVar("folder-var-set-by-folder-script"); + if (shouldTestFolderScripts && folderVar) { + test("folder level test - should get the var that was set by the folder script", function() { + expect(folderVar).to.equal("folder-var-value-set-by-folder-script"); + }); + bru.setVar('folder-var-set-by-folder-script', null); + bru.setVar('should-test-folder-scripts', null); + } +}