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

Fix/100 matcher includes no working #213

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/models/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,19 @@ class Expect {
if (this.updateSnapshot) {
log.warn(`Update snapshot is enabled for '${snapshot_name}'`);
file.saveSnapshot(snapshot_name, response.json);
}
}
if (value) {
const key = Object.keys(value)[0];
if(value[key].pactum_type == 'REGEX' && typeof(value[key].matcher) == 'string'){
if (value[key].matcher.includes('return')){
const expected = file.getSnapshotFile(snapshot_name, response.json);
const getValue = new Function('body',value[key].matcher);
if(getValue && expected){
value[key].matcher = getValue(expected);
value[key].value = getValue(expected);
}
}
}
const current_rules = jmv.getMatchingRules(value, '$.body');
let errors = jmv.validate(actual, jmv.getRawValue(value), current_rules, '$.body');
if (errors) {
Expand Down
77 changes: 76 additions & 1 deletion test/component/expect.jsonSnapshot.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { like } = require('pactum-matchers');
const { like, includes } = require('pactum-matchers');
const { spec } = require('../../src/index');
const fs = require('fs');
const ce = require('chai').expect;
Expand Down Expand Up @@ -65,6 +65,44 @@ describe('expectations - json snapshot', () => {
});
});

it('should validate snapshot successfully with include matchers', async () => {
await spec()
.useInteraction('get user with id 1')
.name('json snapshot - with matchers')
.get('http://localhost:9393/api/users/1')
.expectStatus(200)
.expectJsonSnapshot({
id: includes('return body.id')
});
await spec()
.useInteraction('get user with id 1')
.name('json snapshot - with matchers')
.get('http://localhost:9393/api/users/1')
.expectStatus(200)
.expectJsonSnapshot({
name: includes('return body.name')
});
});

it('should validate snapshot successfully with include matchers', async () => {
await spec()
.useInteraction('get user with id 1')
.name('json snapshot - with matchers')
.get('http://localhost:9393/api/users/1')
.expectStatus(200)
.expectJsonSnapshot({
id: includes(1)
});
await spec()
.useInteraction('get user with id 1')
.name('json snapshot - with matchers')
.get('http://localhost:9393/api/users/1')
.expectStatus(200)
.expectJsonSnapshot({
name: includes('snow')
});
});

});

describe('bdd style - jsonSnapshot', () => {
Expand Down Expand Up @@ -130,6 +168,15 @@ describe('expectations - json snapshot', () => {
http.response().should.have.jsonSnapshot('json snapshot - with matchers', { name: like('snow') });
});

it('should validate snapshot successfully with multiple includes matchers', async () => {
const http = spec();
http.useInteraction('get user with id 1');
http.get('http://localhost:9393/api/users/1');
await http.toss();
http.response().should.have.jsonSnapshot('json snapshot - with matchers', { id: includes('return body.id') });
http.response().should.have.jsonSnapshot('json snapshot - with matchers', { name: includes('return body.name') });
});

it('should not validate snapshot successfully when matchers fail', async () => {
const http = spec();
http.useInteraction('get user with id 1');
Expand All @@ -144,6 +191,34 @@ describe('expectations - json snapshot', () => {
ce(err).not.to.be.undefined;
});

it('should not validate snapshot successfully when includes matchers fail', async () => {
const http = spec();
http.useInteraction('get user with id 1');
http.get('http://localhost:9393/api/users/1');
await http.toss();
let err;
try {
http.response().should.have.jsonSnapshot('json snapshot - with matchers', { id: includes('return body') })
} catch (error) {
err = error;
}
ce(err).not.to.be.undefined;
});

it('should not validate snapshot successfully when includes matchers fail', async () => {
const http = spec();
http.useInteraction('get user with id 1');
http.get('http://localhost:9393/api/users/1');
await http.toss();
let err;
try {
http.response().should.have.jsonSnapshot('json snapshot - with matchers', { id: includes('body') })
} catch (error) {
err = error;
}
ce(err).not.to.be.undefined;
});

});

});