Skip to content

Commit

Permalink
Added Chai.js plugin to cater for isJson assertion Re usebruno#132
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoomber committed Mar 21, 2023
1 parent 074d72d commit d21e7f6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/bruno-js/src/runtime/assert-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
Expand Down

0 comments on commit d21e7f6

Please sign in to comment.