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

@giancosta86/worker-mock

v1.0.0

Published

Minimalist Worker mocks in TypeScript

Downloads

15

Readme

worker-mock

Minimalist Worker mocks in TypeScript

GitHub CI npm version MIT License

worker-mock is a TypeScript library working in conjunction with worker-facade to easily test Worker logic.

Overview

Installation

The package on NPM is:

@giancosta86/worker-mock

which should most often be a dev dependency.

The public API entirely resides in the root package index, so one shouldn't reference specific modules.

worker-facade

Since the WorkerFacade interface should be referenced by clients in order to be able to plug WorkerMock when running tests, you'll most often want to add the worker-facade peer library as a non-dev dependency:

@giancosta86/worker-facade

Matchers

To use the matchers provided by this library, one needs to:

  1. Add the following attribute to the object exported by jest.config.js:

    setupFilesAfterEnv: ["@giancosta86/worker-mock/dist/all"],
  2. Add this line to a global.d.ts file within the project root directory:

    import "@giancosta86/worker-mock";
  3. Add "./global.d.ts" to the include array in tsconfig.json

Usage

Basics

After installing worker-facade as a non-dev dependency, you should:

  1. Implement the body of the worker as a RequestListener<TRequest, TResponse> - a function type provided by worker-facade:

    export const yourRequestListener: RequestListener<TRequest, TResponse> = (
      request,
      sendResponse
    ) => {
      //Here, process the request and
      //call sendResponse() for each message
      //to be sent to the client
    };

    Please, note: this function must NOT reside in the worker script - but in a dedicated module instead.

  2. In the worker script, just import RequestListener and yourRequestListener, then add the line:

    RequestListener.register(self, yourRequestListener);
  3. Every software component that needs to exchange messages with the worker should not depend on Worker - but on the WorkerFacade<TRequest> interface, which includes just the subset of methods and events dedicated to message passing:

    function f(worker: WorkerFacade<TRequest>): void {
      //Do some stuff, then send a request,
      //which must be of type TRequest
    
      worker.postMessage({
        alpha: 90,
        beta: 100
      });
    }
  4. In tests, the Worker logic can be plugged into clients by importing yourRequestListener and wrapping it into a WorkerMock:

    WorkerMock.create(yourRequestListener);

    because WorkerMock actually implements WorkerFacade

Matchers

worker-mock also provides useful extensions to Jest's expect():

  • expect(eventListenerFunction).toHaveBeenCalledWithMessageEvents([array of message objects]):

    • the argument of expect() must be a mock function created via jest.fn() and registered via:

      workerFacade.addListener("message", eventListenerFunction);
    • the argument of the matcher but be an array of message objects - that is, the data fields of the MessageEvent instances actually received by the listener

    Please, note: the arrays of actual and expected messages are compared according to deep structural equality - that is, by comparing their JSON strings, so as to support arbitrarily-nested message structures

  • expect(eventListenerFunction).toHaveBeenCalledWithErrorEvents([array of error message strings]):

    • the argument of expect() must be a mock function created via jest.fn() and registered via:

      workerFacade.addListener("error", eventListenerFunction);
    • the argument of the matcher must be an array of error message strings - that is, the message fields of the ErrorEvent instances actually received by the listener

Further references