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

xhr-mocklet

v1.2.3

Published

Utility for mocking XMLHttpRequests in the browser or nodejs.

Downloads

261,392

Readme

XMLHttpRequest mocking

Build Status MIT NPM Version

Utility for mocking XMLHttpRequests both in the browser and nodejs. It's primary use case is for unit tests, allowing you to respond with mock responses, trigger timeouts, etc.

This library comes with complete TypeScript declaration files.

Installation

# NPM
npm install --save --dev xhr-mocklet

# Or via yarn:
yarn add --dev xhr-mocklet

Usage

const mock = require('xhr-mocklet');

// Replace the real XHR object with the mock XHR object
mock.setup();

// Mock a response for all POST requests to http://localhost/api/user
mock.post('http://localhost/api/user', (req, res) => {
  return res
    .status(201)
    .header('Content-Type', 'application/json')
    .body({
      lastName: 'John',
      firstName: 'Smith'
    });
});

// Restore the original XHR object when all your tests are done.
mock.teardown();

Simulating an error

Simply return null from your response handler:

mock.post('http://localhost/foo', (req, res) => null);

Simulate a timeout

mock.post('http://localhost/foo', (req, res) => res.timeout(true));

Use mocked XMLHttpRequest

You can even use a mocked XMLHttpReqeuest instance to create Requests:

// Create an instance of the (mock) XHR object and use as per normal
const xhr = new XMLHttpRequest();

xhr.onreadystatechange = () => {
  if (xhr.readyState === xhr.DONE) {
    // when you're finished put the real XHR object back
    mock.teardown();
  }
}

API

Builder

| Method | Description | |---|---| | setup() | Replace the global XMLHttpRequest object with the MockXMLHttpRequest. | | teardown() | Restore the global XMLHttpRequest object to its original state. | | reset() | Remove all request handlers. | | get(url: string \| regex, callback) | Create GET mock response for a specific url. | | post(url: string \| regex, callback) | Create POST mock response for a specific url. | | put(url: string \| regex, callback) | Create PUT mock response for a specific url. | | patch(url: string \| regex, callback) | Create PATCH mock response for a specific url. | | delete(url: string \| regex, callback) | Create DELETE mock response for a specific url. | | mock(callback) | Register mock response for every request. |

MockXMLHttpRequest

The api is practically similar to the native XMLHttpRequest.

MockRequest

| Method | Description | |---|---| | method(): string | Get the request method. | | url(): string | Get the request URL. | | query(): object | Get the parsed query part of the request URL. | | header(name: string): string | Get a request header. | | headers(): object | Get all request headers. | | body(): string | Get the request body. |

MockResponse

| Method | Description | |---|---| | status(): number | Get the response status. | | status(code: number) | Set the response status. | | header(name: string): string | Get a response header. | | header(name: string, value: string) | Set a response header. | | headers(): object | Get response headers. | | headers(headers: obejct) | Set response headers. | | body(): string | Get response body. | | body(body: string) | Set response body. | | timeout(): boolean \| number | Get weather the response will trigger a timeout. | | timeout(ms: boolean \| number) | Set a timeout, otherwise default to the value set on the XHR object. | | progress(loaded: number, total: number, lengthComputable: boolean) | Trigger progress event. Pass in loaded size, total size and if event is lengthComputable. |

Special Thanks

Special thanks goes to James Newell for his xhr-mock library. xhr-mocklet started out as a fork of his work.