CouchDB Design Doc Testing Tool
npm install couchdb-ddoc-testThis is a simple CouchDB design doc testing tool.
Usage:
``javascript
var DDocTest = require('couchdb-ddoc-test');
var test = new DDocTest({
fixture: {a: 1},
src: 'path/to/map.js'
});
var result = test.runMap();
assert.equals(result, fixture);
`
CouchDB supports require() within design doc functions. It works slightlyrequire()
different from in e.g. Node.js (in which these tests are run).require()
Instead of relying on CouchDB’s we will be using a couchapp
specific pre-processing directive. To make everything work, we have to
jump through a small hoop:
Say you want to var foo = require('foo'); within a map function. Do this:
`javascript
function(doc) {
// prepare for require
var module = module || {};
// This next line is a couchapp preprocessor line, that copy and pastes thepath/to/foo.js
// contents of into this function. It should define thefoo
// variable . That is how this code is run within CouchDB. !code
// path/to/foo.js
// This next line makes sure that we only run the Node.js require() when the!code
// macro is not expanded. This is why !code path/to/foo.js shouldfoo
// create the variable. If it doesn’t exist, we run a regular Node.jsrequire()
// . With one caveat: since map.js will be run within eval()require()
// in another module than your tests, we need to put the full module path into
// , otherwise, the foo package would have to be a dependency of`
// the couchdb-ddoc-test package, which wouldn’t work out. Anyhoo!
var foo = foo || require(process.cwd() + '/different/path/to/foo'
}
npm test`