Skip to content

Commit

Permalink
fix: Use == over === to determine equality for json path values
Browse files Browse the repository at this point in the history
Prior to this commit, it was not possible to do:

```
And the response body json path at "$.[0].age" should equal "10"
```

...because the a number would not match the string passed. This
change introduces a custom chai plugin to perform loose equality
checks in this scenario.

The custom plugin should be removed in future if we upgrade to Chai 5
in favor of:

```
expect('3').to.loose.equal(3)
```
  • Loading branch information
Philip Mander committed Jul 10, 2019
1 parent 2718a5f commit 9aca1be
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
6 changes: 4 additions & 2 deletions docs/step-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ Ensure a response header equals the expect value
Then the response header "Content-Type" should equal "application/json"
```
### Then the response body json path at {string} should equal {string}
Ensure a JSON response body contains a given value at the JSON path.
Ensure a JSON response body equals a given value at the JSON path. Equality is determined
using `==` so giving value "10" will equal the number 10 in JSON.
See [http://goessner.net/articles/JsonPath/](http://goessner.net/articles/JsonPath/)

**Example**
Expand All @@ -333,7 +334,8 @@ Then the response body json path at "$.[1].name" should equal "Rover"
Then json path at "$.[1].name" should equal "Rover"
```
### Then the response body json path at {string} should match {string}
Ensure a JSON response body contains a given value at the JSON path.
Ensure a JSON response body at the given JSON path, matches a regular expression.
n.b. For simpliciy, Bat variables in regular expressions are not subsituted.
See [http://goessner.net/articles/JsonPath/](http://goessner.net/articles/JsonPath/)

**Example**
Expand Down
21 changes: 19 additions & 2 deletions src/steps-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,25 @@ const ql = require('superagent-graphql');
const readFileAsync = promisify(readFile);

chai.use(require('chai-match'));
const { expect } = chai;
chai.use(_chai => {
// support assertions with == (vs ===)
// this should be available in the future with Chai v5
// https://github.com/chaijs/chai/issues/906
_chai.Assertion.addMethod('equalLoosely', function (val) {
const { _obj } = this;

this.assert(
val == _obj,
'expected #{this} to equal #{exp}',
'expected #{this} to not equal #{exp}',
val,
_obj,
true,
);
});
});

const { expect } = chai;

const methodsWithBodies = ['POST', 'PUT', 'PATCH', 'DELETE'];

Expand Down Expand Up @@ -203,7 +220,7 @@ async function responseHeaderEquals(headerName, value) {
async function responseBodyJsonPathEquals(path, value) {
const { body } = await this.getResponse();
const actualValue = JSONPath.eval(body, path)[0];
expect(actualValue).to.equal(this.replaceVars(value));
expect(actualValue).to.equalLoosely(this.replaceVars(value));
}

async function responseBodyJsonPathMatches(path, value) {
Expand Down
3 changes: 2 additions & 1 deletion src/steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ function registerSteps({ Given, When, Then }) {

/**
* ### Then the response body json path at {string} should equal {string}
* Ensure a JSON response body contains a given value at the JSON path.
* Ensure a JSON response body equals a given value at the JSON path. Equality is determined
* using `==` so giving value "10" will equal the number 10 in JSON.
* See [http://goessner.net/articles/JsonPath/](http://goessner.net/articles/JsonPath/)
*
* @example
Expand Down
3 changes: 2 additions & 1 deletion test/features/test-steps-short.feature
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ Feature: API Testing Steps
}
"""
And json path at "$.[1].name" should equal "Rover"
And the response body json path at "$.[0].age" should match "\d{2}"
And json path at "$.[0].age" should equal "10"
And json path at "$.[0].age" should match "\d{2}"
And the response header "Content-Language" should equal "en"

@short
Expand Down
1 change: 1 addition & 0 deletions test/features/test-steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Feature: API Testing Steps
}
"""
And the response body json path at "$.[1].name" should equal "Rover"
And the response body json path at "$.[0].age" should equal "10"
And the response body json path at "$.[0].age" should match "\d{2}"
And the response header "Content-Language" should equal "en"

Expand Down

0 comments on commit 9aca1be

Please sign in to comment.