-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve issue with to.be.json assertions Re #132 #135
Resolve issue with to.be.json assertions Re #132 #135
Conversation
@dcoomber The The expectation of the {
id: 1,
name: "John",
address: {
"state": "NV"
}
} The user should be able to assert that the So, we need to extend chai so that it can check if a given var is json. And here is the answer 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`
);
});
}); You can add this at the top of the file here I think this should solve our issue |
@helloanoop Thank you for your patient explanation. Although I have applied the suggested code, I am uncertain that this meets our needs. In the example below, a string, number or boolean variable (e.g. Given the following response payload: {
"firstname": "Mickey",
"lastname": "Mouse",
"totalprice": 50000,
"depositpaid": true,
"bookingdates": {
"checkin": "2022-05-01",
"checkout": "2022-05-31"
}
} When I assert that
I spent a few hours with ChatGPT working through different ways to code the Chai plugin but all my attempts were unsuccessful. This is the last iteration that I tried - it includes a chai.use(function (chai, utils) {
// Custom assertion for checking if a variable is JSON
function isJson(str) {
try {
JSON.parse(str.toString());
} catch (e) {
return false;
}
return true;
}
chai.Assertion.addMethod('json', function () {
const obj = this._obj;
const isJsonObj = isJson(obj) && typeof obj === 'object' && obj !== null && !Array.isArray(obj) && obj.constructor === Object;
this.assert(
isJsonObj,
`expected ${utils.inspect(obj)} to be JSON`,
`expected ${utils.inspect(obj)} not to be JSON`
);
});
}); I got VS Code setup so that I could debug while running, but was unable to get the breakpoints to work inside of the |
packages/bruno-electron/package.json
Outdated
@@ -18,6 +18,7 @@ | |||
"@usebruno/schema": "0.3.1", | |||
"axios": "^0.26.0", | |||
"chai": "^4.3.7", | |||
"chai-http": "^4.3.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dcoomber This dep can be removed here.
@dcoomber To confirm this, you can put |
Thank you for the encouragement, @helloanoop After reviewing https://www.chaijs.com/api/plugins/ along with stepping through some of the other assertions, I realised that we're looking to I've tested the updated assertion and its passing the positive and negative cases I've used (the failed assertions below are correctly failing). |
Awesome @dcoomber ! Can you remove the |
@helloanoop I am thrilled that I was able to figure this out! Thank you for your help. I've removed the |
Nice work @dcoomber ! Merged ! |
Proposed fix for #132 uses
chai-http
to enable the.to.be.json
assertion.