sprintf-js plugin allowing to mock sprintf behaviour by returning data fixtures based on the resolved pattern
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
tests
.editorconfig
.eslintrc
.gitignore
.travis.yml
LICENCE
README.md
package.json

README.md

sprintf-mock Build Status

sprintf-js plugin allowing to mock sprintf behaviour by returning data fixtures based on the resolved pattern and parameters. For example, it's useful if you build some external resources URLs in your app with sprintf and you rather want to request local resources in your functional tests.

See this post to know why we use sprintf-mock at M6Web.

Installation

Install with npm:

$ npm install sprintf-mock

Usage

First, you have to define the pattern to mock in a configuration file:

// ./sprintf-mock-config.js file
module.exports = [
  {
    // sprintf string pattern
    pattern: 'https://domain.example/%s',

    // `pattern`: above sprintf pattern
    // `params`: parameters used to resolve the pattern
    callback: function (pattern, params) {
      
      // Do some transformations
      
      // Static string or another sprintf pattern resolved with params
      return newPattern;
    }
  },
  ...
];

Then use the plugin:

// ./server.js file
var sprintfLib = require('sprintf-js');
var config = require('./sprintf-mock-config');

require('sprintf-mock')(sprintfLib, config);

Tests

To run units tests: npm test.

To check code style: npm run lint.

Credits

Developped by the Cytron Team of M6 Web.
Tested with nodeunit.

License

sprintf-mock is licensed under the MIT license.