Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

testdouble-nock

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

testdouble-nock

testdouble.js extension to add support for nock request mocking

  • 0.2.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

testdouble-nock

Support for testdouble.js for users of Nock!

Installation

$ npm i -D testdouble-nock

Then, from a test helper, invoke the module and pass in both td and nock, like so:

global.td = require('testdouble')
global.nock = require('nock')
require('testdouble-nock')(td, nock)

We'll want to clear out our stubbings after each test, so add something like this to your test runner's beforeEach() hook:

beforeEach: function () {
  td.reset.onNextReset(() => nock.cleanAll())
},
afterEach: function () {
  td.reset() // <-- you should already be doing this!
}

For an example of a helper that sets up testdouble.js, testdouble-nock, and ensures td.reset() is called after each test, look at test/helper.js in this repo.

Usage

When you invoke testdouble-nock, it adds a new top-level td.api() function that allows you to mock an external API by exposing common HTTP methods as mocked methods. Here's an example:

describe('My API test', () => {
  it('should return a 200 for status', (done) => {
    const myApi = td.api('http://www.my-api.com')

    td.when(myApi.get('/status')).thenReturn('All good here!')

    request.get('http://www.my-api.com/status', (req, res, body) => {
      assert.equals(body, 'All good here!')

      done()
    })
  })
})

You can also verify that a call has taken place using td.verify:

describe('My API test', () => {
  it('should return a 200 for status', (done) => {
    const myApi = td.api('http://www.my-api.com')

    request.get('http://www.my-api.com/status', (req, res, body) => {
      td.verify(myApi.get('/status'))

      done()
    })
  })
})

FAQs

Package last updated on 28 Jun 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc