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

nock-uri-template

v0.1.8

Published

This library provides an extension to the [nock](https://github.com/nock/nock) library to make it easy to mock urls using uri templates instead of using only regexes with nock.

Downloads

279

Readme

nock-uri-template

This library provides an extension to the nock library to make it easy to mock urls using uri templates instead of using only regexes with nock.

Installation

For NPM:

npm install --save-dev nock-uri-template

For yarn

yarn add nock-uri-template -D

URI Template

The first step is to determine the API urls you want to mock. To do so, you must use the uri template standard.

Here are some examples of uri templates:

  • /products/{id}{?category} would match
    • /products/1
    • /products/11
    • /products/1?category=true
  • /products/{type}/{category}
    • /products/tools/electric

Under the hood, nock-uri-template uses this library to match uri templates.

Mocking an api

Here is an example of how to mock an API

const scope = nockUriTemplate("http://localhost:4001")
  .get("/api/products/{id}")
  .reply(
    (params: any): Response => ({
      payload: { id: "1" }
    })
  );

The first line is used to specify the base url of the service. This base url must match the config used for your http calls in your code.

The second line specify we want to mock GET requests to the specified path.

The third line uses a function to create the return payload. The object returned by the function can have a code property for the http return code, and a payload property if there is a payload to return. If no code is provided, a 200 will be returned if there is a payload, and a 204 if there is no payload.

Limit the number of calls

Using the reply method will make the mock API reply to an unlimited number of calls. If you want to limit the number of calls the mock API will reply to, you can use replyOnce or replyTimes method and provide a maximum number.

Expectations

Whatever test runner you are using, nock-uri-template provides an object to make assertions on. Using the object returned by nockUriTemplate, you can expect on the calls made to the api. The following examples are using Jest, but they work with any runner.

Expecting on calls made using certain parameters

This would expect that one call was made to our api endpoint using the parameter id=1

expect(scope.forParams({ id: "1" }).times()).toBe(1);

We could also verify that no calls were made except the call to id=1

expect(scope.notForParams({ id: "1" }).times()).toBe(0);
expect(scope.notForParams({ id: "1" }).urls()).toEqual([]);

The two lines test the same assertion, but it's easier to debug using the urls method, because the runner will print the difference between the two arrays, which will print out the list of urls that were not supposed to be called.