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

nockback-harder

v5.0.3

Published

Wrapper that makes testing using Nock mock record/replay functionality easier. By default does not create mocks for local calls (localhost/127.0.0.1) when recording, allows and passes through local calls when replaying.

Downloads

168

Readme

nockback-harder

Wrapper that makes testing using Nock mock record/replay functionality easier. By default does not create mocks for local calls (localhost/127.0.0.1) when recording, allows and passes through local calls when replaying.

NPM Version NPM Downloads Linux Build

Install

$ npm install --save-dev nockback-harder

Example usage

Basic usage

import { NockbackHelper } from 'nockback-harder'
import nock from 'nock'

  const helper = new NockbackHelper(nock, __dirname + '/nock-fixtures')

  // This will result in calls being recorded when there are no recorded mocks yet, and mocks being replayed if there are any. 
  // It is a good mode for development (although you might use `startRecordingOverwrite` when calls you are making are changing a lot),
  // but you might want to commit code with `startLockdown` mode to ensure no unmocked calls are ever recorded.
  helper.startRecordingNew()

  await helper.nockBack('google.com-GET.json', async () => {
    // External call will be recorded
    const response = await request.get('www.google.com')
    expect(response.status).toBe(200)
    expect(response.text).toMatchSnapshot()
    
    // Local call will not be recorded. But if handler for this request makes additional external calls, they will be recorded.
    const responseLocal = await request.get('localhost:4000')
    expect(responseLocal.status).toBe(200)
    expect(responseLocal.body).toMatchSnapshot()
  })

Recording calls to other local services

import { NockbackHelper } from 'nockback-harder'
import nock from 'nock'

const OTHER_LOCAL_SERVICE_PORT = 8087

  const helper = new NockbackHelper(nock, __dirname + '/nock-fixtures')
  helper.startRecordingNew()

  await helper.nockBack('google.com-GET.json', { passthroughPortWhitelist: OTHER_LOCAL_SERVICE_PORT] }, async () => {
    // Local call will not be recorded. But calls from request handler to localhost:8087 will be recorded.
    const responseLocal = await request.get('localhost:4000')
    expect(responseLocal.status).toBe(200)
    expect(responseLocal.body).toMatchSnapshot()
  })

Executing tests in CI

import { NockbackHelper } from 'nockback-harder'
import nock from 'nock'

  const helper = new NockbackHelper(nock, __dirname + '/nock-fixtures')
  
  // This will cause test to fail when unexpected calls are being made, instead of recording them. 
  // This is the mode with which you should be committing your tests.
  helper.startLockdown()  

  await helper.nockBack('google.com-GET.json', async () => {
    /* actual test */
  })

Configuration

NockbackHelper constructor accepts following parameters:

  • nock -> instance of nock. Usually retrieved by calling import * as nock from 'nock' or 'const nock = require('nock)'
  • fixtureDirectory?: string -> base directory, relative to which path to fixtures will be resolved
  • config?: NockbackHelperConfig -> optional helper configuration

NockbackHelperConfig parameters:

  • passThroughLocalCall: boolean = true -> do not create or replay mocks for local calls (localhost or 127.0.0.1), execute actual calls instead.

nockBack execution method accepts following parameters:

  • pathToFixture: string -> relative path to a file which will be used for recording and replaying call mocks.
  • callbackOrConfig: NockbackExecutionConfig | Function -> either optional config parameter, or function that will be executed within the current Nockback context. Mocks for HTTP calls within this function (including nested calls) will be recorded/replayed to/from file specified in pathToFixture.
  • callback?: Function -> function that will be executed within the current Nockback context. Should only be set if optional config parameter is passed.

NockbackExecutionConfig parameters:

  • passthroughLocalCall?: boolean -> override for helper-wide passthrough parameter.
  • passthroughPortWhitelist? number[] -> enable creating and replaying mocks for local calls on specific ports.
  • doNotOverwrite?: boolean -> if set to true, this execution will not overwrite mocks even startRecordingOverwrite() was invoked. Used to preserve manually crafted mocks.
  • nockOptionsOverride?: NockBackOptions -> used to override Nockback options

Supported helper modes

  • startRecordingNew() -> Use recorded mocks, record if there are none, throw an error if mocks exist but none of them match request being sent
  • startRecordingOverwrite() -> Delete recorded mocks if they exist, record new mocks
  • startLockdown() -> Use recorded mocks, throw an error on missing and unused ones. Never records anything. Meant to be used for CI

Helper methods

NockbackHelper provides following helper methods:

  • expectNoPendingMocks() -> throw an error if nock instance used by helper still has recorded mocks that were never used during test execution.
  • disableExternalCalls() -> make nock instance used by helper throw an error if external call not covered by existing mocks is made.