npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-fetch-response-matchers

v1.1.47

Published

Chai asserts for node-fetch response promise, make your tests with http more declarative

Downloads

369

Readme

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 function | params | description | | ---------------------|----------| ---------------------------------| | 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 function | params | description | | ----------------------|-------------------| -------------------------------------------------------------| | 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 function | params | description | | ----------------|-------------------------| --------------------------------------------------------------------------------| | 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 function | params | description | | -------------------|----------------------------| --------------------------------------------------------------------------| | 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 function | params | | ------------------------------|-------------| | cacheControlMustRevalidate() | () | | cacheControlNoCache() | () | | cacheControlNoStore() | () | | cacheControlNoTransform() | () | | cacheControlPublic() | () | | cacheControlPrivate() | () | | cacheControlProxyMaxRevalidate| () | | cacheControlmMaxAge() | (age-in-sec)| | cacheControlSMaxAge() | (age-in-sec)|