diff --git a/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js b/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js index 96fade0d..4211e8ff 100644 --- a/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js +++ b/packages/bruno-app/src/components/Sidebar/ImportCollectionLocation/index.js @@ -65,20 +65,18 @@ const TranslationLog = ({ translationLog }) => {
{value.script && ( -
- - test : - {value.script.map((scriptValue, index) => ( - - {scriptValue} - {index < value.script.length - 1 && <> - } - - ))} - +
+ script : + {value.script.map((scriptValue, index) => ( + + {scriptValue} + {index < value.script.length - 1 && <> - } + + ))}
)} {value.test && ( -
+
test : {value.test.map((testValue, index) => (
diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index dc291dbd..0683a3bb 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -54,7 +54,7 @@ const convertV21Auth = (array) => { }, {}); }; -const translationLog = {}; +let translationLog = {}; const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => { brunoParent.items = brunoParent.items || []; @@ -394,9 +394,13 @@ const importCollection = (options) => { .then((collection) => resolve({ collection, translationLog })) .catch((err) => { console.log(err); + translationLog = {}; reject(new BrunoError('Import collection failed')); }) - .then(() => logTranslationDetails(translationLog)); + .then(() => { + logTranslationDetails(translationLog); + translationLog = {}; + }); }); }; 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 6f9d2fb1..b70691f2 100644 --- a/packages/bruno-app/src/utils/importers/translators/index.spec.js +++ b/packages/bruno-app/src/utils/importers/translators/index.spec.js @@ -40,8 +40,8 @@ describe('postmanTranslation function', () => { }); test('should comment non-translated pm commands', () => { - const inputScript = "pm.test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; - const expectedOutput = "// test('random test', () => pm.variables.replaceIn('{{$guid}}'));"; + const inputScript = "pm.test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; + const expectedOutput = "// test('random test', () => postman.variables.replaceIn('{{$guid}}'));"; expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); test('should handle multiple pm commands on the same line', () => { @@ -137,3 +137,17 @@ describe('postmanTranslation function', () => { expect(postmanTranslation(inputScript)).toBe(expectedOutput); }); }); + +test('should handle response commands', () => { + const inputScript = ` + const responseTime = pm.response.responseTime; + const responseCode = pm.response.code; + const responseText = pm.response.text(); + `; + const expectedOutput = ` + const responseTime = res.getResponseTime(); + const responseCode = res.getStatus(); + const responseText = res.getBody()?.toString(); + `; + expect(postmanTranslation(inputScript)).toBe(expectedOutput); +}); 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 7844f91f..bd60fbca 100644 --- a/packages/bruno-app/src/utils/importers/translators/postman_translation.js +++ b/packages/bruno-app/src/utils/importers/translators/postman_translation.js @@ -13,10 +13,19 @@ const replacements = { 'pm\\.expect\\(': 'expect(', 'pm\\.environment\\.has\\(([^)]+)\\)': 'bru.getEnvVar($1) !== undefined && bru.getEnvVar($1) !== null', 'pm\\.response\\.code': 'res.getStatus()', - 'pm\\.response\\.text\\(': 'res.getBody()?.toString(' + 'pm\\.response\\.text\\(': 'res.getBody()?.toString(', + 'pm\\.expect\\.fail\\(': 'expect.fail(', + 'pm\\.response\\.responseTime': 'res.getResponseTime()' }; -const compiledReplacements = Object.entries(replacements).map(([pattern, replacement]) => ({ +const extendedReplacements = Object.keys(replacements).reduce((acc, key) => { + const newKey = key.replace(/^pm\\\./, 'postman\\.'); + acc[key] = replacements[key]; + acc[newKey] = replacements[key]; + return acc; +}, {}); + +const compiledReplacements = Object.entries(extendedReplacements).map(([pattern, replacement]) => ({ regex: new RegExp(pattern, 'g'), replacement })); @@ -31,8 +40,8 @@ export const postmanTranslation = (script, logCallback) => { modified = true; } } - if (modifiedScript.includes('pm.')) { - modifiedScript = modifiedScript.replace(/^(.*pm\..*)$/gm, '// $1'); + if (modifiedScript.includes('pm.') || modifiedScript.includes('postman.')) { + modifiedScript = modifiedScript.replace(/^(.*(pm\.|postman\.).*)$/gm, '// $1'); logCallback?.(); } return modifiedScript;