Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow translation of more postman scripts #2054

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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