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

@arbendium/mock-esm

v2.2.1

Published

A small mocking library for Node ESM based codebase

Downloads

9

Readme

A small mocking library for Node ESM based codebase

Warning: This library uses customization hooks which, at the time of writing, is in a "Release candidate" stage.

Usage

// Instead of these two lines, the following Node CLI option would also work if the application is run in the package directory:
// --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("@arbendium/mock-esm/loader.js", pathToFileURL("./"));'
import { register } from "node:module";
register("@arbendium/mock-esm/loader", import.meta.url);

import mock from '@arbendium/mock-esm';

const mockedConfig = { listen: { port: 8080 } }

const { load, cleanup } = await mock(import.meta.url, {
	'./config.js': {
		default: mockedConfig
	}
});

const app = await load('./app.js');
cleanup();

API

mock(entryModuleUrl, mocks)

Initialize mock context.

  • entryModuleUrl - the root module url from which relative module specifiers are derived, usually import.meta.url
  • mocks - module specifier -> exports maps. Exports are always identifier -> value maps, even for CommonJS modules (since all mocks are exposed as ESM). Use default identifier for CommonJS exports.

load(specifier)

Load module in given mock context and given specifier relative to entry module.

cleanup()

Delete mocked modules from internal map for garbage collection. This is usually safe to call right after all needed modules are loaded, except when there are further dynamic imports of mocked modules. In this case, these imports will fail with exception.

Limitations/notes:

  • Modules imported from CommoonJS or built-in modules cannot be mocked because Node does not use custom loader for these.
  • Every mock call creates new namespace in module cache, which means all modules reexecuted, except modules imported from unmocked CommonJS & built-in modules. This is desired behaviour for me. If you need to cache certain modules, import them separately and pass as mocks.
  • The library does not account for module specifiers with query parameters. I guess it's easy to fix but I have no need for this at the moment.