Refactor: Reorganize Postman translation tests into modules (#4524)

This commit is contained in:
pooja-bruno 2025-04-22 16:42:09 +05:30 committed by GitHub
parent 317ccccfc6
commit 3bd44ea7ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 148 additions and 139 deletions

View File

@ -1,139 +0,0 @@
const { default: postmanTranslation } = require("../../src/postman/postman-translations");
describe('postmanTranslation function', () => {
test('should translate pm commands correctly', () => {
const inputScript = `
pm.environment.get('key');
pm.environment.set('key', 'value');
pm.variables.get('key');
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');
bru.setEnvVar('key', 'value');
bru.getVar('key');
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);
});
test('should not translate non-pm commands', () => {
const inputScript = `
console.log('This script does not contain pm commands.');
const data = pm.environment.get('key');
pm.collectionVariables.set('key', data);
`;
const expectedOutput = `
console.log('This script does not contain pm commands.');
const data = bru.getEnvVar('key');
bru.setVar('key', data);
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
test('should comment non-translated pm commands', () => {
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', () => {
const inputScript = "pm.environment.get('key'); pm.environment.set('key', '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 = `
// This is a comment
const value = 'test';
pm.environment.set('key', value);
/*
Multi-line comment
*/
const result = pm.environment.get('key');
console.log('Result:', result);
`;
const expectedOutput = `
// This is a comment
const value = 'test';
bru.setEnvVar('key', value);
/*
Multi-line comment
*/
const result = bru.getEnvVar('key');
console.log('Result:', result);
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
test('should handle nested commands and edge cases', () => {
const inputScript = `
const sampleObjects = [
{
key: pm.environment.get('key'),
value: pm.variables.get('value')
},
{
key: pm.collectionVariables.get('key'),
value: pm.collectionVariables.get('value')
}
];
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
// this is a comment
acc[key] = pm.collectionVariables.get(pm.environment.get(value));
return acc; // Return the accumulator
}, {});
Object.values(dataTesting).forEach((data) => {
pm.environment.set(data.key, pm.variables.get(data.value));
});
`;
const expectedOutput = `
const sampleObjects = [
{
key: bru.getEnvVar('key'),
value: bru.getVar('value')
},
{
key: bru.getVar('key'),
value: bru.getVar('value')
}
];
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
// this is a comment
acc[key] = bru.getVar(bru.getEnvVar(value));
return acc; // Return the accumulator
}, {});
Object.values(dataTesting).forEach((data) => {
bru.setEnvVar(data.key, bru.getVar(data.value));
});
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
test('should handle test commands', () => {
const inputScript = `
pm.test('Status code is 200', () => {
pm.response.to.have.status(200);
});
pm.test('this test will fail', () => {
return false
});
`;
const expectedOutput = `
test('Status code is 200', () => {
expect(res.getStatus()).to.equal(200);
});
test('this test will fail', () => {
return false
});
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
});

View File

@ -0,0 +1,53 @@
const { default: postmanTranslation } = require("../../../src/postman/postman-translations");
describe('postmanTranslations - comment handling', () => {
test('should not translate non-pm commands', () => {
const inputScript = `
console.log('This script does not contain pm commands.');
const data = pm.environment.get('key');
pm.collectionVariables.set('key', data);
`;
const expectedOutput = `
console.log('This script does not contain pm commands.');
const data = bru.getEnvVar('key');
bru.setVar('key', data);
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
test('should comment non-translated pm commands', () => {
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', () => {
const inputScript = "pm.environment.get('key'); pm.environment.set('key', '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 = `
// This is a comment
const value = 'test';
pm.environment.set('key', value);
/*
Multi-line comment
*/
const result = pm.environment.get('key');
console.log('Result:', result);
`;
const expectedOutput = `
// This is a comment
const value = 'test';
bru.setEnvVar('key', value);
/*
Multi-line comment
*/
const result = bru.getEnvVar('key');
console.log('Result:', result);
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
});

View File

@ -0,0 +1,47 @@
const { default: postmanTranslation } = require("../../../src/postman/postman-translations");
describe('postmanTranslations - edge cases', () => {
test('should handle nested commands and edge cases', () => {
const inputScript = `
const sampleObjects = [
{
key: pm.environment.get('key'),
value: pm.variables.get('value')
},
{
key: pm.collectionVariables.get('key'),
value: pm.collectionVariables.get('value')
}
];
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
// this is a comment
acc[key] = pm.collectionVariables.get(pm.environment.get(value));
return acc; // Return the accumulator
}, {});
Object.values(dataTesting).forEach((data) => {
pm.environment.set(data.key, pm.variables.get(data.value));
});
`;
const expectedOutput = `
const sampleObjects = [
{
key: bru.getEnvVar('key'),
value: bru.getVar('value')
},
{
key: bru.getVar('key'),
value: bru.getVar('value')
}
];
const dataTesting = Object.entries(sampleObjects || {}).reduce((acc, [key, value]) => {
// this is a comment
acc[key] = bru.getVar(bru.getEnvVar(value));
return acc; // Return the accumulator
}, {});
Object.values(dataTesting).forEach((data) => {
bru.setEnvVar(data.key, bru.getVar(data.value));
});
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
});

View File

@ -0,0 +1,23 @@
const { default: postmanTranslation } = require("../../../src/postman/postman-translations");
describe('postmanTranslations - test commands', () => {
test('should handle test commands', () => {
const inputScript = `
pm.test('Status code is 200', () => {
pm.response.to.have.status(200);
});
pm.test('this test will fail', () => {
return false
});
`;
const expectedOutput = `
test('Status code is 200', () => {
expect(res.getStatus()).to.equal(200);
});
test('this test will fail', () => {
return false
});
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
});

View File

@ -0,0 +1,25 @@
const { default: postmanTranslation } = require("../../../src/postman/postman-translations");
describe('postmanTranslations - variables commands', () => {
test('should translate variable commands correctly', () => {
const inputScript = `
pm.environment.get('key');
pm.environment.set('key', 'value');
pm.variables.get('key');
pm.variables.set('key', 'value');
pm.collectionVariables.get('key');
pm.collectionVariables.set('key', 'value');
pm.expect(pm.environment.has('key')).to.be.true;
`;
const expectedOutput = `
bru.getEnvVar('key');
bru.setEnvVar('key', 'value');
bru.getVar('key');
bru.setVar('key', 'value');
bru.getVar('key');
bru.setVar('key', 'value');
expect(bru.getEnvVar('key') !== undefined && bru.getEnvVar('key') !== null).to.be.true;
`;
expect(postmanTranslation(inputScript)).toBe(expectedOutput);
});
});