Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create 'emits' and 'doesNotEmit'.

  • Loading branch information...
commit 7c8490f4ec8ec1834946720614c74e64bc084271 1 parent d8bbb48
David Braun authored
Showing with 69 additions and 0 deletions.
  1. +26 −0 lib/main.js
  2. +43 −0 test/main.js
View
26 lib/main.js
@@ -0,0 +1,26 @@
+var assert = require('assert');
+
+module.exports = assert;
+
+function emitTest(emitter, event, block) {
+ var emitted = false;
+
+ emitter.once(event, function () {
+ emitted = true;
+ });
+
+ block();
+ return emitted;
+}
+
+assert.emits = function (emitter, event, block, message) {
+ if (!emitTest(emitter, event, block))
+ assert.fail(event, null,
+ message || "Missing expected event '" + event + "'.");
+};
+
+assert.doesNotEmit = function (emitter, event, block, message) {
+ if (emitTest(emitter, event, block))
+ assert.fail(event, null,
+ message || "Emitted unwanted event '" + event + "'.");
+};
View
43 test/main.js
@@ -0,0 +1,43 @@
+var assert = require('./../lib/main'),
+ events = require('events'),
+ emitter = new events.EventEmitter();
+
+// Normal assert tests still work.
+
+assert.ok(true);
+
+// assert.emits
+
+assert.emits(emitter, 'test', function () {
+ emitter.emit('test');
+});
+
+assert.throws(function () {
+ assert.emits(emitter, 'test', function () {
+ // No emissions here.
+ });
+}, assert.AssertionError);
+
+assert.throws(function () {
+ assert.emits(emitter, 'test', function () {
+ // No emissions here.
+ }, "I really like test events.");
+}, /I really like test events./);
+
+// assert.doesNotEmit
+
+assert.doesNotEmit(emitter, 'test', function () {
+ // No emissions here.
+});
+
+assert.throws(function () {
+ assert.doesNotEmit(emitter, 'test', function () {
+ emitter.emit('test');
+ });
+}, assert.AssertionError);
+
+assert.throws(function () {
+ assert.doesNotEmit(emitter, 'test', function () {
+ emitter.emit('test');
+ }, "I really don't like test events.");
+}, /I really don't like test events./);

0 comments on commit 7c8490f

Please sign in to comment.
Something went wrong with that request. Please try again.