Skip to content

Commit

Permalink
Merge pull request #82 from pactumjs/v3
Browse files Browse the repository at this point in the history
3.0.19
  • Loading branch information
ASaiAnudeep authored Sep 5, 2021
2 parents 81f9db6 + f4191ba commit 599a108
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node-version: [10.x, 12.x, 14.x]
node-version: [10.x, 12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v2
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v2
with:
node-version: '12.x'
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm test
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
![Size](https://img.shields.io/bundlephobia/minzip/pactum)
![Platform](https://img.shields.io/node/v/pactum)

![Stars](https://img.shields.io/github/stars/pactumjs/pactum?style=social)
![Twitter](https://img.shields.io/twitter/follow/pactumjs?label=Follow&style=social)
[![Stars](https://img.shields.io/github/stars/pactumjs/pactum?style=social)](https://github.com/pactumjs/pactum/stargazers)
[![Twitter](https://img.shields.io/twitter/follow/pactumjs?label=Follow&style=social)](https://twitter.com/pactumjs)

#### REST API Testing Tool for all levels in a Test Pyramid

Expand Down Expand Up @@ -125,7 +125,7 @@ Scenario: Check Tea Pot
```js
// steps.js
const pactum = require('pactum');
const { Given, When, Then, Before } = require('cucumber');
const { Given, When, Then, Before } = require('@cucumber/cucumber');

let spec = pactum.spec();

Expand Down Expand Up @@ -180,7 +180,7 @@ Inspired from [frisby](https://docs.frisbyjs.com/) and [pact](https://docs.pact.

## Support

Like this project! Star it on [Github](https://github.com/pactumjs/pactum/stargazers). Your support means a lot to us.
Like this project! Star it on [Github](https://github.com/pactumjs/pactum/stargazers) and follow on [Twitter](https://twitter.com/pactumjs). Your support means a lot to us.

## Contributors

Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.0.18",
"version": "3.0.19",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down Expand Up @@ -59,7 +59,7 @@
"author": "Anudeep <[email protected]>",
"license": "MIT",
"dependencies": {
"@exodus/schemasafe": "^1.0.0-rc.3",
"@exodus/schemasafe": "^1.0.0-rc.4",
"deep-override": "^1.0.2",
"form-data": "^4.0.0",
"json-query": "^2.2.2",
Expand All @@ -68,7 +68,7 @@
"openapi-fuzzer-core": "^1.0.6",
"pactum-matchers": "^1.0.2",
"parse-graphql": "^1.0.0",
"phin": "^3.5.1",
"phin": "^3.6.0",
"polka": "^0.5.2"
},
"devDependencies": {
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 @@ -18,6 +18,8 @@ export interface Have {
jsonMatch(path: string, value: object): void;
jsonMatchStrict(value: object): void;
jsonMatchStrict(path: string, value: object): void;
jsonLength(value: number): void;
jsonLength(path: string, value: number): void;
responseTimeLessThan(ms: number): void;
error(err?: string | object): void;
_(handler: string, data: any): Promise<void>;
Expand Down
5 changes: 5 additions & 0 deletions src/exports/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class Have {
this._validate();
}

jsonLength(path, value) {
typeof value === 'undefined' ? this.expect.jsonLike.push(path) : this.expect.jsonLengthQuery.push({ path, value });
this._validate();
}

responseTimeLessThan(ms) {
this.expect.responseTime = ms;
this._validate();
Expand Down
6 changes: 6 additions & 0 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ declare class Spec {
expectJsonMatchStrict(value: object): Spec;
expectJsonMatchStrict(path: string, value: object): Spec;

/**
* expects the json to an array with length
*/
expectJsonLength(value: number): Spec;
expectJsonLength(path: string, value: number): Spec;

/**
* expect network errors
* @see https://pactumjs.github.io/#/response-validation?id=expecterror
Expand Down
5 changes: 5 additions & 0 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ class Spec {
return this;
}

expectJsonLength(path, value) {
typeof value === 'undefined' ? this._expect.jsonLength.push(path) : this._expect.jsonLengthQuery.push({ path, value });
return this;
}

expectError(error) {
this._expect.errors.push(error);
return this;
Expand Down
31 changes: 31 additions & 0 deletions src/models/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Expect {
this.jsonMatchStrict = [];
this.jsonMatchStrictQuery = [];
this.jsonSnapshot = [];
this.jsonLength = [];
this.jsonLengthQuery = [];
this.headers = [];
this.headerContains = [];
this.responseTime = null;
Expand All @@ -57,6 +59,8 @@ class Expect {
this._validateJsonMatchQuery(response);
this._validateJsonMatchStrict(response);
this._validateJsonMatchStrictQuery(response);
this._validateJsonLength(response);
this._validateJsonLengthQuery(response);
this._validateJsonSnapshot(response);
this._validateResponseTime(response);
this._validateErrors(response);
Expand Down Expand Up @@ -354,6 +358,33 @@ class Expect {
}
}

_validateJsonLength(response) {
this.jsonLength = processor.processData(this.jsonLength);
for (let i = 0; i < this.jsonLength.length; i++) {
const expected = this.jsonLength[i];
if (response.json && Array.isArray(response.json)) {
const actual = response.json.length;
assert.strictEqual(actual, expected, `JSON Length ${actual} !== ${expected}`);
} else {
this.fail('Response does not contain a json array');
}
}
}

_validateJsonLengthQuery(response) {
this.jsonLengthQuery = processor.processData(this.jsonLengthQuery);
for (let i = 0; i < this.jsonLengthQuery.length; i++) {
const { path, value: expected } = this.jsonLengthQuery[i];
const actualValue = jqy(path, { data: response.json }).value;
if (actualValue && Array.isArray(actualValue)) {
const actual = actualValue.length;
assert.strictEqual(actual, expected, `JSON Length ${actual} !== ${expected}`);
} else {
this.fail(`Response does not contain a json array at '${path}'`);
}
}
}

_validateResponseTime(response) {
this.responseTime = processor.processData(this.responseTime);
if (this.responseTime !== null) {
Expand Down
10 changes: 10 additions & 0 deletions src/models/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Server {
registerAllRoutes(this, this.app);
this.app.listen(config.mock.port, () => {
log.info(`Mock server is listening on port ${config.mock.port}`);
this._registerEvents();
resolve();
});
} else {
Expand Down Expand Up @@ -78,6 +79,15 @@ class Server {
}
}

_registerEvents() {
process.on('SIGTERM', () => {
if (this.app) {
log.warn('Termination Signal Received - SIGTERM');
this.stop();
}
});
}

}

/**
Expand Down
16 changes: 16 additions & 0 deletions test/component/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ function addDefaultMockHandlers() {
}
};
});
handler.addInteractionHandler('default users', () => {
return {
request: {
method: 'GET',
path: '/default/users'
},
response: {
status: 200,
body: [
{ name: 'Matt', country: 'NZ' },
{ name: 'Pete', country: 'AU' },
{ name: 'Mike', country: 'NZ' }
]
}
};
});
}

before(async () => {
Expand Down
20 changes: 20 additions & 0 deletions test/component/bdd.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ describe('BDD', () => {
ce(spec.returns('name')).equals('snow');
});

it('should not have a json with length', async () => {
let err;
try {
await expect(response).to.have.jsonLength(1);
} catch (error) {
err = error;
}
ce(err).not.undefined;
});

it('should not have a json with length at some path', async () => {
let err;
try {
await expect(response).to.have.jsonLength('path', 1);
} catch (error) {
err = error;
}
ce(err).not.undefined;
});

});

describe('BDD - AutoReportRunner Disabled', () => {
Expand Down
Loading

0 comments on commit 599a108

Please sign in to comment.