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

automocker

v0.2.0

Published

A lightweight TS Mocking framework for unit testing

Downloads

1,893

Readme

automocker

npm downloads buddy pipeline

A lightweight TS class mocking module to supplement your testing framework.

Why automocker?


If you have been writing jest tests for a while you know the test setup, especially in a complex project, can be very tedious. If you use an IoC framework like NestJS to structure your application quite often you would have class dependency hierarchies spanning a few levels where you have class A depending on classes B, C, and D in its constructor (allowing the framework to do its job). What's more, classes B, C, D often themselves depend on others making it more complicated to easily stub their behavior.

Being in the NodeJS domain you are probably already using one of the mainstream testing frameworks Jest or Mocha being the two most famous. These frameworks would usually provided a behavior mocking facility ,as is the case with jest, or suggest plugging in a third party module like Sinon to help with the mocking/stubbing. If you have explored any of these in the context of stubbing ES6 class behavior you would know the options are not so intuitive. :scream:

Other, more traditional, platforms like Java and C# make it a lot easier to create Mock instances of dependency classes with 1-2 lines of code (or with the help of decorators/annotations).

Automocker is a supplementary module to your JS/TS test framework of choice which aims to make class mocking a breeze when dependency injection is used.

How to use automocker?

That's easy... :palm_tree: :relaxed:

import { AutoMocker } from '../src'
import { FxRateDataSource, SupportedCurrencyPairs, RealTimeExchangeRateProvider } from './domain/FxRateDataSource'


describe('RealTimeExchangeRateProvider', () => {
  const mocker = AutoMocker.createJestMocker(jest)
  const fxRateDataSource = mocker.createMockInstance(FxRateDataSource)
  const realTimeExchangeRateProvider = new RealTimeExchangeRateProvider(fxRateDataSource)

  it('should return the correct rate when fetchFxRate called', async () => {
    const rawExchangeRate = 1
    fxRateDataSource.fetchFxRate.mockResolvedValue(rawExchangeRate)

    const rate = await realTimeExchangeRateProvider.fetchFxRate(SupportedCurrencyPairs.USD_EUR)

    expect(rate).toEqual(rawExchangeRate * 1.02)
  })
})

Check out the example/ folder for a deeper dive.