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 yarn:
$ 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 Bedrock Streaming.
Tested with Jest.
License
sprintf-mock is licensed under the MIT license.