Skip to content

Commit

Permalink
feat: allow translation of more postman scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthoor committed Apr 10, 2024
1 parent b5a1c80 commit ff4faaf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/bruno-app/src/utils/importers/translators/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
});
Expand All @@ -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 = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]) => ({
Expand Down

0 comments on commit ff4faaf

Please sign in to comment.