feat: add custom jsonBody Chai assertion + simplify Postman translation#7299
feat: add custom jsonBody Chai assertion + simplify Postman translation#7299sanish-bruno wants to merge 2 commits intousebruno:mainfrom
Conversation
WalkthroughUnifies Postman pm.response.jsonBody translations to assert on res.getBody(), adds a new Chai Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/bruno-converters/src/utils/postman-to-bruno-translator.js (1)
558-577:Array.isArray(u)branch in the generated predicate is redundant.
u !== null && typeof u === "object"already returnstruefor arrays (typeof [] === "object"), so theArray.isArray(u) ||short-circuit is never needed for correctness. The generated assertion is semantically equivalent without it, and dropping it produces cleaner output.♻️ Simplified satisfy predicate
- j.logicalExpression( - '||', - j.callExpression( - j.memberExpression(j.identifier('Array'), j.identifier('isArray')), - [j.identifier('u')] - ), - j.logicalExpression( - '&&', - j.binaryExpression('!==', j.identifier('u'), j.literal(null)), - j.binaryExpression( - '===', - j.unaryExpression('typeof', j.identifier('u'), true), - j.literal('object') - ) - ) - ) + j.logicalExpression( + '&&', + j.binaryExpression('!==', j.identifier('u'), j.literal(null)), + j.binaryExpression( + '===', + j.unaryExpression('typeof', j.identifier('u'), true), + j.literal('object') + ) + )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/bruno-converters/src/utils/postman-to-bruno-translator.js` around lines 558 - 577, The generated predicate includes an unnecessary Array.isArray(u) branch: inside the j.arrowFunctionExpression that uses identifier 'u' you can remove the j.callExpression of Array.isArray and the surrounding '||' logicalExpression and instead keep only the null/type check (u !== null && typeof u === "object"); update the j.logicalExpression construction so it no longer builds the '||' with Array.isArray and directly returns the '&&' binaryExpression chain, removing the Array.isArray-related nodes from the AST.packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/response.test.js (1)
658-675: Consider adding an object-arg variant to the existing inside-test-block coverage.The
should handle pm.response.to.have.jsonBody inside test blockstest (line 677) only exercises string-path variants. The new object-argument branch has no corresponding inside-pm.test(…)test, which would confirm the transform behaves correctly when the call is nested inside a callback.🧪 Suggested additional test case
+ it('should translate pm.response.to.have.jsonBody with object argument inside test block', () => { + const code = ` + pm.test("Body matches schema", function() { + pm.response.to.have.jsonBody({ success: true }); + }); + `; + const translatedCode = translateCode(code); + expect(translatedCode).toContain('test("Body matches schema", function() {'); + expect(translatedCode).toContain('expect(res.getBody()).to.deep.equal'); + expect(translatedCode).toContain('success: true'); + });Based on learnings: "Cover both the 'happy path' and the realistically problematic paths. Validate expected success behaviour, but also validate error handling, edge cases, and degraded-mode behaviour."
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/response.test.js` around lines 658 - 675, Add a new test variant for the existing "should handle pm.response.to.have.jsonBody inside test blocks" scenario that uses an object argument; specifically, inside the same describe/it group call translateCode on a string like 'pm.test("...", () => { pm.response.to.have.jsonBody({ success: true, data: { id: 1 } }); });' and assert the result contains the same expectations as the top-level object-arg tests (e.g., expect(res.getBody()).to.deep.equal and the object keys like success: true or nested data.id). Locate the existing test by the description "should handle pm.response.to.have.jsonBody inside test blocks" and use translateCode and the same assertion helpers to mirror the object-argument coverage.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/bruno-converters/src/utils/postman-to-bruno-translator.js`:
- Around line 558-577: The generated predicate includes an unnecessary
Array.isArray(u) branch: inside the j.arrowFunctionExpression that uses
identifier 'u' you can remove the j.callExpression of Array.isArray and the
surrounding '||' logicalExpression and instead keep only the null/type check (u
!== null && typeof u === "object"); update the j.logicalExpression construction
so it no longer builds the '||' with Array.isArray and directly returns the '&&'
binaryExpression chain, removing the Array.isArray-related nodes from the AST.
In
`@packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/response.test.js`:
- Around line 658-675: Add a new test variant for the existing "should handle
pm.response.to.have.jsonBody inside test blocks" scenario that uses an object
argument; specifically, inside the same describe/it group call translateCode on
a string like 'pm.test("...", () => { pm.response.to.have.jsonBody({ success:
true, data: { id: 1 } }); });' and assert the result contains the same
expectations as the top-level object-arg tests (e.g.,
expect(res.getBody()).to.deep.equal and the object keys like success: true or
nested data.id). Locate the existing test by the description "should handle
pm.response.to.have.jsonBody inside test blocks" and use translateCode and the
same assertion helpers to mirror the object-argument coverage.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/bruno-converters/src/utils/postman-to-bruno-translator.jspackages/bruno-converters/tests/postman/postman-translations/transpiler-tests/response.test.js
…nce translation handling - Added custom Chai assertion for jsonBody to validate JSON structures, including deep equality and nested properties. - Updated Postman to Bruno translation logic to utilize the new jsonBody assertion, improving the handling of response validations. - Enhanced test coverage for jsonBody translations, including positive and negative cases for nested properties and deep equality checks.
Summary
jsonBody(...)as a native custom Chai assertion in Bruno, matching Postman'spm.response.to.have.jsonBody(...)behaviorpm.response.to.not.have.jsonBody(...)negation (previously unhandled)Motivation
Postman's
pm.response.to.have.jsonBody(...)was previously expanded during translation into verbose Chai chains (to.have.nested.property,to.deep.equal,to.satisfy). This made translated scripts harder to read and diverged from the original Postman syntax. By implementingjsonBodyas a first-class assertion, the translator becomes a simple passthrough and Bruno users get direct access to the same ergonomic API.jsonBodyVariantsjsonBody()jsonBody({...})jsonBody("path")jsonBody("path", value)All variants support negation via
.not(e.g.,to.not.have.jsonBody("key")).Changes
bruno-js/.../assert-runtime.jsjsonBodyChai method for Node VM runtimebruno-js/.../quickjs/shims/test.jsjsonBodyon Chai prototype for QuickJS sandboxbruno-converters/.../postman-to-bruno-translator.js.not.have.jsonBodypatternbruno-converters/tests/.../response.test.jsbruno-converters/tests/.../multiline-syntax.test.jsbruno-tests/.../res/jsonBody.bruTest plan
npm run test --workspace=packages/bruno-converters— 679 tests pass (52 suites)npm run test:e2e— newjsonBody.bruvalidates the assertion at runtimepm.response.to.have.jsonBody(...)and verify translated scripts workSummary by CodeRabbit
Release Notes
New Features
jsonBody()assertion for validating JSON response bodies with support for deep equality checks, nested property verification, and value matching.Tests
jsonBody()assertions including negation scenarios and multiline syntax support.