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.
Install with yarn:
$ yarn add sprintf-mock
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);
To run units tests: yarn test
.
To check code style: yarn lint
.
Developped by the Cytron Team of Bedrock Streaming.
Tested with Jest.
sprintf-mock is licensed under the MIT license.