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

jesm

v0.0.15

Published

Native ESM testing and mocking, jest-free: - to mock ESM modules we are using [quibble](https://www.npmjs.com/package/quibble) - to use additional features like spies and stubs you can use [sinon](https://sinonjs.org/releases/v12.0.1/) - to use assertions

Downloads

42

Readme

Jesm

Native ESM testing and mocking, jest-free:

  • to mock ESM modules we are using quibble
  • to use additional features like spies and stubs you can use sinon
  • to use assertions you can use expect

Assumptions and limitations:

  • nodejs >= 16 is required
  • only test, it, describe, beforeAll, beforeEach, afterEach, afterAll, mockModule are supported (exported)
  • only testNamePattern, runTestsByPath flags are supported
  • only sequential running of test is supported (implicit --runInBand)
  • no describe nested in describe support
  • no coverage support
  • jesm command must be run in the root of the package

Usage

Run spec files:

yarn add --dev jesm
NODE_OPTIONS="--loader=quibble" yarn jesm *.spec.js

See math.spec.js example on how to use mockModule.

mockModule

mockModule function can mock a module in 3 ways:

  • inline providing of default export and named exports
  • lookup of .mock.js file
  • lookup of __mocks__ folder

Examples:

test('inline mock', async () => {
  // mock import
  await mockModule('./subtract.js', (a, b) => a * b, { MAGIC_NUMBER: 1 });

  // import mock, either a module importing the one mocked or importing directly the mocked one 
  const { subtract } = await import('./math.js');
  const { MAGIC_NUMBER } = await import('./subtract.js');

  const actual = subtract(2, 2);
  const expected = 4;
  expect(actual).toEqual(expected);
  expect(MAGIC_NUMBER).toEqual(666);
});

test('.mock.js file mock', async () => {
  // mock import
  await mockModule('./multiply.js');

  // import mock
  const { multiply } = await import('./math.js');
  const { MAGIC_NUMBER } = await import('./multiply.js');

  const actual = multiply(3, 3);
  const expected = 27;
  expect(actual).toEqual(expected);
  expect(MAGIC_NUMBER).toEqual(2);

  // reset mocks
  mockModule.reset();
});

test('__mocks__ folder mock', async () => {
  // mock import
  await mockModule('./divide.js');

  // import mock
  const { divide } = await import('./math.js');
  const { MAGIC_NUMBER } = await import('./divide.js');

  const actual = divide(3, 3);
  const expected = 27;
  expect(actual).toEqual(expected);
  expect(MAGIC_NUMBER).toEqual(2);

  // reset mocks
  mockModule.reset();
});

Webstorm support

Copy the .jest-run folder inside your root before running any spec with the run/debug button. Remember to properly set your NODE_ENV=xxx variable.

Notes

Thanks to Gil Tayar and its blog post here.