Skip to content

Instantly share code, notes, and snippets.

@crrobinson14
Last active March 1, 2017 00:19
Show Gist options
  • Select an option

  • Save crrobinson14/ca7529a925bdea2c35e59ce9eab98d85 to your computer and use it in GitHub Desktop.

Select an option

Save crrobinson14/ca7529a925bdea2c35e59ce9eab98d85 to your computer and use it in GitHub Desktop.
ActionHero + Mocha test/ subfolder support files.
// Set NODE_ENV to test so AH creates api.specHelper for us
process.env.NODE_ENV = 'test';
process.env.ACTIONHERO_CONFIG = 'config,local-config';
// Get ActionHero ready to go
var actionheroPrototype = require('actionhero').actionheroPrototype,
actionhero = new actionheroPrototype(),
running = false;
global.api = null;
global.assert = require('assertthat');
global.bootstrap = {
init: function(done) {
actionhero.start(function(err, api) {
running = true;
if (err) {
throw err;
}
global.api = api;
// Set up anonymous and authenticated test user connections for calling API actions
api.resetConnection = function() {
api.testConnections = {};
api.testConnections.anonymous = new api.specHelper.connection();
api.testConnections.anonymous.rawConnection.req = { headers: {} };
Object.keys(api.session.testUsers).map(function(entry) {
api.testConnections[entry] = new api.specHelper.connection();
api.testConnections[entry].rawConnection.req = {
headers: {
authorization: 'Bearer ' + api.session.testUsers[entry].token
}
};
});
};
// Wait for an async message to be sent/broadcast to us
api.awaitMessage = function(connection, messageType, maxWait, callback) {
var maxTime = new Date().getTime() + maxWait,
cancel;
function check() {
if (new Date().getTime() > maxTime) {
clearInterval(cancel);
api.log('Timeout waiting for ' + messageType, 'info');
callback(null);
}
connection.messages.some(function(message) {
if (message.message && message.message.type && message.message.type === messageType) {
clearInterval(cancel);
callback(message);
return true;
}
});
}
cancel = setInterval(check, 100);
cancel.unref();
};
// Run an action but with a promise-style syntax. Note that we always resolve, so the test suite can deliberately evaluate
// errors as desired behavior.
api.specHelper.actionPromise = function(action, params) {
return new api.Promise(function(resolve, reject) {
api.specHelper.runAction(action, params, function(result) {
resolve(result);
});
});
};
// Pretend we've had a database failure
Object.keys(api.models).map(function(modelName) {
api.models[modelName].shouldFail = false;
api.models[modelName].beforeCreate(function(instance, options, callback) {
if (api.models[modelName].shouldFail) {
api.expectingOrmError = true;
callback(api.orm.queryError('Unable to process request.'));
} else {
callback();
}
});
api.models[modelName].beforeBulkCreate(function(instance, options, callback) {
if (api.models[modelName].shouldFail) {
api.expectingOrmError = true;
callback(api.orm.queryError('Unable to process request.'));
} else {
callback();
}
});
api.models[modelName].beforeFind(function(options, callback) {
if (api.models[modelName].shouldFail) {
api.expectingOrmError = true;
callback(api.orm.queryError('Unable to process request.'));
} else {
callback();
}
});
});
done();
});
},
teardown: function(done) {
if (running) {
actionhero.stop(function() {
delete global.api;
delete global.assert;
delete global.key;
});
}
done();
}
};
// Bootstrap functions before each test
before(bootstrap.init);
after(bootstrap.teardown);
// Reset from previous test runs
beforeEach(function(done) {
Object.keys(api.models).map(function(modelName) {
api.models[modelName].shouldFail = false;
});
api.resetConnection();
done();
});
describe('API: Core', function() {
it('should boot the test environment', function() {
assert.that(process.env.NODE_ENV).is.equalTo('test');
assert.that(api.env).is.equalTo('test');
assert.that(api.id).is.not.undefined();
});
});
--recursive
--reporter spec
--timeout 30000
--ignore-leaks
--slow 10000
--globals api,assert
--require test/bootstrap
test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment