From ff4faaf91e9f25234cc7c12dda59b29af153afbc Mon Sep 17 00:00:00 2001 From: Lallu Anthoor Date: Wed, 10 Apr 2024 20:46:24 +0530 Subject: [PATCH] feat: allow translation of more postman scripts --- .../src/utils/importers/translators/index.spec.js | 11 ++++++++--- .../importers/translators/postman_translation.js | 6 +++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/translators/index.spec.js b/packages/bruno-app/src/utils/importers/translators/index.spec.js index bbe82bcb57..6f9d2fb1a0 100644 --- a/packages/bruno-app/src/utils/importers/translators/index.spec.js +++ b/packages/bruno-app/src/utils/importers/translators/index.spec.js @@ -9,6 +9,8 @@ describe('postmanTranslation function', () => { pm.variables.set('key', 'value'); pm.collectionVariables.get('key'); pm.collectionVariables.set('key', 'value'); + const data = pm.response.json(); + pm.expect(pm.environment.has('key')).to.be.true; `; const expectedOutput = ` bru.getEnvVar('key'); @@ -17,6 +19,8 @@ describe('postmanTranslation function', () => { bru.setVar('key', 'value'); bru.getVar('key'); bru.setVar('key', 'value'); + const data = res.getBody(); + expect(bru.getEnvVar('key') !== undefined && bru.getEnvVar('key') !== null).to.be.true; `; expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); @@ -36,13 +40,14 @@ describe('postmanTranslation function', () => { }); test('should comment non-translated pm commands', () => { - const inputScript = "pm.test('random test', () => pm.response.json());"; - const expectedOutput = "// test('random test', () => pm.response.json());"; + const inputScript = "pm.test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; + const expectedOutput = "// test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); test('should handle multiple pm commands on the same line', () => { const inputScript = "pm.environment.get('key'); pm.environment.set('key', 'value');"; - const expectedOutput = "bru.getEnv(var); bru.setEnvVar(var, 'value');"; + const expectedOutput = "bru.getEnvVar('key'); bru.setEnvVar('key', 'value');"; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); test('should handle comments and other JavaScript code', () => { const inputScript = ` diff --git a/packages/bruno-app/src/utils/importers/translators/postman_translation.js b/packages/bruno-app/src/utils/importers/translators/postman_translation.js index 95f9065164..3c00ed3829 100644 --- a/packages/bruno-app/src/utils/importers/translators/postman_translation.js +++ b/packages/bruno-app/src/utils/importers/translators/postman_translation.js @@ -7,7 +7,11 @@ const replacements = { 'pm\\.collectionVariables\\.set\\(': 'bru.setVar(', 'pm\\.setNextRequest\\(': 'bru.setNextRequest(', 'pm\\.test\\(': 'test(', - 'pm.response.to.have\\.status\\(': 'expect(res.getStatus()).to.equal(' + 'pm.response.to.have\\.status\\(': 'expect(res.getStatus()).to.equal(', + 'pm\\.response\\.to\\.have\\.status\\(': 'expect(res.getStatus()).to.equal(', + 'pm\\.response\\.json\\(': 'res.getBody(', + 'pm\\.expect\\(': 'expect(', + 'pm\\.environment\\.has\\(([^)]+)\\)': 'bru.getEnvVar($1) !== undefined && bru.getEnvVar($1) !== null' }; const compiledReplacements = Object.entries(replacements).map(([pattern, replacement]) => ({