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

expressively-mocked-fetch

v0.2.3

Published

A fetch mock that launches express as a worker_thread

Downloads

38

Readme

expressively-mocked-fetch

npm version

A fetch mock that launches express as a worker_thread to help testing.

Installation

NOTE: This library uses worker_threads, a nodejs builtin that was released with node v13.

$ npm i -D expressively-mocked-fetch

Why

Mocking fetch is annoying. I saw msw. But it didn't fit my use case. So I ended up writing a wrapper around express.js.

Develop

  1. Download this repo and install the dependencies
  2. Run npm run test

Usage

// with ava
const test = require("ava");
const createWorker = require("expressively-mocked-fetch");

test("if module is loaded and executed", async (t) => {
  const worker = await createWorker(`
// regular express.js code
app.get('/', function (req, res) {
  res.send("hello world");
});
  `);

  const res = await fetch(`http://localhost:${worker.port}`);
  const text = await res.text();
  t.assert(test === "hello world");
});

createWorker(string, object)

  • string needs to be valid Node.js JavaScript

  • object is of shape { requestCount: Number, port: Number }, where:

    • requestCount (optional, default: 1) is the amount of times a server should respond before automatically shutting itself.
    • port (optional, default: 0) is the desired port the server should launch at. For dynamic allocation by the OS, use the default value 0.
    • pauseMilliseconds (optional, default: 0) the time in milliseconds a server must delay sending an answer using connect-pause.

Changelog

0.2.3

  • Introduce optional option pauseMilliseconds that allows simulating a server's delayed response-ability.

0.2.2

  • Allow template's app to be overwritten by using let.

0.2.1

  • (Bug fix): options allows now to set individual keys without forgetting all default options.

0.2.0

  • Refactor createWorker function signature to allow an options object.
  • Add port to options that allows a user to define a port before launching a worker.

0.1.2

  • Using node.js's worker_threads now allows expressively-mocked-fetch to spawn threads without the usage of temporary files. Interface remains the same.

0.1.1

  • Add support for Content-Type: text/calendar and potentially other types.

0.1.0

  • Allow accepting text/plain and application/json.

0.0.3

  • Allow defaultCount option allows a worker to exist for more than 1 request.

0.0.2

  • Bug fix: Create new module file for each worker to allow running in parallel.

0.0.1

  • Initial release

LICENSE

See LICENSE file.