fix: include examples when writing collection items in CLI OpenAPI import (#7613)

* feat: add support for examples in collection items

- Enhanced the processCollectionItems function to include examples from imported collection items.
- Added a new test case to verify that examples are correctly written to the output file during collection creation.

* fix: coerce response status to number in collection creation tests

Updated the test for createCollectionFromBrunoObject to ensure the response status is compared as a number, improving type consistency in assertions.
This commit is contained in:
Abhishek S Lal
2026-04-01 21:34:23 +05:30
committed by GitHub
parent 97467c57bf
commit 64bdef23ec
2 changed files with 59 additions and 1 deletions

View File

@@ -641,7 +641,8 @@ const processCollectionItems = async (items = [], currentPath, options = {}) =>
assertions: item.request?.assertions || [],
tests: item.request?.tests || '',
docs: item.request?.docs || ''
}
},
examples: item.examples || []
};
// Convert to YML format and write to file

View File

@@ -124,6 +124,63 @@ describe('createCollectionFromBrunoObject', () => {
expect(nestedRequest).toHaveProperty('request.method', 'GET');
});
it('writes examples from imported collection items', async () => {
createOutputDir();
await createCollectionFromBrunoObject(
{
name: 'examples-collection',
items: [
{
type: 'http-request',
name: 'Get Users',
filename: 'get-users.bru',
seq: 1,
request: {
method: 'GET',
url: 'https://api.example.com/users'
},
examples: [
{
uid: 'ex1',
name: 'Success Response',
type: 'http-request',
request: {
url: 'https://api.example.com/users',
method: 'GET',
headers: [],
params: [],
body: { mode: 'none' }
},
response: {
status: 200,
statusText: 'OK',
headers: [{ uid: 'h1', name: 'Content-Type', value: 'application/json', enabled: true }],
body: {
type: 'json',
content: JSON.stringify([{ id: 1, name: 'John' }], null, 2)
}
}
}
]
}
]
},
outputDir,
{ format: 'bru' }
);
const httpPath = path.join(outputDir, 'get-users.bru');
expect(fs.existsSync(httpPath)).toBe(true);
const parsed = parseBruRequestFromPath(httpPath);
expect(parsed.examples).toBeDefined();
expect(parsed.examples).toHaveLength(1);
expect(parsed.examples[0].name).toBe('Success Response');
expect(Number(parsed.examples[0].response.status)).toBe(200);
expect(parsed.examples[0].response.body.content).toContain('John');
});
it('throws for unsupported item types', async () => {
createOutputDir();