MimusMocker is a unified API mocking middleware which can be used for providing API mocks quickly i.e even before writing a single line of actual api into your Routers.
Install via npm.
$ npm install mimus-mocker
or
Install via git clone
$ git clone https://github.com/ajaykumarmeher/mimus-mocker.git
$ npm install
- Supports configuration based mocking.
- Allows multi-level controls for quick API testing.
- Supports custom validations per API.
const mocks = {
'mocks': [{
'url': '/user/:id',
'method': 'GET',
'responseData': userMockResponse,
'isEligible': true
}, {
'url': '/posts/:id',
'method': 'GET',
'responseData': postMockResponse,
'additionalValidator': customValidator /* optional */,
'isEligible': true
}],
'isEligible': process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'staging'
};
function customValidator (req, res, next) {
if (req.params.id) {
res.status(200).json(postV2MockResponse);
return next(true, res);
}
return next(false, null);
}
const app = require('express')();
const mimusMocker = require('mimus-mocker')(mocks);
app.get('/user/:id', mimusMocker, function (req, res) {
res.status(200).json({ name: 'tobi' });
});
app.get('/posts/:id', mimusMocker, function (req, res) {
res.status(200).json({ title: 'some-post' });
});
const mocks = {
'mocks': [{
'url': '/user/:id',
'method': 'GET',
'responseData': userMockResponse,
'isEligible': true
}, {
'url': '/posts/:id',
'method': 'GET',
'responseData': postMockResponse,
'additionalValidator': customValidator /* optional */,
'isEligible': true
}],
'isEligible': process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'staging'
};
function customValidator (req, res, next) {
if (req.params.id) {
res.status(200).json(postV2MockResponse);
return next(true, res);
}
return next(false, null);
}
const app = require('express')();
const mimusMocker = require('mimus-mocker')(mocks);
app.use(mimusMocker);
MimusMocker uses url-matcher for matching urls.
All the methods like(GET, PUT, POST, DELETE etc.) currently getting supported by Express framework are eligible for validation.
The mock contract for the API.
This will be a callback for additional validation.
anyFunction(req,res,next)
- next(isOverrideResult, newResponse)
The next function will allow you to process the required action based on your
additonalValidator
. IfisOverrideResult = true
thennewResponse
will be sent. Else the default data defined inresponseData
will be sent. Useres
for sending custom response and passres
Object as thenewResponse
. For more info please check the examples) given above. - req
If the filter specified with query params like
/test/:id
and the requested path isapi/test/1234/remainingPath
then Mimus will attach the remaining path toreq
and you can access it viareq.remainingPath
which will contain the additional path information if any.
This is used to control each mock item during development. If true
, the url will be matched against the api request.
This is used to control the complete mock feature. eg. the mock should only be enabled for "staging/dev" environment. But, should be disabled for production environments.
MIT
Free Software, Hell Yeah!