Skip to content

Commit

Permalink
Merge pull request #1103 from pact-foundation/fix/eachValue-eachKey-m…
Browse files Browse the repository at this point in the history
…atchers

add support for `eachKey` and `eachValue` matchers
  • Loading branch information
mefellows committed Jul 14, 2023
2 parents fdca5de + 0b2098b commit 4c4c839
Show file tree
Hide file tree
Showing 8 changed files with 2,246 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ const animalBodyExpectation = {
| `includes` | value: string | Value that must include the example value as a substring. |
| `nullValue` | | Value that must be null. This will only match the JSON Null value. For other content types, it will match if the attribute is missing. |
| `arrayContaining` | variants... | Matches the items in an array against a number of variants. Matching is successful if each variant occurs once in the array. Variants may be objects containing matching rules. |
| `eachKeyLike` | key: string, template: any | Object where the keys itself is ignored, but the values must match a particular shape. Variants may be objects containing matching rules |
| `eachKeyMatches` | example: object, rules: Matcher[] | Object where the _keys_ must match the supplied matching rules and the values are ignored. |
| `eachValueMatches` | example: object, rules: Matcher[] | Object where the _values_ must match the supplied matching rules and keys are ignored. |
| `fromProviderState` | expression: string, exampleValue: string | Sets a type matcher and a provider state generator. See the section below. |

#### Array contains matcher
Expand Down
10 changes: 10 additions & 0 deletions examples/v4/matchers/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"bail": true,
"reporter": "spec",
"colors": true,
"timeout": 30000,
"exit": true,
"require": ["ts-node/register", "source-map-support/register"],
"full-trace": true,
"recursive": true
}
100 changes: 100 additions & 0 deletions examples/v4/matchers/consumer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* tslint:disable:no-unused-expression no-empty */
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import {
SpecificationVersion,
PactV4,
LogLevel,
MatchersV3,
} from '@pact-foundation/pact';
import axios from 'axios';

chai.use(chaiAsPromised);

const { expect } = chai;

process.env.ENABLE_FEATURE_V4 = 'true';

describe('V4 Matchers', () => {
describe('eachKeyMatches', () => {
it('returns the shape of object given to the matcher', async () => {
const pact = new PactV4({
consumer: 'myconsumer',
provider: 'myprovider',
spec: SpecificationVersion.SPECIFICATION_VERSION_V4,
logLevel: (process.env.LOG_LEVEL as LogLevel) || 'error',
});
await pact
.addInteraction()
.uponReceiving('a request only checks the keys and ignores the values')
.withRequest('GET', '/eachKeyMatches')
.willRespondWith(200, (builder) => {
builder.jsonBody(
MatchersV3.eachKeyMatches(
{
key1: "a string we don't care about",
key2: 1,
},
[MatchersV3.regex(/[a-z]{3,}[0-9]/, 'key1')]
)
);
})
.executeTest((mockserver) => {
return axios
.request({
baseURL: mockserver.url,
method: 'GET',
url: '/eachKeyMatches',
})
.then((res) => {
expect(res.data.key1).to.equal("a string we don't care about");
expect(res.data.key2).to.equal(1);
});
});
});
});

describe('eachValueMatches', () => {
it('returns the shape of object given to the matcher', async () => {
const pact = new PactV4({
consumer: 'myconsumer',
provider: 'myprovider',
spec: SpecificationVersion.SPECIFICATION_VERSION_V4,
logLevel: (process.env.LOG_LEVEL as LogLevel) || 'error',
});
await pact
.addInteraction()
.uponReceiving(
'a request that ignores the keys and only checks the values'
)
.withRequest('GET', '/eachValueMatches')
.willRespondWith(200, (builder) => {
builder.jsonBody(
MatchersV3.eachValueMatches(
{
key1: 'a string',
key2: 'this is another string',
key3: 'this, unbelievably, is YET ANOTHER, string',
},
[MatchersV3.regex(/[a-z\s]+/, 'a string')]
)
);
})
.executeTest((mockserver) => {
return axios
.request({
baseURL: mockserver.url,
method: 'GET',
url: '/eachValueMatches',
})
.then((res) => {
expect(res.data.key1).to.equal('a string');
expect(res.data.key2).to.equal('this is another string');
expect(res.data.key3).to.equal(
'this, unbelievably, is YET ANOTHER, string'
);
});
});
});
});
});
Loading

0 comments on commit 4c4c839

Please sign in to comment.