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

waterline-fakes

v1.0.1

Published

Utilities to make it easier to fake out waterline models and collections for unit testing

Downloads

5

Readme

waterline-fakes

Utilities to make it easier to fake out waterline models and collections for unit testing.

description

When unit testing controllers or services in Sails.js, it can be helpful to replace model and collection interaction results with fake results, so that you're only testing the one layer of your code that you are focusing on. This package exposes some factories to generate objects and methods that would take the place of normal Sails model or collection methods.

install

npm i --save-dev waterline-fakes

api

var waterlineFakes = require('waterline-fakes');
/* => {
  fakeWaterlineChainMethod: function () {},
  fakeWaterlineModel: function () {}
} */

using fakeWaterlineChainMethod(options)

Used to replace a method on a Collection, that would normally be followed by a call to .exec(function (err, results)) {}. A normal block of code that you would want to test may look something like this:

// GET /pets
list: function (req, res) {
  Pets.find(req.query).exec(function (err, pets) {
      if (err) {
        res.send(500, { error: 'internal server error' });
        return;
      }

      res.send(200, pets);
  });
}

To test the error condition of this controller action using mocha, you could do something like this:

var req = require('supertest');
var sinon = require('sinon');
var expect = require('chai').expect;
var fakeWaterlineChainMethod = require('waterline-fakes').fakeWaterlineChainMethod;

describe('the list method', function () {

  describe('when an error occurs looking up the pets', function () {

    beforeEach(function () {
      sinon.stub(Pets, 'find', fakeWaterlineChainMethod({
        err: new Error('boom');
      }));
    });

    it('returns 500 status code and error json', function (done) {
      req(sails.hooks.http.server)
        .get('/pets')
        .expect(500)
        .expect('Content-Type', /json/)
        .end(function (err, res) {
          expect(err).to.not.exist();
          expect(res.body).to.deep.equal({
            error: 'internal server error'
          });
          done();
        });

    });
  });
});

fakeWaterlineChainMethod api

var fakeWaterlineChainMethod = require('waterline-fakes').fakeWaterlineChainMethod;
var fakeMethod = fakeWaterlineChainMethod({
  err: {}, // Default: null. The error value you want the exec callback to be called with
  result: {} // Default: []. The result value you want the exec callback to be called with
});
// if you pass both err and result, the err will take precedence. so... don't do that.

using fakeWaterlineModel(options)

Used to simulate an actual Waterline model, if your controller or service code interacts with the model directly. This is usually passed as the result option to fakeWaterlineChainMethod when needed.

Take this other example controller action:

// PUT /pets/:id
update: function (req, res) {
  Pets.findOneById(req.params.id).exec(function (err, pet) {
    if (err) {
      res.send(500, { error: 'internal server error' });
      return;
    }

    if (!pet) {
      res.send(404, { error: 'not found' });
      return;
    }

    pet.name = req.body.name;
    pet.color = req.body.color;
    pet.type = req.body.type;
    pet.save(function (err, updatedPet) {
      if (err) {
        res.send(500, { error: 'internal server error' });
        return;
      }

      res.send(200, updatedPet);
    };
  });
}

You could test the error condition of the pet.save() using the following mocha test:

var req = require('supertest');
var sinon = require('sinon');
var expect = require('chai').expect;
var waterlineFakes = require('waterline-fakes');
var fakeWaterlineChainMethod = waterlineFakes.fakeWaterlineChainMethod;
var fakeWaterlineModel = waterlineFakes.fakeWaterlineModel;

describe('the update method', function () {

  describe('when an error occurs while saving the updated pet', function () {

    beforeEach(function () {
      var fakePet = fakeWaterlineModel({
        save: { err: new Error('boom') }
      });

      sinon.stub(Pets, 'findOneById', fakeWaterlineChainMethod({ result: fakePet }));
    });

    it('returns 500 status code and error json', function (done) {
      req(sails.hooks.http.server)
        .put('/pets/foo')
        .send({
          name: 'Lenny',
          type: 'golden retriever',
          color: 'champagne'
        }).expect(500)
        .expect('Content-Type', /json/)
        .end(function (err, res) {
          expect(err).to.not.exist();
          expect(res.body).to.deep.equal({
            error: 'internal server error'
          });
          done();
        });
    });
  });
});

fakeWaterlineModel api

var fakeWaterlineModel = require('waterline-fakes').fakeWaterlineModel;
var fakeModel = fakeWaterlineModel({
  props: {}, // properties you want the model to have. would be available at, for example, model.foo
  destroy: {
    err: {} // Default: null. The error value you want the destroy callback to be called with
  },
  save: {
    err: {}, // Default: null. The error value you want the save callback to be called with
    result: {} // Default: model. The result value you want the save callback to be called with.
  }
});
// if you pass both err and result, the err will take precedence. so... don't do that.

contributing

To contribute, see CONTRIBUTING.md

license

MIT