Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #139 from shrikster/master
Browse files Browse the repository at this point in the history
Added support for Using Moment.js in the nunjuck templates
  • Loading branch information
jgroom33 authored Mar 22, 2019
2 parents 613b4b7 + e6caeb9 commit 46a2904
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ requests:
method: GET
```
## Use strest file name as paramter in the tests
You can use the strest file name as a parmater in the tests .
## Use strest file name as parameter in the tests
You can use the strest file name as a parameter in the tests .
*note* that the strest suffix is removed
Expand All @@ -350,6 +350,35 @@ requests:
expect: 200
```
## Using dates and dates format
You can insert dates times plus format them using the custom [nunjucks date filter](https://www.npmjs.com/package/nunjucks-date)
under the hood its a wrapper for [momentjs](https://momentjs.com/) so all its [formatting](https://momentjs.com/docs/#/displaying/) is supported
**Usage**
You can use the date filter inside a nunjuck brackets in the request and inside the validate parts.
```yml
requests:
moment-in-request:
request:
url: https://postman-echo.com/get
method: GET
queryString:
- name: foo
value: <$ now | date('YYYY') $>
validate:
- jsonpath: content.args.foo
expect: "<$ '2019-10-10' | date('YYYY') $>"
moment-in-validate:
request:
url: https://postman-echo.com/time/format?timestamp=2019-10-10&format=YYYY
method: GET
validate:
- jsonpath: content.format
expect: "<$ '2019-10-10' | date('YYYY') $>"
```
## Response Validation
The immediate response is stored in [HAR Format](http://www.softwareishard.com/blog/har-12-spec/#response)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"line-number": "0.1.0",
"mkdirp": "^0.5.1",
"nunjucks": "^3.1.3",
"ora": "^3.0.0",
"nunjucks-date": "^1.2.0",
"ora": "^3.2.0",
"recursive-readdir": "^2.2.2",
"request-to-curl": "^0.1.1",
"sort-paths": "^1.1.1"
Expand Down Expand Up @@ -60,7 +61,7 @@
"@types/jsonpath": "^0.2.0",
"@types/node": "^10.9.4",
"@types/nunjucks": "^3.1.0",
"@types/ora": "^1.3.4",
"@types/nunjucks-date": "0.0.6",
"@types/recursive-readdir": "^2.2.0",
"jest": "^23.6.0",
"ts-jest": "^23.1.4",
Expand Down
7 changes: 6 additions & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import * as Joi from 'joi';
import * as ora from 'ora';
import ora from 'ora';
import axios from 'axios';
import * as faker from 'faker';
import { colorizeMain, colorizeCustomRed } from './handler';
Expand All @@ -12,12 +12,15 @@ import * as yaml from 'js-yaml';
import * as jsonfile from 'jsonfile'
import * as path from 'path';
import * as Ajv from 'ajv';
import * as nunjucksDate from 'nunjucks-date';

var deepEql = require("deep-eql");
var lineNumber = require('line-number');
var getLineFromPos = require('get-line-from-pos');

require('request-to-curl');

nunjucksDate.setDefaultFormat('MMMM Do YYYY, h:mm:ss a');
const nunjucksEnv = nunjucks.configure(".", {
tags: {
blockStart: '<%',
Expand All @@ -30,6 +33,8 @@ const nunjucksEnv = nunjucks.configure(".", {
throwOnUndefined: true,
autoescape: false
});

nunjucksDate.install(nunjucksEnv);
nunjucksEnv.addGlobal('Faker', function (faked: string) {
return faker.fake(`{{${faked}}}`);
})
Expand Down
20 changes: 20 additions & 0 deletions tests/success/Value/moment.strest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 2

requests:
moment-in-request:
request:
url: https://postman-echo.com/get
method: GET
queryString:
- name: foo
value: <$ now | date('YYYY') $>
validate:
- jsonpath: content.args.foo
expect: "<$ '2019-10-10' | date('YYYY') $>"
moment-in-validate:
request:
url: https://postman-echo.com/time/format?timestamp=2019-10-10&format=YYYY
method: GET
validate:
- jsonpath: content.format
expect: "<$ '2019-10-10' | date('YYYY') $>"

0 comments on commit 46a2904

Please sign in to comment.