Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
* release/1.0.1:
  Update deps; bump version number
  Convert files to Unix Line-endings; add .editorconfig
  Add some Mocha tests
  Add Travis-CI & NPM badges to readme.
  Add Travis file; remove spaces in readme.
  • Loading branch information
sirlancelot committed Dec 18, 2014
2 parents 4a277bc + e3fccc1 commit 6473540
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 136 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
insert_final_newline = true
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js

node_js:
- "0.10"

script:
npm run lint && npm test
161 changes: 83 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,83 @@
# Chai Checkmark

**Checkmark** is a [Chai][] plugin for counting assertions made during a test.
Often, when dealing with asynchronous tests, it can be difficult to determine
if a callback was actually called. With Checkmark, you can be assured that the
assertion was made.

## Installation

Include Checkmark in the browser, as a CommonJS module, or through AMD.

```html
<!-- Browser -->
<script src="chai.js"></script>
<script src="chai-checkmark.js"></script>
```
```js
// CommonJS
var chai = require("chai"),
plugin = require("chai-checkmark")
chai.use(plugin)
```
```js
// AMD
require(["chai", "chai-checkmark"], function(chai, plugin) {
chai.use(plugin)
})
```

## How to use

```js
describe("something", function() {
it("should check two things", function(next) {
expect(2).checks(next) // <-- pass in the callback

"sync test".should.be.a("string").mark() // <-- check 1

setTimeout(function() {
// check 2, callback is called after the current event finishes
"async test".should.be.a("string").mark()
}, 500)
})
})
```

## API

Checkmark builds on Chai's assertion library by adding just two methods:

### `expect(Number).check(Function)` or `.checks(Function)`

This must be called before `.mark()` but doesn't have to be at the very
beginning of a test. You establish how many times you expect `.mark()` to
be called and optionally pass in a callback to be called when the number
of marks is reached.

### `expect(str).to.be.a("string").mark()`

Add `.mark()` to the end of every assertion to which you want to have
tracked by Checkmark. You can use any number of Chai's assertions,
including `.and`, as long as you end your statement with `.mark()`.

## Contributing

Pull Requests are welcome. They will only be merged if they are based off the
tip of the [develop][] branch. Please rebase (don't merge!) your changes if
you are behind. To learn about why rebase is better than merge, check out [The
Case for Git Rebase][rebase].

In short, to bring your Working Copy up to the tip of [develop][], you can use
the rebase feature: `git pull --rebase`. See [Pull with Rebase][pull] for
details.

[develop]: https://github.com/sirlancelot/chai-checkmark/tree/develop
[rebase]: http://darwinweb.net/articles/the-case-for-git-rebase
[pull]: http://gitready.com/advanced/2009/02/11/pull-with-rebase.html
[Chai]: http://chaijs.com/
# Chai Checkmark [![][ci-develop]][travis-ci] [![][downloads]][npm]

**Checkmark** is a [Chai][] plugin for counting assertions made during a test.
Often, when dealing with asynchronous tests, it can be difficult to determine
if a callback was actually called. With Checkmark, you can be assured that the
assertion was made.

## Installation

Include Checkmark in the browser, as a CommonJS module, or through AMD.

```html
<!-- Browser -->
<script src="chai.js"></script>
<script src="chai-checkmark.js"></script>
```
```js
// CommonJS
var chai = require("chai"),
plugin = require("chai-checkmark")
chai.use(plugin)
```
```js
// AMD
require(["chai", "chai-checkmark"], function(chai, plugin) {
chai.use(plugin)
})
```

## How to use

```js
describe("something", function() {
it("should check two things", function(next) {
expect(2).checks(next) // <-- pass in the callback

"sync test".should.be.a("string").mark() // <-- check 1

setTimeout(function() {
// check 2, callback is called after the current event finishes
"async test".should.be.a("string").mark()
}, 500)
})
})
```

## API

Checkmark builds on Chai's assertion library by adding just two methods:

### `expect(Number).check(Function)` or `.checks(Function)`

This must be called before `.mark()` but doesn't have to be at the very
beginning of a test. You establish how many times you expect `.mark()` to
be called and optionally pass in a callback to be called when the number
of marks is reached.

### `expect(str).to.be.a("string").mark()`

Add `.mark()` to the end of every assertion to which you want to have
tracked by Checkmark. You can use any number of Chai's assertions,
including `.and`, as long as you end your statement with `.mark()`.

## Contributing

Pull Requests are welcome. They will only be merged if they are based off the
tip of the [develop][] branch. Please rebase (don't merge!) your changes if
you are behind. To learn about why rebase is better than merge, check out [The
Case for Git Rebase][rebase].

In short, to bring your Working Copy up to the tip of [develop][], you can use
the rebase feature: `git pull --rebase`. See [Pull with Rebase][pull] for
details.

[Chai]: http://chaijs.com/
[ci-develop]: https://img.shields.io/travis/sirlancelot/chai-checkmark/develop.svg?style=flat-square
[ci-master]: https://img.shields.io/travis/sirlancelot/chai-checkmark/master.svg?style=flat-square
[develop]: https://github.com/sirlancelot/chai-checkmark/tree/develop
[downloads]: https://img.shields.io/npm/dm/chai-checkmark.svg?style=flat-square
[npm]: https://www.npmjs.org/package/chai-checkmark
[pull]: http://gitready.com/advanced/2009/02/11/pull-with-rebase.html
[rebase]: http://darwinweb.net/articles/the-case-for-git-rebase
[travis-ci]: https://travis-ci.org/sirlancelot/chai-checkmark
111 changes: 56 additions & 55 deletions chai-checkmark.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,56 @@
;(function(context, factory) {
if (typeof require === "function" &&
typeof exports === "object" &&
typeof module === "object"
) {
module.exports = factory()
} else {
if (typeof define === "function" &&
typeof define.amd === "object"
) {
define(factory)
} else {
if (!context.chai) {
throw new Error("chai-checkmark couldn't find Chai.")
}
context.chai.use(factory())
}
}
}(this, function() {
"use strict";
var nextTick = this.setImmediate || setTimeout,
noop = function() {}

// Chai Plugin Definition
return function chaiCheckmark(chai, util) {
var Assertion = chai.Assertion,
expect = chai.expect

function check(done) {
/*jshint validthis:true */
var total = util.flag(this, "object"),
count = 0
expect(total).a("number").above(0, "Provide a count to check")
if (done) {
expect(done).a("function", "Provide a function for check")
}

function chain() {
expect(count).below(total,
"Target checkmarks already reached")
count += 1;
}
function mark() {
if (count === total && done) { nextTick(done, 0) }
return noop;
}
mark.getCount = function() { return count; }

Assertion.addChainableMethod("mark", mark, chain)
return mark;
}
Assertion.addMethod("check", check)
Assertion.addMethod("checks", check)
}
}))
;(function(context, factory) {
if (typeof require === "function" &&
typeof exports === "object" &&
typeof module === "object"
) {
module.exports = factory()
} else {
if (typeof define === "function" &&
typeof define.amd === "object"
) {
define(factory)
} else {
if (!context.chai) {
throw new Error("chai-checkmark couldn't find Chai.")
}
context.chai.use(factory())
}
}
}(this, function() {
"use strict";
var nextTick = (typeof setImmediate === "function" ?
setImmediate : setTimeout),
noop = function() {}

// Chai Plugin Definition
return function chaiCheckmark(chai, util) {
var Assertion = chai.Assertion,
expect = chai.expect

function check(done) {
/*jshint validthis:true */
var total = util.flag(this, "object"),
count = 0
expect(total).a("number").above(0, "Provide a count to check")
if (done) {
expect(done).a("function", "Provide a function for check")
}

function chain() {
expect(count).below(total,
"Target checkmarks already reached")
count += 1;
}
function mark() {
if (count === total && done) { nextTick(done, 0) }
return noop;
}
mark.getCount = function() { return count; }

Assertion.addChainableMethod("mark", mark, chain)
return mark;
}
Assertion.addMethod("check", check)
Assertion.addMethod("checks", check)
}
}))
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "chai-checkmark",
"version": "1.0.0",
"version": "1.0.1",
"description": "A Chai plugin for counting assertions.",
"main": "chai-checkmark.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha",
"lint": "jshint --exclude node_modules/ ./"
},
"repository": {
Expand All @@ -22,6 +22,8 @@
},
"homepage": "https://github.com/sirlancelot/chai-checkmark",
"devDependencies": {
"jshint": "^2.5.10"
"chai": "^1.10.0",
"jshint": "^2.5.11",
"mocha": "^2.0.1"
}
}
12 changes: 12 additions & 0 deletions test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

var chai = require("chai")
var chaiCheckmark = require("../chai-checkmark")

chai.should()
chai.use(chaiCheckmark)

global.chaiCheckmark = chaiCheckmark
global.expect = chai.expect
global.Assertion = chai.Assertion
global.AssertionError = chai.AssertionError
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--reporter spec
--require test/bootstrap
69 changes: 69 additions & 0 deletions test/test-chai-checkmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"use strict";
/* global describe, it, expect, Assertion, AssertionError */

describe("Chai Checkmark", function() {
it("should add a method to Assertion", function() {
expect(Assertion).to.respondTo("check")
expect(Assertion).to.respondTo("checks")
})

describe("Assertion#Check()", function() {
it("should return a function with methods", function() {
var subject = expect(1).check()

expect(subject).to.be.a("function")

expect(subject).itself.to.respondTo("getCount")
})

it("should accept only numbers greater than 0", function() {
expect(subject(-1)).to.Throw(AssertionError)
expect(subject(0)).to.Throw(AssertionError)
expect(subject("wat")).to.Throw(AssertionError)
expect(subject(NaN)).to.Throw(AssertionError)

subject(1)()
subject(10)()
subject(100)()

function subject(value) {
return function() {
return expect(value).check();
};
}
})

it("should allow a callback function to be passed in", function() {
expect(subject("wat")).to.Throw(AssertionError)

subject(function() {})()

function subject(callback) {
return function() {
return expect(1).check(callback);
};
}
})
})

describe("Assertion#Mark()", function() {
it("should increment the count", function() {
var mark = expect(2).checks()

expect(mark.getCount()).to.equal(0).mark()
expect(mark.getCount()).to.equal(1).mark()
expect(mark.getCount()).to.equal(2)
})

it("should throw an error if too many checks happened", function() {
expect(1).check()

subject()
expect(subject).to.Throw(AssertionError)

function subject() {
return expect().mark();
}
})
})
})

0 comments on commit 6473540

Please sign in to comment.