Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added before and after events and listeners. closes #3

  • Loading branch information...
commit 994d7ea6bdd7dc350523715e87bff7bc45bb4a23 1 parent 8ea2419
@masylum authored
Showing with 63 additions and 2 deletions.
  1. +6 −0 Readme.md
  2. +23 −2 lib/testosterone.js
  3. +34 −0 tests/before_and_after.js
View
6 Readme.md
@@ -29,6 +29,8 @@ _testosterone_ is simple and flexible.
- `get|post|head|put|delete...(url, req, response, cb)`: Does a http call with the given request. If a response is given, testosterone will assert that the real response matches.
- `add(spec, function(done))`: Adds a test. The test is considered executed when `done` function is called. You can use `done` to curry a function.
+- `before(function)`: Listener for fired events before a test runs.
+- `after(function)`: Listener for fired events after a test runs.
- `run(cb)`: Runs the tests in serial. `cb` will be called once all the tests are executed.
- `assert`: Using this assert object instead of the native one will allow you to count and print the assertions.
@@ -71,6 +73,10 @@ You have more examples on the `test` folder:
testosterone
+ .before(function () {
+ console.log('test about to run!');
+ })
+
// using done to tell testosterone when the test is done
.add('First test', function (done) {
setTimeout(function () {
View
25 lib/testosterone.js
@@ -7,6 +7,7 @@ module.exports = function (config) {
require('colors');
var _sys = require('sys'),
+ _eventEmitter = new (require('events').EventEmitter)(),
_specs = [],
@@ -119,6 +120,21 @@ module.exports = function (config) {
});
/**
+ * Listens for a `beforeTest` of `afterTest` event
+ *
+ * @param {Function} [listener]
+ *
+ * @returns
+ * Testosterone
+ */
+ ['before', 'after'].forEach(function (event) {
+ TESTOSTERONE[event] = function (listener) {
+ _eventEmitter.on(event + 'Test', listener);
+ return TESTOSTERONE;
+ };
+ });
+
+ /**
* Adds a function to be called
*
* @see #serial
@@ -130,7 +146,7 @@ module.exports = function (config) {
* This allows to work with asyncronous tests.
*
* @returns
- * Testosterone, so you can chain http calls.
+ * Testosterone
*/
TESTOSTERONE.add = function (spec, body) {
_specs.push({spec: spec, body: body});
@@ -146,7 +162,7 @@ module.exports = function (config) {
* Callback that is run after all the `done` callbacks are run.
*
* @returns
- * Testosterone, so you can chain http calls.
+ * Testosterone
*/
TESTOSTERONE.serial = TESTOSTERONE.run = function (cb) {
cb = cb || function () {};
@@ -159,8 +175,11 @@ module.exports = function (config) {
_sys.print('\n\n' + _parseSpec(spec.spec) + ' => '.yellow);
}
+ _eventEmitter.emit('beforeTest');
+
if (_config.sync) {
spec.body.call(spec.body);
+ _eventEmitter.emit('afterTest');
next();
} else {
spec.body.call(spec.body, function done(fn) {
@@ -168,9 +187,11 @@ module.exports = function (config) {
if (fn) {
return function () {
fn.apply(fn, arguments);
+ _eventEmitter.emit('afterTest');
next();
};
} else {
+ _eventEmitter.emit('afterTest');
next();
}
});
View
34 tests/before_and_after.js
@@ -0,0 +1,34 @@
+var testosterone = require('../lib/testosterone')({port: 3000, title: 'Before and After'}),
+ add = testosterone.add,
+ count = 0,
+ assert = testosterone.assert;
+
+testosterone
+
+ .before(function () {
+ console.log('before called');
+ count++;
+ })
+
+ .after(function () {
+ console.log('after called');
+ })
+
+ .add('`first` test', function (next) {
+ assert.equal(count, 1);
+
+ count++;
+
+ setTimeout(function () {
+ assert.equal(count, 2);
+ next();
+ }, 100);
+ })
+
+ .add('and this is a `second` test', function (done) {
+ setTimeout(done(function () {
+ assert.equal(count, 3);
+ }), 200);
+ })
+
+ .run();
Please sign in to comment.
Something went wrong with that request. Please try again.