Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 

README.md

sprintf-mock

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:

$ yarn add 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: yarn test.

To check code style: yarn lint.

Credits

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

License

sprintf-mock is licensed under the MIT license.

About

sprintf-js plugin allowing to mock sprintf behaviour by returning data fixtures based on the resolved pattern

Resources

License

Packages

No packages published