Skip to content
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

Merged
merged 4 commits into from
Mar 23, 2023

Conversation

dcoomber
Copy link
Member

Proposed fix for #132 uses chai-http to enable the .to.be.json assertion.

@helloanoop
Copy link
Contributor

@dcoomber The chai-http package seems to be checking at the response header property
https://github.com/chaijs/chai-http/blob/master/lib/http.js#L46

The expectation of the isJson is that we can assert any given path in the response is a json.
Ex: Given this response

{
   id: 1,
   name: "John",
   address: {
     "state": "NV"
   }
}

The user should be able to assert that the res.body.address is a json.

So, we need to extend chai so that it can check if a given var is json.
I use ChatGPT, I recommend you give it a try if you haven't used it.
I asked ChatGPT "write a chaijs plugin that checks whether a given variable is json or not ?"

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

@dcoomber
Copy link
Member Author

@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. Mickey, 50000 or true) is passing the isJson test where I think that they should be failing.

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 <<variable>> isJson

variable then (expected result) actual result test outcome
res fail fail
res.body pass pass
res.body.firstname fail pass
res.body.bookingdates pass pass

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 JSON.parse step to confirm that the object is actually a JSON string.

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 chai.use(function (chai, utils) {} block. To me it looks as if the custom plugin method json isn't being used.

@@ -18,6 +18,7 @@
"@usebruno/schema": "0.3.1",
"axios": "^0.26.0",
"chai": "^4.3.7",
"chai-http": "^4.3.0",
Copy link
Contributor

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.

@helloanoop
Copy link
Contributor

To me it looks as if the custom plugin method json isn't being used.

@dcoomber To confirm this, you can put console.log() statements inside the chaijs json function. These logs will be displayed in the terminal where the electron app is running.

@dcoomber dcoomber changed the title Add chai-http to enable to.be.json assertions Re #132 Resolve issue with to.be.json assertions Re #132 Mar 22, 2023
@dcoomber
Copy link
Member Author

dcoomber commented Mar 22, 2023

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 addProperty and not addMethod.

I've tested the updated assertion and its passing the positive and negative cases I've used (the failed assertions below are correctly failing).

image

@helloanoop
Copy link
Contributor

Awesome @dcoomber !
I am sure you had had the high of "damn it, i finally cracked this" 😃

Can you remove the chai-http in package.json ?
Once done, this PR is good to merge.

@dcoomber
Copy link
Member Author

@helloanoop I am thrilled that I was able to figure this out! Thank you for your help.

I've removed the chai-http package 😅 .

@helloanoop helloanoop merged commit fbc6e7b into usebruno:main Mar 23, 2023
@helloanoop
Copy link
Contributor

Nice work @dcoomber ! Merged !

@dcoomber dcoomber deleted the bugfix/132-isjson-assertion branch March 23, 2023 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants