1.1.47 • Published 7 years ago

node-fetch-response-matchers v1.1.47

Weekly downloads
228
License
SEE LICENSE IN LI...
Repository
github
Last release
7 years ago

node-fetch-response-matchers

Build Status

Chai plugin with matchers for node-fetch promise response. It helps the tests to be more declarative.

TL;DR

  • This lib gives you a declarative way to assert fetch response, Also it hides the promises and their callbacks noise:
   it('some-test', function(){
     return expect(fetch('http://localhost/')).to.be.successful()
                            .and.to.haveBodyText('foo');

   });
  • If you are not using this lib it becomes very verbose:
   it('some-test', function(done){
      fetch('http://localhost/')
         .then(res => {
            expect(res.status).to.equal(200);
            return res.text();
         }).then(text => {
            expect(text).to.equal('foo');
            done();
         })
   });

Install (for dev only - used by tests)

$ npm install --save-dev node-fetch-response-matchers

Usage example

const nodeFetchMatchers = require('node-fetch-response-matchers');
const fetch = require('node-fetch');
const chai = require('chai');

chai.use(nodeFetchMatchers);

describe('test suite', function(){
    it('http success test', function(){
        return expect(fetch('http://localhost/')).to.be.successful();
    });
    it('and', function(){
          return expect(fetch('http://localhost/')).to.be.successful()
                                                    .and.haveBodyText('foo');
    });
});

Chai native plugin

You can all use chai "not"

   it('not', function(){
      return expect(fetch('http://localhost/')).to.not.be.successful();
   });

Status matchers

   it('http success test', function(){
      return expect(fetch('http://localhost/')).to.be.successful();
   });
   it('http status assert', function(){
        return expect(fetch('http://localhost/')).to.haveStatus(500);
   });

Full status matchers list

API functionparamsdescription
successful()()Assert that the status is 200 OK
created()()Assert that the status is 201
badRequest()()Assert that the status is 400
unauthorized()()Assert that the status is 401
rejected()()Assert that the status is 403
notFound()()Assert that the status is 404
serverError()()Assert that the status is 500
serviceUnAvailable()()Assert that the status is 503
haveStatus()(status)Assert that the status is provided number argument

Body matchers

   it('have body object', () => {
     return expect(fetch('http://localhost/').to.haveBodyObject({foo: 'bar'});
   });

Full body matchers list

API functionparamsdescription
haveBodyObject()(obj)Assert equal provided object
haveBodyText()(text)Assert equal provided string text
haveBodyBuffer()(Buffer)Assert equal provided Node Buffer
haveBodyRegexpMatch()(regexp)Assert match body on regular expression
haveBodyThat()(predicate(text))Assert match body on provided function predicate on the text

Header matchers

   it('have header', () => {
     return expect(fetch('http://localhost/').to.haveHeader('connection', 'close');
   });

Headers matchers list

API functionparamsdescription
haveHeader()(name, value)Assert that response contains header by provided name and value
headerExists()(name)Assert that response contains header by provided name
haveHeaderThat()(name, predicate(value))Assert that header with given name have true on the value for a given predicate
haveHeaders()(headersMap)Assert that given key-value headers are exists in headers response

Cookie matchers

   it('have cookie', () => {
     return expect(fetch('http://localhost/').to.haveCookie('foo', 'bar');
   });

Cookie matchers list

API functionparamsdescription
haveCookieByName()(name)Assert that cookie by name is written to the response
haveCookie()(name, value)Assert that cookie by name and value is written to the response
haveCookieThat()(name, predicate(cookie))Assert that cookie by name and match given predicate on cookie properties

Cache control response matchers

   it('must-revalidate', () => {
     return expect(fetch('http://localhost/').to.have.cacheControlMustRevalidate();
   });
   it('max-age', () => {
        return expect(fetch('http://localhost/').to.have.cacheControlmMaxAge(120);
   });

cache control full matchers list

API functionparams
cacheControlMustRevalidate()()
cacheControlNoCache()()
cacheControlNoStore()()
cacheControlNoTransform()()
cacheControlPublic()()
cacheControlPrivate()()
cacheControlProxyMaxRevalidate()
cacheControlmMaxAge()(age-in-sec)
cacheControlSMaxAge()(age-in-sec)
1.1.47

7 years ago

1.1.46

7 years ago

1.1.45

7 years ago

1.1.44

7 years ago

1.1.43

7 years ago

1.1.41

7 years ago

1.1.40

7 years ago

1.1.39

7 years ago

1.1.38

7 years ago

1.1.37

7 years ago

1.1.36

7 years ago

1.1.35

7 years ago

1.1.34

7 years ago

1.1.33

7 years ago

1.1.32

7 years ago

1.1.31

7 years ago

1.1.30

7 years ago

1.1.29

7 years ago

1.1.28

7 years ago

1.1.27

7 years ago

1.1.26

7 years ago

1.1.25

7 years ago

1.1.24

7 years ago

1.1.23

7 years ago

1.1.22

7 years ago

1.1.21

7 years ago

1.1.20

7 years ago

1.1.19

7 years ago

1.1.18

7 years ago

1.1.17

7 years ago

1.1.16

7 years ago

1.1.15

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago