Setup and serve stubs, fakes and expectations
npm install vanilliNodejs library that allows managing of a RESTful server for storing, matching and verifying stubs/expectations from a test suite.
Your SUT is then configured to call vanilli instead of the REST services it usually uses.
npm install vanilli
``js`
var vanilli = require('vanilli');
...
vanilli.listen(port, config);
config is an optional parameter. All config options are given below with their default values:
`js`
{
logLevel: "error", // See 'Diagnostics' section below
static: undefined // See 'Static Content' section below
}
`js
var vanilli = require('vanilli');
describe('My SUT', function () {
before(function () {
vanilli.listen(port); // Start the vanilli REST server
});
after(function () {
vanilli.stop(); // Shutdown vanilli REST server
});
afterEach(function () {
vanilli.verify(); // Verify all expectations have been met
vanilli.clear(); // Clear down stubs from vanilli ready for next test
});
it('does something', function (done) {
vanilli.stub(
vanilli.onGet("/this/url/MIGHT/happen").respondWith(200)
);
vanilli.expect(
vanilli.onGet("/this/url/MUST/happen").respondWith(200)
);
// Manipulate SUT to required state
// Make assertions
// Note that the vanilli expectation above will be verified by the vanilli.verify() in 'afterEach'.
});
});
`
See the API for more usage information.
This approach means more succinct stubs and less matching criteria irrelevant to the test at hand.
to debug will cause
vanilli to spit out a whole load of diagnostic information relating to what stubs are stored and how it is matching stubs against
incoming requests. In such situations I recommend piping the output to the bunyan executable itself (which you can install in the usual way with npm install -g bunyan) to get nicely formatted output.See the bunyan project itself for more info on logging and log levels.
Stubs vs expectations
For vanilli, an "expectation" is simply a specialized stub. In short: a stub MIGHT be matched; an
expectation MUST be matched.A stub...
* CAN be matched UP TO the specified number of times (1 if not explicitly specified).
* WILL cause an error if matched too many times.
* WILL NOT cause an error if matched too few times.
An expectation...
* MUST be matched the specified number of times (1 if not explicitly specified).
* WILL cause an error if matched too many times.
* WILL cause an error if matched too few times.
So, if you want to assert on the actual calls that your SUT is using use an expectation;
otherwise use a stub.
REMEMBER: The more vanilli expectations you add to your tests the more brittle they will get:
consider using stubs as your first choice.
Static Content
To serve up the stubbed responses vanilli is, at its core, an HTTP server. This means that it could, potentially,
serve up content other than stubs. This opens up the possibility of vanilli acting as your web app's
HTTP server.So, to this end, the
static config option was created. It acts like a "pass through" filter - if
an incoming request matches the static filter then the static content will be served rather than
attempting to match against a stub. The option takes the form:`js
{
static: {
root: "sut/static/root", // Root path for static content
"default": "page.html", // Default document from root
include: [
"*/.html", // normal include
{ path: '/foo', target: 'foo.html' }, // proxy route to specified target
{ path: '/foo/**', target: 'foo-sub.html' }, // proxy wildcard route to specified target
{ path: '/bar', useDefault: true } // proxy route to default "page.html"
],
exclude: [ globA, globB, ... , globZ ]
}
}
`You can see an example in test/e2e/vanilli-test.js
CLI
As well as the javascript API described above, the vanilli server also provides a CLI for starting a server from non-javascript environments. To see the CLI simply run vanilli --help. (You may need to install vanilli globally first with npm install -g vanilli`.)