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

sinon-mock-server

v0.4.1

Published

Better fake mock server for sinon

Downloads

43

Readme

sinon-mock-server Build Status Coverage Status

A more elegant mock server based on sinon fake server

npm install sinon-mock-server --save-dev

Example

import mockServer from 'sinon-mock-server'
import myModule from 'my-module'
import chai from 'chai'
var expect = chai.expect

describe('api test', function() {
  var server
  var endpoint
  var fetchPromise
  beforeEach(function() {
    server = mockServer()
    endpoint = server.get('/api/books')
    fetchPromise = myModule.fetchAllBooks()
  })

  afterEach(function () {
    server.restore()
  })

  describe('when call successful', function() {
    var books = [{
      id: 1,
      title: 'Moby dick'
    }]

    beforeEach(function() {
      endpoint.resolves(200, books)
      return fetchPromise
    })

    it('exposes the books', function() {
      expect(mymodule.books).to.eql(books)
    })
  })

  describe('when server fails', function() {
    beforeEach(function() {
      endpoint.rejects(500, {})
      return fetchPromise.catch(function () {
        //silence, fail is expected
      })
    })

    it('exposes the Error', function() {
      expect(mymodule.loadBooksFailed).to.eql(true)
    })
  })
})

methods

restore

server.restore()

restores the server

verbs

The server will have methods for the following verbs: get, post, put, patch, delete, head, options which can be used in the following way:

server.post(url, [requiredBody], [requiredHeaders])

server.post('/api/books', {
  bodyParam: 3
}, {
  'Content-Type': 'application/json'
})

For non standard HTTP verbs use server.use(method, url, [requiredBody], [requiredHeaders])

Url, requiredBody and requiredHeaders will all be wrapped in sinon.match

If you don't want this use server.post.strict instead. You can still use sinon matchers as you please on some params like this:

server.post.strict(sinon.match('books'), {
  bodyParam: 3
}, {
  'Content-Type': sinon.match('application/json')
})

Url also supports regex matching:

server.post(/books/)

These methods will return a sinon stub that you can perform normal sinon assertions on, like:

var endpoint = server.post('/books').resolves({})
mymodule.createBook('new book').then(function () {
  sinon.assert.calledOnce(endpoint)
  sinon.assert.calledWithMatch(endpoint, 'POST', '/books', {
    title: 'new Book'
  }, {
    'accept': sinon.match('json')
  })
})

The stub is called like this:

stub(method, url, requestBody, requestHeaders)

Headers names are normalized (lowercased) before being called.

The stub also have the methods resolves and rejects on them that you can use to define success and failure responses.

They take these parameters:

resolves(responseBody) // default status 200
resolves(responseBody, responseHeaders) // default status 200
resolves(statusCode, responseBody)
resolves(statusCode, responseBody, responseHeaders)

// same with rejects but default status is 500

If the body is an array or an object it will automatically be serialized to JSON and the header content-type: application/json; charset=utf-8 will be set, unless the content-type header is already present in the responseHeaders.

For incoming requests the request body is parsed to JSON if the content-type in the request headers contains application/json.

catch all 404

If an url is called and you don't have an matching endpoint defined the server will throw an error GET /api/not-existing: No such endpoint registered.

This is to help with testing. If you want to respond with something for all urls then register a catch all endpoint:

server.any(/.*/).rejects(404, 'Route not found')

Be aware that routes are matched in order. So make sure to add this endpoint last.

Contributing

Create tests for new functionality and follow the eslint rules.

License

MIT © Martin Hansen