Skip to content

Commit

Permalink
Merge pull request #78 from pactumjs/v3
Browse files Browse the repository at this point in the history
3.0.18
  • Loading branch information
ASaiAnudeep authored Aug 8, 2021
2 parents 67952ba + 8e97e57 commit 81f9db6
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.0.17",
"version": "3.0.18",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions src/adapters/json.schema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { validator } = require('@exodus/schemasafe');

function validate(schema, target) {
const validate = validator(schema, { includeErrors: true });
function validate(schema, target, options = {}) {
options.includeErrors = true;
const validate = validator(schema, options);
validate(target);
if (validate.errors) {
return JSON.stringify(validate.errors, null, 2);
Expand Down
2 changes: 2 additions & 0 deletions src/exports/expect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export interface Have {
jsonLike(value: any): void;
jsonLike(path: string, value: any): void;
jsonSchema(schema: object): void;
jsonSchema(schema: object, options: object): void;
jsonSchema(path: string, schema: object): void;
jsonSchema(path: string, schema: object, options: object): void;
jsonMatch(value: object): void;
jsonMatch(path: string, value: object): void;
jsonMatchStrict(value: object): void;
Expand Down
16 changes: 14 additions & 2 deletions src/exports/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,20 @@ class Have {
this._validate();
}

jsonSchema(path, value) {
typeof value === 'undefined' ? this.expect.jsonSchema.push(path) : this.expect.jsonSchemaQuery.push({ path, value });
jsonSchema(path, value, options) {
if (typeof options === 'object') {
this.expect.jsonSchemaQuery.push({ path, value, options });
} else {
if (typeof value === 'undefined') {
this.expect.jsonSchema.push({ value: path });
} else {
if (typeof path === 'object' && typeof value === 'object') {
this.expect.jsonSchema.push({ value: path, options: value });
} else {
this.expect.jsonSchemaQuery.push({ path, value });
}
}
}
this._validate();
}

Expand Down
23 changes: 23 additions & 0 deletions src/exports/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ export interface Logger {
}
export function setLogger(logger: Logger): void;

export type JsonLikeValidateFunction = (actual: any, expected: any, options?: any) => any;
export interface JsonLikeAdapter {
validate: JsonLikeValidateFunction;
}
export function setJsonLikeAdapter(adapter: JsonLikeAdapter): void;

export type GetMatchingRulesFunction = (data: any, path: any, rules: any) => any;
export type GetRawValueFunction = (data: any) => any;
export type JsonMatchValidateFunction = (actual: any, expected: any, rules: any, path: any, strict: any) => any;
export interface JsonMatchAdapter {
getMatchingRules: GetMatchingRulesFunction;
getRawValue: GetRawValueFunction;
validate: JsonMatchValidateFunction;
}
export function setJsonMatchAdapter(adapter: JsonMatchAdapter): void;

export type JsonSchemaValidateFunction = (schema: any, target: any, options?: any) => any;
export interface JsonSchemaAdapter {
validate: JsonSchemaValidateFunction;
}
export function setJsonSchemaAdapter(adapter: JsonSchemaAdapter): void;


export interface Strategy {
starts?: string;
ends?: string;
Expand Down
15 changes: 15 additions & 0 deletions src/exports/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const config = require('../config');
const logger = require('../plugins/logger');
const jsonLike = require('../plugins/json.like');
const jsonMatch = require('../plugins/json.match');
const jsonSchema = require('../plugins/json.schema');

const settings = {

Expand All @@ -11,6 +14,18 @@ const settings = {
logger.setAdapter(lgr);
},

setJsonLikeAdapter(adapter) {
jsonLike.setAdapter(adapter);
},

setJsonMatchAdapter(adapter) {
jsonMatch.setAdapter(adapter);
},

setJsonSchemaAdapter(adapter) {
jsonSchema.setAdapter(adapter);
},

setAssertHandlerStrategy(strategy) {
config.strategy.assert.handler = strategy;
},
Expand Down
2 changes: 2 additions & 0 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ declare class Spec {
* @see https://pactumjs.github.io/#/response-validation?id=expectjsonschema
*/
expectJsonSchema(schema: object): Spec;
expectJsonSchema(schema: object, options: object): Spec;
expectJsonSchema(path: string, schema: object): Spec;
expectJsonSchema(path: string, schema: object, options: object): Spec;

/**
* expects the json to match with value
Expand Down
16 changes: 14 additions & 2 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,20 @@ class Spec {
}
expectJsonLikeAt(...args) { return this.expectJsonLike(...args); }

expectJsonSchema(path, value) {
typeof value === 'undefined' ? this._expect.jsonSchema.push(path) : this._expect.jsonSchemaQuery.push({ path, value });
expectJsonSchema(path, value, options) {
if (typeof options === 'object') {
this._expect.jsonSchemaQuery.push({ path, value, options });
} else {
if (typeof value === 'undefined') {
this._expect.jsonSchema.push({ value: path });
} else {
if (typeof path === 'object' && typeof value === 'object') {
this._expect.jsonSchema.push({ value: path, options: value });
} else {
this._expect.jsonSchemaQuery.push({ path, value });
}
}
}
return this;
}
expectJsonSchemaAt(...args) { return this.expectJsonSchema(...args); }
Expand Down
4 changes: 2 additions & 2 deletions src/models/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Expect {
_validateJsonSchema(response) {
this.jsonSchema = processor.processData(this.jsonSchema);
for (let i = 0; i < this.jsonSchema.length; i++) {
const errors = jsv.validate(this.jsonSchema[i], response.json);
const errors = jsv.validate(this.jsonSchema[i].value, response.json, this.jsonSchema[i].options);
if (errors) {
this.fail(`Response doesn't match with JSON schema - ${errors}`);
}
Expand All @@ -258,7 +258,7 @@ class Expect {
for (let i = 0; i < this.jsonSchemaQuery.length; i++) {
const jQ = this.jsonSchemaQuery[i];
const value = jqy(jQ.path, { data: response.json }).value;
const errors = jsv.validate(jQ.value, value);
const errors = jsv.validate(jQ.value, value, jQ.options);
if (errors) {
this.fail(`Response doesn't match with JSON schema at ${jQ.path}: \n ${JSON.stringify(errors, null, 2)}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/json.schema.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const BasePlugin = require('./plugin.base');

class JsonSchemaValidator extends BasePlugin {
validate(schema, target) {
return this.adapter.validate(schema, target);
validate(schema, target, options) {
return this.adapter.validate(schema, target, options);
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/component/bdd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,39 @@ describe('BDD', () => {
expect(response).to.have.jsonSchema({ properties: { name: { type: 'string' } } });
});

it('should return a valid schema with options', async () => {
expect(response).to.have.jsonSchema(
{
properties: {
name: {
type: 'string',
format: 'only-snow'
}
}
},
{
formats: {
'only-snow': /^snow$/
}
}
);
});

it('should return a valid schema at', async () => {
expect(response).to.have.jsonSchema('.', { properties: { name: { type: 'string' } } });
});

it('should return a valid schema at with options', async () => {
expect(response).to.have.jsonSchema('.',
{ properties: { name: { type: 'string', format: 'only-snow' } } },
{
formats: {
'only-snow': /^snow$/
}
}
);
});

it('should return a match', async () => {
expect(response).to.have.jsonMatch(like({ name: 'snow' }));
});
Expand Down
33 changes: 33 additions & 0 deletions test/component/expects.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1028,4 +1028,37 @@ describe('Expects', () => {
expect(err).not.undefined;
});

it('json schema with options', async () => {
await pactum.spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectJsonSchema({
type: 'object',
properties: {
method: {
type: 'string',
format: 'only-get'
}
}
}, {
formats: {
'only-get': /^GET$/
}
});
});

it('json schema at with options', async () => {
await pactum.spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectJsonSchema('method', {
type: 'string',
format: 'only-get'
}, {
formats: {
'only-get': /^GET$/
}
});
});

});
12 changes: 12 additions & 0 deletions test/unit/settings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ describe('Settings', () => {
settings.setLogger(logger);
});

it('setJsonLikeAdapter', () => {
settings.setJsonLikeAdapter(require('../../src/adapters/json.like'));
});

it('setJsonMatchAdapter', () => {
settings.setJsonMatchAdapter(require('../../src/adapters/json.match'));
});

it('setJsonSchemaAdapter', () => {
settings.setJsonSchemaAdapter(require('../../src/adapters/json.schema'));
});

after(() => {
settings.setSnapshotDirectoryPath('.pactum/snapshots');
});
Expand Down

0 comments on commit 81f9db6

Please sign in to comment.