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

@petrocloud/sails-hook-rewire

v1.0.1

Published

Installable sailsjs hook that lets you rewire sails components during tests

Downloads

14

Readme

sails-hook-rewire

This is a installable sailsjs hook that lets you rewire sails components, when sails is lifted in test environment.

Often during testing you need to stub certain functions inside your sails models/controllers/services, for example a function that makes http calls.

sails-hook-rewire uses the rewire module to replace components already loaded by sails, with their rewired versions.

Installation

npm install sails-hook-rewire --save

Usage

List components you want rewired, in config/rewire.js
/**
 * Configuration to list which sails components to rewire.
 *
 * You can rewire models, polices, adapters, hooks, blueprints, and responses.
 * You can also choose to have models and services rewired in the global object. Sails only injects models and services
 * in the global obejct.
 *
 * @type {Object}
 */
module.exports.rewire = {
  // List services to be rewired
  services: [
    {
      name: 'FileService',
      global: true    // replace the global FileService with the rewired version
    },
    {
      name: 'FetchService',
      global: false   // don't replace the global FetchService with the rewired version
    }
  ],
  // List controllers to be rewired
  controllers: [
    {
      name: 'FileController'
    },
    {
      name: 'DNSLookUpController',
      global: true    // this does nothing as controllers are not injected into the global object by sails
    }
  ]
};
Rewire your components

Suppose you have a service, FileService, that depends on the fs module, and you want to stub fs.readFile method in the service. Since the service has already been rewired, all you need to do is stub

var expect = require('expect.js'),
  fileServiceRevert;

describe('Tests for FileService', function () {
  before(function () {
    fileServiceRevert = FileService.__set__({
      fs: {
        readFile: function (filename, options, callback) {
          var response = {
            err: null,
            data: undefined
          };

          callback = typeof options === 'function' && options || callback || function () {};

          // Return an error if filename matches error, eg /var/www/jsons/error.json
          filename.match(/error/) && (response.err = new Error('Error reading file'));

          // Return a valid JSON string if file matches valid, eg /var/www/jsons/valid.json
          filename.match(/.+\/valid/) && (response.data = '{"valid": true}');

          // Return an invalid JSON string if file matches invalid, eg /var/ww/jsons/invalid.json
          filename.match(/.+\/invalid/) && (response.data = '{"invalid": true;');

          return callback(response.err, response.data);
        }
      }
    });
  });

  after(function () {
    // Revert the rewiring
    fileServiceRevert();
  });

  it('handles read error', function (done) {
    FileService.fetchJSONFromFile('/var/www/jsons/error.json', function (err, data) {
      expect(err).to.be.ok();
      expect(err.error instanceof Error).to.be.ok();
      expect(data).to.not.be.ok()

      return done();
    });
  });

  it('handles invalid json', function (done) {
    FileService.fetchJSONFromFile('/var/www/jsons/invalid.json', function (err, data) {
      expect(err).to.be.ok();
      expect(err.error instanceof Error).to.be.ok();
      expect(data).to.not.be.ok()

      return done();
    });
  });

  it('returns a json if file has valid json', function (done) {
    FileService.fetchJSONFromFile('/var/www/jsons/valid.json', function (err, data) {
      expect(err).to.not.be.ok();
      expect(data).to.be.ok();
      expect(data).to.be.an('object');
      expect(data.valid).to.be.ok();

      return done();
    });
  });
});
Run tests

When running tests, ensure that sails is lifted in the test environment. You can do this by either,

  • Setting NODE_ENV to test before the tests are run
export NODE_ENV=test;
npm test;
unset NODE_ENV;
  • Passing the environment to sails while loading in a test bootstrap.
var Sails = require('sails'),
  sails;

before(function (done) {
  Sails.load({
    environment: 'test'
    // other setting overrides
  },
  function (err, server) {
    if (err) { return done(sails); }
    sails = server;
    return done(null, sails);
  });
});

after(function (done) {
  sails.lower(done);
});

Optionally, if you wish to lift your server in test mode, you can pass the environment as an option to sails lift

sails lift --environment=test

####### Changing name of environment

If you run your tests with another environment variable name, you can specity it in the configuration:

module.exports.rewire = {
  environments: {
    test: true,  // should default to true
    shippable: true,
    development: false
  }

For more examples of how to test sails components with rewire, check out the tests for this module.


This project is licensed under the Apache 2.0 License.

For contributing, please check the contributing guidelines.

Made with :heart: by Postman