Skip to content

Commit

Permalink
docs: template name and file name in expect json
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep committed Apr 17, 2024
1 parent c5adbe8 commit 6e8f388
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"pactum"
]
}
72 changes: 67 additions & 5 deletions docs/api/assertions/expectJson.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ Performs deep equal of JSON objects.

```js
expectJson(json)
expectJson(path, json)
expectJson(template-name)
expectJson(file-path)
expectJson(file-name)
expectJson(json-path, json)
```

## Usage

### ✅ Correct Usage

```js
```js
await spec()
.get('api/health')
.expectJson({
.expectJson({
message: 'OK'
});
```
Expand All @@ -33,7 +36,15 @@ await spec()

Response json body.

#### > path (string)
#### > template-name (string)

name of the data template to use.

#### > file-name (string)

name of the json file to use.

#### > json-path (string)

Json path. See [json-query](https://www.npmjs.com/package/json-query) for more usage details.

Expand Down Expand Up @@ -70,4 +81,55 @@ await spec()
.get('https://reqres.in/api/users/1')
.expectJson('data.first_name', 'George')
.expectJson('data.last_name', 'Bluth');
```
```

### Using data template

```js
const { spec, stash } = require('pactum');

stash.addDataTemplate({
'FIRST_USER': {
"data": {
"id": 1,
"email": "george.bluth@reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
},
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
});

await spec()
.get('https://reqres.in/api/users/1')
.expectJson('FIRST_USER');
```

### Using file path

```js
const { spec } = require('pactum');

await spec()
.get('https://reqres.in/api/users/1')
.expectJson('./data/user.json');
```

#### Using file name

```js
const { spec } = require('pactum');

await spec()
.get('https://reqres.in/api/users/1')
.expectJson('user.json') // searches for the file inside the data folder
.expectStatus(201);
```

## See Also

- [setDataDirectory](/api/settings/setDataDirectory)
55 changes: 51 additions & 4 deletions docs/api/assertions/expectJsonLike.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ Performs partial equal of JSON objects.

```js
expectJsonLike(json)
expectJson(template-name)
expectJson(file-path)
expectJson(file-name)
expectJsonLike(path, json)
```

## Usage

### ✅ Correct Usage

```js
```js
await spec()
.get('api/health')
.expectJsonLike({
.expectJsonLike({
message: 'OK'
});
```
Expand Down Expand Up @@ -61,6 +64,49 @@ await spec()
});
```

### Using data template

```js
const { spec, stash } = require('pactum');

stash.addDataTemplate({
'FIRST_USER': {
"data": {
"id": 1,
"email": "george.bluth@reqres.in",
"first_name": "George",
"last_name": "Bluth",
"avatar": "https://reqres.in/img/faces/1-image.jpg"
}
}
});

await spec()
.get('https://reqres.in/api/users/1')
.expectJsonLike('FIRST_USER');
```

### Using file path

```js
const { spec } = require('pactum');

await spec()
.get('https://reqres.in/api/users/1')
.expectJsonLike('./data/user.json');
```

#### Using file name

```js
const { spec } = require('pactum');

await spec()
.post('https://reqres.in/api/users')
.expectJsonLike('user.json') // searches for the file inside the data folder
.expectStatus(201);
```

### Regular Expressions

```js
Expand All @@ -79,7 +125,7 @@ await spec()

### Assert Expressions

Assert Expressions helps to run custom JavaScript code on a JSON that performs user defined assertions.
Assert Expressions helps to run custom JavaScript code on a JSON that performs user defined assertions.

- Expression should contain `$V` to represent current value.
- Expression should be a valid JavaScript code.
Expand Down Expand Up @@ -121,4 +167,5 @@ await spec()

## See Also

- [Assert Handlers](/api/handlers/addAssertHandler)
- [Assert Handlers](/api/handlers/addAssertHandler)
- [setDataDirectory](/api/settings/setDataDirectory)
16 changes: 12 additions & 4 deletions docs/api/assertions/expectJsonMatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ Assert a JSON using a set of matchers.

```js
expectJsonMatch(json)
expectJson(template-name)
expectJson(file-path)
expectJson(file-name)
expectJsonMatch(path, json)
```

## Usage

#### ✅ Correct Usage

```js
```js
await spec()
.get('api/users/1')
.expectJsonMatch({
.expectJsonMatch({
id: like(1)
});
```
Expand All @@ -41,7 +44,7 @@ Json path. See [json-query](https://www.npmjs.com/package/json-query) for more u

## Examples

### Partial deep equal
### Partial deep equal

```js
const { spec } = require('pactum');
Expand All @@ -65,4 +68,9 @@ const { spec } = require('pactum');
await spec()
.get('https://reqres.in/api/users/1')
.expectJsonMatch('data.first_name', like('George'));
```
```

## See Also

- [Matching](/guides/matching)
- [setDataDirectory](/api/settings/setDataDirectory)
16 changes: 12 additions & 4 deletions docs/api/assertions/expectJsonMatchStrict.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ Assert a JSON using a set of strict matchers.

```js
expectJsonMatchStrict(json)
expectJson(template-name)
expectJson(file-path)
expectJson(file-name)
expectJsonMatchStrict(path, json)
```

## Usage

#### ✅ Correct Usage

```js
```js
await spec()
.get('api/users/1')
.expectJsonMatchStrict({
.expectJsonMatchStrict({
id: like(1)
});
```
Expand All @@ -41,7 +44,7 @@ Json path. See [json-query](https://www.npmjs.com/package/json-query) for more u

## Examples

### Partial deep equal
### Partial deep equal

```js
const { spec } = require('pactum');
Expand All @@ -65,4 +68,9 @@ const { spec } = require('pactum');
await spec()
.get('https://reqres.in/api/users/1')
.expectJsonMatchStrict('data.first_name', like('George'));
```
```

## See Also

- [Matching](/guides/matching)
- [setDataDirectory](/api/settings/setDataDirectory)
11 changes: 9 additions & 2 deletions docs/api/assertions/expectJsonSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ Assert based on JSON schema.

```js
expectJsonSchema(schema)
expectJson(template-name)
expectJson(file-path)
expectJson(file-name)
expectJsonSchema(path, schema)
```

## Usage

### ✅ Correct Usage

```js
```js
await spec()
.get('api/users/1')
.expectJsonSchema({
Expand Down Expand Up @@ -66,4 +69,8 @@ await spec()
.expectJsonMatch('data', {
"type": "object"
});
```
```

## See Also

- [setDataDirectory](/api/settings/setDataDirectory)

0 comments on commit 6e8f388

Please sign in to comment.