From 5c1dc1184a84b92f60aa71b29ab45c6d60e3c06f Mon Sep 17 00:00:00 2001 From: Abhishek S Lal Date: Fri, 3 Apr 2026 01:47:57 +0530 Subject: [PATCH] fix: isJson assertion should accept arrays as valid JSON (#7620) * fix(assert-runtime): update JSON validation to allow arrays and enhance test coverage - Modified the JSON validation logic to accept arrays as valid JSON objects. - Updated tests to ensure correct behavior for various array scenarios, including empty arrays and arrays of strings and objects. * fix(assert-runtime): refine JSON validation logic to correctly handle arrays - Updated the JSON validation to allow arrays as valid JSON objects, ensuring compatibility with various data structures. - Adjusted the test assertions to reflect the new validation criteria. --- .../bruno-js/src/runtime/assert-runtime.js | 4 +-- .../src/sandbox/quickjs/shims/test.js | 4 +-- packages/bruno-js/tests/runtime.spec.js | 28 +++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/bruno-js/src/runtime/assert-runtime.js b/packages/bruno-js/src/runtime/assert-runtime.js index c30f1eb65..1af9c338a 100644 --- a/packages/bruno-js/src/runtime/assert-runtime.js +++ b/packages/bruno-js/src/runtime/assert-runtime.js @@ -17,8 +17,8 @@ chai.use(function (chai, utils) { // Objects created inside Node's vm.createContext() have a different Object constructor, // so obj.constructor === Object fails for objects passed via res.setBody() from scripts. // Note: toString check is more permissive than constructor check — custom class instances - const isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) - && Object.prototype.toString.call(obj) === '[object Object]'; + const isJson = typeof obj === 'object' && obj !== null + && (Array.isArray(obj) || Object.prototype.toString.call(obj) === '[object Object]'); this.assert(isJson, `expected ${utils.inspect(obj)} to be JSON`, `expected ${utils.inspect(obj)} not to be JSON`); }); diff --git a/packages/bruno-js/src/sandbox/quickjs/shims/test.js b/packages/bruno-js/src/sandbox/quickjs/shims/test.js index 12c6e5058..f7ef90854 100644 --- a/packages/bruno-js/src/sandbox/quickjs/shims/test.js +++ b/packages/bruno-js/src/sandbox/quickjs/shims/test.js @@ -69,8 +69,8 @@ const addBruShimToContext = (vm, __brunoTestResults) => { Object.defineProperty(proto, 'json', { get: function () { var obj = this._obj; - var isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) && - Object.prototype.toString.call(obj) === '[object Object]'; + var isJson = typeof obj === 'object' && obj !== null && + (Array.isArray(obj) || Object.prototype.toString.call(obj) === '[object Object]'); this.assert(isJson, 'expected #{this} to be JSON', 'expected #{this} not to be JSON'); return this; }, diff --git a/packages/bruno-js/tests/runtime.spec.js b/packages/bruno-js/tests/runtime.spec.js index 2ecc19f4e..a925db847 100644 --- a/packages/bruno-js/tests/runtime.spec.js +++ b/packages/bruno-js/tests/runtime.spec.js @@ -317,12 +317,36 @@ describe('runtime', () => { expect(results[0].status).toBe('pass'); }); - it('should fail for an array', () => { + it('should pass for an array', () => { const results = runAssertions( [{ name: 'res.body', value: 'isJson', enabled: true }], makeResponse([1, 2, 3]) ); - expect(results[0].status).toBe('fail'); + expect(results[0].status).toBe('pass'); + }); + + it('should pass for an array of strings', () => { + const results = runAssertions( + [{ name: 'res.body', value: 'isJson', enabled: true }], + makeResponse(['A55001213ZX0A']) + ); + expect(results[0].status).toBe('pass'); + }); + + it('should pass for an empty array', () => { + const results = runAssertions( + [{ name: 'res.body', value: 'isJson', enabled: true }], + makeResponse([]) + ); + expect(results[0].status).toBe('pass'); + }); + + it('should pass for an array of objects', () => { + const results = runAssertions( + [{ name: 'res.body', value: 'isJson', enabled: true }], + makeResponse([{ id: 1 }, { id: 2 }]) + ); + expect(results[0].status).toBe('pass'); }); it('should fail for a string', () => {