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

chai-nock

v1.3.0

Published

Extends Chai with assertions for the Nock Http mocking framework

Downloads

10,634

Readme

Chai Assertions for Nock

Build Status Coverage Status npm version

Nock Chai extends Chai with a language for asserting facts about Nock.

Instead of manually wiring up your expectations to intercepting a nocked request:

const nockedRequest = nock('http://some-url');

nockedRequest.on('request', function(req, interceptor, body) {
  expect(body).to.deep.equal({ hello: 'world' });
});

you can write code that expresses what you really mean:

return expect(nock('http://some-url')).to.have.been.requestedWith({
  hello: 'world'
});

Installation

npm install chai-nock

Then add to your test setup:

const chai = require('chai');
const chaiNock = require('chai-nock');

chai.use(chaiNock);

Assertions

requested

Asserts that a request has been made to the nock.

it('requested', () => {
  const requestNock = nock('http://bbc.co.uk')
    .get('/')
    .reply(200);

  request({
    uri: 'http://bbc.co.uk',
  });

  return expect(requestNock).to.have.been.requested;
});

requestedWith(body)

Asserts that a request has been made to the nock with a body that exactly matches the object provided.

it('requestedWith', () => {
  const requestNock = nock('http://bbc.co.uk')
    .get('/')
    .reply(200);

  request({
    json: true,
    uri: 'http://bbc.co.uk',
    body: {
      hello: 'world'
    }
  });

  return expect(requestNock).to.have.been.requestedWith({ hello: 'world' });
});

requestedWithHeaders(headers)

Asserts that a request has been made to the nock with headers that exactly match the object provided.

it('requestedWithHeaders', () => {
  const requestNock = nock('http://bbc.co.uk')
    .get('/')
    .reply(200);

  request({
    json: true,
    uri: 'http://bbc.co.uk',
    headers: {
      myHeader: 'myHeaderValue'
    }
  });

  return expect(requestNock).to.have.been.requestedWithHeaders({
    host: 'bbc.co.uk',
    accept: 'application/json',
    myHeader: 'myHeaderValue'
  });
});

requestedWithHeadersMatch(partialHeaders)

Asserts that a request has been made to the nock with headers that contain the key/value pairs in the object provided.

it('requestedWithHeadersMatch', () => {
  const requestNock = nock('http://bbc.co.uk')
    .get('/')
    .reply(200);

  request({
    json: true,
    uri: 'http://bbc.co.uk',
    headers: {
      myHeader: 'myHeaderValue',
      otherHeader: 'otherHeaderValue'
    }
  });

  return expect(requestNock).to.have.been.requestedWithHeadersMatch({
    myHeader: 'myHeaderValue'
  });
});

Setting a Timeout

  • By default, a timeout of 2 seconds is applied to assertions on nock requests. This means that if nock has not intercepted the request within the set time, the assertion will be false
  • You can set a custom global timeout by calling setTimeout on the chaiNock object:
const chaiNock = require('chai-nock');

chai.use(chaiNock);
// Set a timeout of 10 seconds
chaiNock.setTimeout(10000);
  • WARNING: If not set already, the test timeout must be greater than that of chaiNock!
jest.setTimeout(12000);

Usage

const { expect } = require('chai');
const nock = require('nock');
const request = require('request-promise-native');

describe('example', () => {
  it('test', () => {
    const requestNock = nock('http://bbc.co.uk')
    .get('/')
    .reply(200);

    request({
      json: true,
      uri: 'http://bbc.co.uk',
      body: {
        hello: 'world'
      }
    });

    return expect(requestNock).to.have.been.requestedWith({ hello: 'world' });
  });
});