From 074d72d885c24ffce0b8858050839c6fab132334 Mon Sep 17 00:00:00 2001 From: David Coomber <47242934+dcoomber@users.noreply.github.com> Date: Sun, 19 Mar 2023 21:08:19 +0200 Subject: [PATCH 1/4] Add chai-http to enable to.be.json assertions Re #132 --- packages/bruno-electron/package.json | 1 + packages/bruno-js/src/runtime/assert-runtime.js | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/bruno-electron/package.json b/packages/bruno-electron/package.json index 36d0107d..8b90abc7 100644 --- a/packages/bruno-electron/package.json +++ b/packages/bruno-electron/package.json @@ -18,6 +18,7 @@ "@usebruno/schema": "0.3.1", "axios": "^0.26.0", "chai": "^4.3.7", + "chai-http": "^4.3.0", "chokidar": "^3.5.3", "dotenv": "^16.0.3", "electron-is-dev": "^2.0.0", diff --git a/packages/bruno-js/src/runtime/assert-runtime.js b/packages/bruno-js/src/runtime/assert-runtime.js index eca4f845..07053b91 100644 --- a/packages/bruno-js/src/runtime/assert-runtime.js +++ b/packages/bruno-js/src/runtime/assert-runtime.js @@ -6,6 +6,7 @@ const BrunoRequest = require('../bruno-request'); const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils'); const { expect } = chai; +const chaiHttp = require('chai-http'); /** * Assertion operators @@ -92,7 +93,7 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => { return; } - // gracefulle allyow both a,b as well as [a, b] + // gracefully allow both a,b as well as [a, b] if(operator === 'in' || operator === 'notIn') { if(rhsOperand.startsWith('[') && rhsOperand.endsWith(']')) { rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1); @@ -225,6 +226,7 @@ class AssertRuntime { expect(lhs).to.be.false; break; case 'isJson': + chai.use(chaiHttp); expect(lhs).to.be.json; break; case 'isNumber': @@ -267,4 +269,4 @@ class AssertRuntime { } } -module.exports = AssertRuntime; \ No newline at end of file +module.exports = AssertRuntime; From d21e7f6fb5ced75fd8465204c78c76d80d8681be Mon Sep 17 00:00:00 2001 From: David Coomber <47242934+dcoomber@users.noreply.github.com> Date: Tue, 21 Mar 2023 17:30:45 +0200 Subject: [PATCH 2/4] Added Chai.js plugin to cater for isJson assertion Re #132 --- packages/bruno-js/src/runtime/assert-runtime.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/bruno-js/src/runtime/assert-runtime.js b/packages/bruno-js/src/runtime/assert-runtime.js index 07053b91..5985000a 100644 --- a/packages/bruno-js/src/runtime/assert-runtime.js +++ b/packages/bruno-js/src/runtime/assert-runtime.js @@ -6,7 +6,19 @@ const BrunoRequest = require('../bruno-request'); const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils'); const { expect } = chai; -const chaiHttp = require('chai-http'); +chai.use(function (chai, utils) { + // Custom assertion for checking if a variable is JSON + chai.Assertion.addMethod('json', function () { + const obj = this._obj; + const isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) && obj.constructor === Object; + + this.assert( + isJson, + `expected ${utils.inspect(obj)} to be JSON`, + `expected ${utils.inspect(obj)} not to be JSON` + ); + }); +}); /** * Assertion operators @@ -226,7 +238,6 @@ class AssertRuntime { expect(lhs).to.be.false; break; case 'isJson': - chai.use(chaiHttp); expect(lhs).to.be.json; break; case 'isNumber': From 5c154389493de0325cfe852269c5dd6923ef06b4 Mon Sep 17 00:00:00 2001 From: David Coomber <10194965-dcoomber@users.noreply.gitlab.com> Date: Wed, 22 Mar 2023 20:56:35 +0200 Subject: [PATCH 3/4] Updated plugin to be addProperty Re #132 --- packages/bruno-js/src/runtime/assert-runtime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bruno-js/src/runtime/assert-runtime.js b/packages/bruno-js/src/runtime/assert-runtime.js index 5985000a..04b49df3 100644 --- a/packages/bruno-js/src/runtime/assert-runtime.js +++ b/packages/bruno-js/src/runtime/assert-runtime.js @@ -8,7 +8,7 @@ const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } const { expect } = chai; chai.use(function (chai, utils) { // Custom assertion for checking if a variable is JSON - chai.Assertion.addMethod('json', function () { + chai.Assertion.addProperty('json', function () { const obj = this._obj; const isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) && obj.constructor === Object; From 4884106aaa9652ecb8e93d98c3c530498df9f2ed Mon Sep 17 00:00:00 2001 From: David Coomber <47242934+dcoomber@users.noreply.github.com> Date: Wed, 22 Mar 2023 22:12:17 +0200 Subject: [PATCH 4/4] Removed chai-http Re #132 --- packages/bruno-electron/package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/bruno-electron/package.json b/packages/bruno-electron/package.json index 8b90abc7..36d0107d 100644 --- a/packages/bruno-electron/package.json +++ b/packages/bruno-electron/package.json @@ -18,7 +18,6 @@ "@usebruno/schema": "0.3.1", "axios": "^0.26.0", "chai": "^4.3.7", - "chai-http": "^4.3.0", "chokidar": "^3.5.3", "dotenv": "^16.0.3", "electron-is-dev": "^2.0.0",