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

fibble

v1.2.3

Published

Small library to insert test doubles into your tests

Downloads

101

Readme

fibble

Fibble is a small library to insert test doubles into your test. It is heavily inspired by quibble but targeted at es modules only. It was also made to support pnpm which creates a slightly different layout in the node_modules folder than other package managers and is thus built independent of resolution algorithm.

api

There are two functions exported from fibble:

  • replace(path, replacement) - Replaces the module at path with the replacement.

    • path (string) - A path to the module to replace. The path should be relative from the file you are calling replace in.
    • replacement (object) - The object to replace the replacement to do. The object should contain key that you import in the file you want to test. If you want to stub the default export, name a key default.
    • returns - Promise. The replacement is async so you should await its' completion.
  • reset() - Removes all registered replacements. All imports made after this call will use their native dependencies.

usage

setup

There are two modes available depending on the version of node you run. This is because node has changed their API for loaders.

modern mode

Use this with node version 18.19.0 and above.

node --experimental-import-meta-resolve --import fibble/register [my app]

legacy mode

Use this with node version 18.18.x and below.

node --experimental-import-meta-resolve --loader fibble/loader [my app]

non supported versons

Fibble does not support node version 20.0.0 - 20.5.x.

mocha example

This example uses mocha/chai but feel free to use in your favorite test runner.

import { describe, beforeEach, it } from 'mocha'
import { expect } from 'chai'
import { replace, reset } from 'fibble'

describe('my module', function(){
  let subject

  beforeEach(async () => {
    await replace ('./path/to/module/to/replace.js', { someProperty: 'some replacement value' })

    subject = await import('./module-to-test.js').default // or the property you want to test
  })

  afterEach(reset) // clear all replacements

  it('contains the stubbed stuff', function() {
    expect(subject(), ...) // to work with the replacemed module
  })
})

Setup mocha like this:

// .mocharc.cjs
module.exports = {
  'node-option': ['experimental-import-meta-resolve', 'import=fibble/register'],
}