0.1.2 • Published 2 years ago

automocker v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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.

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago