diff --git a/packages/bruno-js/src/runtime/assert-runtime.js b/packages/bruno-js/src/runtime/assert-runtime.js index c30f1eb65fb..1af9c338aad 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 12c6e5058f0..f7ef9085450 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 2ecc19f4e52..a925db847c3 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', () => {