Skip to content

Instantly share code, notes, and snippets.

@doublemarked
Last active November 23, 2015 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doublemarked/1566e62112e89aed98bd to your computer and use it in GitHub Desktop.
Save doublemarked/1566e62112e89aed98bd to your computer and use it in GitHub Desktop.
Loopback unit testing bootstrapper
var chai = require('chai');
chai.use(require('chai-things'));
var expect = chai.expect;
var appRoot = require('app-root-path');
var factories = appRoot.require('/server/test/factories');
var app = appRoot.require('/server/server');
var contextUtils = appRoot.require('/common/utils/context');
var loopbackContext = contextUtils.useDummyContext();
module.exports = {
chai: chai,
app: app,
appRoot: appRoot,
models: app.models,
factories: factories,
lbCtx: contextUtils.useDummyContext(),
authenticate: function (username, cb) {
app.models.user.findOne({username:username}, function (err, user) {
if (err) return cb(err);
if (user) loopbackContext.set('currentUser', user);
cb(null, user);
});
}
};
before(function importSampleData(done) {
this.timeout(5000);
if (app.importing) {
console.log('Waiting for import...');
app.on('import done', done);
} else {
done();
}
});
/* HM 2015-11-23
* This is our dummy loopback context, which we use for simulating the context outside of an http request. It's not
* perfect and I'm not even sure it should be used - the more we use it the more we approximate the actual context.
* but it does get the job done, for now. Use it with caution and make sure you unload it when you're done using it,
* or you may contaminate some future http request.
*/
var dummyContext = null;
var _lbGetCurrentContext;
var tryLoopbackContext;
exports.getDummyContext = function() {
return dummyContext;
};
exports.makeDummyContext = function () {
var storage = {};
return {
storage: storage,
get: function (name) {
if (tryLoopbackContext) {
var lbc = _lbGetCurrentContext && _lbGetCurrentContext();
if (lbc) return lbc.get(name);
}
return storage[name];
},
set: function (name, val) {
if (tryLoopbackContext) {
var lbc = _lbGetCurrentContext && _lbGetCurrentContext();
if (lbc) return lbc.set(name);
}
storage[name] = val;
},
reset: function (/*name, val*/) { throw 'unimplemented'; }
};
};
exports.useDummyContext = function (preventFallback) {
tryLoopbackContext = !preventFallback;
dummyContext = dummyContext || exports.makeDummyContext();
var loopback = require('loopback');
if (loopback.getCurrentContext === exports.getDummyContext) {
return dummyContext;
}
_lbGetCurrentContext = loopback.getCurrentContext;
loopback.getCurrentContext = exports.getDummyContext;
return dummyContext;
};
exports.unloadDummyContext = function () {
dummyContext = null;
if (_lbGetCurrentContext) {
var loopback = require('loopback');
loopback.getCurrentContext = _lbGetCurrentContext;
_lbGetCurrentContext = undefined;
}
};
var chalk = require('chalk');
var bootstrap = require('./bootstrap');
var expect = bootstrap.chai.expect;
var models = bootstrap.models;
describe(chalk.blue('Something'), function() {
describe('Something else', function() {
it('should be possible to do something', function(done) {
models.Your Model.findById(someId, function (err, instance) {
if (err) return done(err);
expect(instance).to.be.something();
// more expectations
done();
});
});
});
});
@sachinhub
Copy link

Where can i find -var contextUtils = appRoot.require('/common/utils/context');

@doublemarked
Copy link
Author

This is our dummy context. It is not an ideal solution, but it gets the job done. I can share it with you. It has been updated since this snippet was posted. I'll attach it as another file.

@sachinhub
Copy link

That would be great. Can you please share the latest scripts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment