feat: allow translation of more postman scripts (#2054)

This commit is contained in:
Lallu Anthoor 2024-05-13 21:22:05 +05:30 committed by GitHub
parent e149c8dc9a
commit 4df78910f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 4 deletions

View File

@ -9,6 +9,8 @@ describe('postmanTranslation function', () => {
pm.variables.set('key', 'value'); pm.variables.set('key', 'value');
pm.collectionVariables.get('key'); pm.collectionVariables.get('key');
pm.collectionVariables.set('key', 'value'); pm.collectionVariables.set('key', 'value');
const data = pm.response.json();
pm.expect(pm.environment.has('key')).to.be.true;
`; `;
const expectedOutput = ` const expectedOutput = `
bru.getEnvVar('key'); bru.getEnvVar('key');
@ -17,6 +19,8 @@ describe('postmanTranslation function', () => {
bru.setVar('key', 'value'); bru.setVar('key', 'value');
bru.getVar('key'); bru.getVar('key');
bru.setVar('key', 'value'); 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); expect(postmanTranslation(inputScript)).toBe(expectedOutput);
}); });
@ -36,13 +40,14 @@ describe('postmanTranslation function', () => {
}); });
test('should comment non-translated pm commands', () => { test('should comment non-translated pm commands', () => {
const inputScript = "pm.test('random test', () => pm.response.json());"; const inputScript = "pm.test('random test', () => pm.variables.replaceIn('{{$guid}}'));";
const expectedOutput = "// test('random test', () => pm.response.json());"; const expectedOutput = "// test('random test', () => pm.variables.replaceIn('{{$guid}}'));";
expect(postmanTranslation(inputScript)).toBe(expectedOutput); expect(postmanTranslation(inputScript)).toBe(expectedOutput);
}); });
test('should handle multiple pm commands on the same line', () => { test('should handle multiple pm commands on the same line', () => {
const inputScript = "pm.environment.get('key'); pm.environment.set('key', 'value');"; 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', () => { test('should handle comments and other JavaScript code', () => {
const inputScript = ` const inputScript = `

View File

@ -7,7 +7,11 @@ const replacements = {
'pm\\.collectionVariables\\.set\\(': 'bru.setVar(', 'pm\\.collectionVariables\\.set\\(': 'bru.setVar(',
'pm\\.setNextRequest\\(': 'bru.setNextRequest(', 'pm\\.setNextRequest\\(': 'bru.setNextRequest(',
'pm\\.test\\(': 'test(', '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]) => ({ const compiledReplacements = Object.entries(replacements).map(([pattern, replacement]) => ({