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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@testingrequired/ioc

v0.1.0

Published

A simple dependency injection implementation

Readme

Build Status

@testingrequired/ioc

A simple dependency injection implementation.

Installation

$ npm i @testingrequired/ioc

Getting Started

Components

Define components with the component decorator.

Example

import { component, register } from "@testingrequired/ioc";

@component
class Component {}

Register Example

Components can also be defined using the register function for more control.

import { component, register, instance } from "@testingrequired/ioc";

class RandomEachTime {}

register(
  RandomEachTime,
  () => {
    const component = new RandomEachTime();
    component.value = Math.floor(Math.random() * (10000 - 0) + 0);
    return component;
  },
  { lifetime: instance }
);

Dependencies

The inject function defines a dependency to a component.

import { component, inject, resolve } from "@testingrequired/ioc";

@component
class Child {}

@component
class Parent {
  @inject(Child) child;
}

const parent = resolve(Parent);

parent.child instanceof Child === true;

Example

See the working example

Testing

A container's register function can be passed a custom factory even after dependency resolution. This factory can return a mock/spy/stub.

import { makeContainer } from "@testingrequired/ioc";

const container = makeContainer();

@container.component
class Child {}

class Parent {
  @container.inject(Child) child;
}

class Sibling {}

container.register(Child, () => new Sibling());

const parent = containter.resolve(Parent);

parent.child instanceof Sibling === true;

Development

build

Build project in to dist

$ npm run build

watch

Build project when files change.

$ npm run build -- -w

test

Run tests

npm run test

example

Run example app.

This will use current build in dist.

$ npm run example

API

register(componentKey, [factory, [options = {}]])

Register a class as a component.

import { register } from "@testingrequired/ioc";

class Foo {}

register(Foo);

component

The class, string or symbol used to identify the component.

factory

Function to be called when creating instance of component. This defaults to calling new on component.

options

lifetime

There are two lifetime options: session (one instance for every resolve) & instance (new instance on each resolve).

import { register, session, instance } from "@testingrequired/ioc";

class Singleton {}

register(Singleton, null, { lifetime: session });

class NewEveryTime {}

register(NewEveryTime, null, { lifetime: instance });

resolve(componentKey)

Return instance of component.

import { resolve } from "@testingrequired/ioc";

const foo = resolve(Foo);

component

Register a class as an injectable component.

import { component } from "@testingrequired/ioc";

@component
class Foo {}

component(options = {})

Pass options while registering a component.

import { component } from "@testingrequired/ioc";

@component({})
class Foo {}
Options
Lifetime

There are two lifetime options: session (one instance for every resolve) & instance (new instance on each resolve).

import { component, session, instance } from "@testingrequired/ioc";

@component({ lifetime: session })
class Singleton {}

// same as:

@component.session
class Singleton {}

// ---

@component({ lifetime: instance })
class NewEveryTime {}

// same as:

@component.instance
class NewEveryTime {}

inject(componentKey)

Injects initialized component.

import { inject } from "@testingrequired/ioc";

class Bar {
  @inject(Foo) foo;
}

makeContainer

Create a container to manage components.

import { makeContainer } from "@testingrequired/ioc";

const container = makeContainer();

@container.component
class Foo {}

class Bar {
  @container.inject(Foo) foo;
}