Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modernizing tests.

* Upgrade all devDependencies.
* Use a required setup.js file to take care of some global stuff.
* Make the throwArgumentError helper into a mini Chai plugin.
* Move completely to the should interface, over expect.
  • Loading branch information...
commit 6f9ebf42c0d65083f42a3523679df1a84bd651e5 1 parent da62923
@domenic authored
View
10 package.json
@@ -22,7 +22,7 @@
},
"main": "./lib/pubit.js",
"scripts": {
- "test": "node ./node_modules/mocha/bin/mocha"
+ "test": "mocha"
},
"engines": {
"node": "*"
@@ -31,9 +31,9 @@
"dict": "1"
},
"devDependencies": {
- "coffee-script": "~1.2.0",
- "mocha": "~0.11.0",
- "chai": "~0.2.4",
- "sinon": "~1.3.1"
+ "coffee-script": ">= 1.3.3",
+ "mocha": ">= 1.1.0",
+ "chai": ">= 1.0.4",
+ "sinon": ">= 1.3.4"
}
}
View
3  test/1 basicFunctionality.coffee
@@ -1,6 +1,3 @@
-require("chai").should()
-sinon = require("sinon")
-
Publisher = require("../lib/pubit").Publisher
describe "Publisher/emitter under normal usage", ->
View
3  test/2 unusualSituations.coffee
@@ -1,6 +1,3 @@
-require("chai").should()
-sinon = require("sinon")
-
Publisher = require("../lib/pubit").Publisher
describe "Publisher/emitter in unusual situations", ->
View
31 test/3 options.coffee
@@ -1,6 +1,3 @@
-sinon = require("sinon")
-expect = require("chai").expect
-
Publisher = require("../lib/pubit").Publisher
describe "Publisher options", ->
@@ -78,21 +75,21 @@ describe "Publisher options", ->
sinon.assert.called(listener2)
it "should throw an error upon attempting to subscribe to an unknown event", ->
- expect(-> emitter.on("unknownEvent", ->)).to.throw()
+ (-> emitter.on("unknownEvent", ->)).should.throw()
it "should throw an error upon attempting to publish an unknown event", ->
- expect(-> publisher.publish("unknownEvent")).to.throw()
+ (-> publisher.publish("unknownEvent")).should.throw()
it "should be validated", ->
- expect(-> new Publisher(null)).to.throw(TypeError)
- expect(-> new Publisher(5)).to.throw(TypeError)
- expect(-> new Publisher("hi")).to.throw(TypeError)
- expect(-> new Publisher(->)).to.throw(TypeError)
-
- expect(-> new Publisher(onListenerError: null)).to.throw(TypeError)
- expect(-> new Publisher(onListenerError: {})).to.throw(TypeError)
- expect(-> new Publisher(onListenerError: 5)).to.throw(TypeError)
-
- expect(-> new Publisher(events: null)).to.throw(TypeError)
- expect(-> new Publisher(events: {})).to.throw(TypeError)
- expect(-> new Publisher(events: 5)).to.throw(TypeError)
+ (-> new Publisher(null)).should.throw(TypeError)
+ (-> new Publisher(5)).should.throw(TypeError)
+ (-> new Publisher("hi")).should.throw(TypeError)
+ (-> new Publisher(->)).should.throw(TypeError)
+
+ (-> new Publisher(onListenerError: null)).should.throw(TypeError)
+ (-> new Publisher(onListenerError: {})).should.throw(TypeError)
+ (-> new Publisher(onListenerError: 5)).should.throw(TypeError)
+
+ (-> new Publisher(events: null)).should.throw(TypeError)
+ (-> new Publisher(events: {})).should.throw(TypeError)
+ (-> new Publisher(events: 5)).should.throw(TypeError)
View
2  test/4 onNext.coffee
@@ -1,5 +1,3 @@
-sinon = require("sinon")
-
Publisher = require("../lib/pubit").Publisher
describe "emitter.onNext", ->
View
8 test/5 makeEmitter.coffee
@@ -1,7 +1,3 @@
-sinon = require("sinon")
-expect = require("chai").expect
-should = require("chai").should()
-
pubit = require("../lib/pubit")
describe "makeEmitter", ->
@@ -50,8 +46,8 @@ describe "makeEmitter", ->
errorThrowingListener = -> throw error
target.on("event", errorThrowingListener)
- expect(-> publish("anotherEvent")).to.throw()
- expect(-> target.on("anotherEvent")).to.throw()
+ (-> publish("anotherEvent")).should.throw()
+ (-> target.on("anotherEvent")).should.throw()
publish("event")
sinon.assert.notCalled(listener)
View
3  test/6 listenerHelpers.coffee
@@ -1,6 +1,3 @@
-require("chai").should()
-sinon = require("sinon")
-
pubit = require("../lib/pubit")
describe "Listener-creating helpers", ->
View
9 test/7 publisherArgumentValidation.coffee
@@ -1,6 +1,3 @@
-expect = require("chai").expect
-require("./helpers")()
-
Publisher = require("../lib/pubit").Publisher
describe "Publisher argument validation", ->
@@ -11,10 +8,10 @@ describe "Publisher argument validation", ->
describe ".publish(eventName)", ->
it "throws an error when given a number", ->
- expect(-> publisher.publish(5)).to.throwArgumentError("eventName", "string")
+ (-> publisher.publish(5)).should.throwArgumentError("eventName", "string")
it "throws an error when given null", ->
- expect(-> publisher.publish(null)).to.throwArgumentError("eventName", "string")
+ (-> publisher.publish(null)).should.throwArgumentError("eventName", "string")
it "throws an error when given an object", ->
- expect(-> publisher.publish("eventName")).to.throwArgumentError("eventName", "string")
+ (-> publisher.publish({})).should.throwArgumentError("eventName", "string")
View
29 test/8 emitterArgumentValidation.coffee
@@ -1,6 +1,3 @@
-expect = require("chai").expect
-require("./helpers")()
-
Publisher = require("../lib/pubit").Publisher
describe "Emitter argument validation", ->
@@ -13,43 +10,43 @@ describe "Emitter argument validation", ->
describe ".on(eventName, listener)", ->
it "throws an error when give a string and a number", ->
- expect(-> emitter.on("eventName", 5)).to.throwArgumentError("listener", "function")
+ (-> emitter.on("eventName", 5)).should.throwArgumentError("listener", "function")
it "throws an error when give a string and null", ->
- expect(-> emitter.on("eventName", null)).to.throwArgumentError("listener", "function")
+ (-> emitter.on("eventName", null)).should.throwArgumentError("listener", "function")
it "throws an error when give null and a function", ->
- expect(-> emitter.on(null, ->)).to.throwArgumentError("eventName", "string")
+ (-> emitter.on(null, ->)).should.throwArgumentError("eventName", "string")
it "throws an error when give a number and a function", ->
- expect(-> emitter.on(5, ->)).to.throwArgumentError("eventName", "string")
+ (-> emitter.on(5, ->)).should.throwArgumentError("eventName", "string")
describe ".on(eventHash)", ->
it "throws an error when given a number", ->
- expect(-> emitter.on(5)).to.throwArgumentError("hash", "string-to-function hash")
+ (-> emitter.on(5)).should.throwArgumentError("hash", "string-to-function hash")
it "throws an error when given null", ->
- expect(-> emitter.on(null)).to.throwArgumentError("hash", "string-to-function hash")
+ (-> emitter.on(null)).should.throwArgumentError("hash", "string-to-function hash")
it "throws an error when given a string by itself", ->
- expect(-> emitter.on("eventName")).to.throwArgumentError("hash", "string-to-function hash")
+ (-> emitter.on("eventName")).should.throwArgumentError("hash", "string-to-function hash")
describe ".off(eventName, listener)", ->
it "throws an error when give a string and a number", ->
- expect(-> emitter.off("eventName", 5)).to.throwArgumentError("listener", "function")
+ (-> emitter.off("eventName", 5)).should.throwArgumentError("listener", "function")
it "throws an error when give a string and null", ->
- expect(-> emitter.off("eventName", null)).to.throwArgumentError("listener", "function")
+ (-> emitter.off("eventName", null)).should.throwArgumentError("listener", "function")
it "throws an error when give null and a function", ->
- expect(-> emitter.off(null, ->)).to.throwArgumentError("eventName", "string")
+ (-> emitter.off(null, ->)).should.throwArgumentError("eventName", "string")
it "throws an error when give a number and a function", ->
- expect(-> emitter.off(5, ->)).to.throwArgumentError("eventName", "string")
+ (-> emitter.off(5, ->)).should.throwArgumentError("eventName", "string")
describe ".off(eventHash)", ->
it "throws an error when given a number", ->
- expect(-> emitter.off(5)).to.throwArgumentError("eventNameOrHash", "string or string-to-function hash")
+ (-> emitter.off(5)).should.throwArgumentError("eventNameOrHash", "string or string-to-function hash")
it "throws an error when given null", ->
- expect(-> emitter.off(null)).to.throwArgumentError("eventNameOrHash", "string or string-to-function hash")
+ (-> emitter.off(null)).should.throwArgumentError("eventNameOrHash", "string or string-to-function hash")
View
16 test/helpers.coffee
@@ -1,16 +0,0 @@
-Assertion = require("chai").Assertion
-
-module.exports = ->
- # Add a throwArgumentError(argName, type) matcher.
- Assertion.prototype.throwArgumentError = (argName, type) ->
- (new Assertion(@obj)).is.a("function")
-
- try
- @obj()
- catch error
- @assert(
- error instanceof TypeError and error.message is "#{ argName } argument must be a #{ type }.",
- "expected #{ @inspect } to throw an argument error requiring #{ argName } to be of type #{ type }"
- )
-
- return this
View
2  test/mocha.opts
@@ -1 +1,3 @@
+--compilers coffee:coffee-script
--reporter spec
+--require test/support/setup
View
7 test/support/argumentErrorChaiPlugin.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = function (chai, utils) {
+ chai.Assertion.addMethod("throwArgumentError", function (argName, type) {
+ this._obj.should.throw(TypeError, argName + " argument must be a " + type + ".");
+ });
+};
View
9 test/support/setup.js
@@ -0,0 +1,9 @@
+"use strict";
+
+var chai = require("chai");
+var argumentErrorChaiPlugin = require("./argumentErrorChaiPlugin");
+
+chai.use(argumentErrorChaiPlugin);
+chai.should();
+
+global.sinon = require("sinon");
Please sign in to comment.
Something went wrong with that request. Please try again.