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
config/rewire.js
List components you want rewired, in /** * 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 */moduleexportsrewire = // 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 = fileServiceRevert; ;
Run tests
When running tests, ensure that sails is lifted in the test
environment. You can do this by either,
- Setting
NODE_ENV
totest
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 = sails; ; ;
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
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 ❤️ by Postman