|
| 1 | + |
| 2 | +# Expect |
| 3 | + |
| 4 | +Minimalistic BDD assertion toolkit based on |
| 5 | +[should.js](http://github.com/visionmedia/should.js) |
| 6 | + |
| 7 | +```js |
| 8 | +expect(window.r).to.be(undefined); |
| 9 | +expect({ a: 'b' }).to.eql({ a: 'b' }) |
| 10 | +expect(5).to.be.a('number'); |
| 11 | +expect([]).to.be.an('array'); |
| 12 | +expect(window).not.to.be.an(Image); |
| 13 | +``` |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- Cross-browser: works on IE6+, Firefox, Safari, Chrome, Opera. |
| 18 | +- Compatible with all test frameworks. |
| 19 | +- Node.JS ready (`require('expect')`). |
| 20 | +- Standalone. Single global with no prototype extensions or shims. |
| 21 | + |
| 22 | +## How to use |
| 23 | + |
| 24 | +### Node |
| 25 | + |
| 26 | +Install it with NPM or add it to your `package.json`: |
| 27 | + |
| 28 | +``` |
| 29 | +$ npm install expect |
| 30 | +``` |
| 31 | + |
| 32 | +Then: |
| 33 | + |
| 34 | +```js |
| 35 | +var expect = require('expect'); |
| 36 | +``` |
| 37 | + |
| 38 | +### Browser |
| 39 | + |
| 40 | +Expose the `expect.js` found at the top level of this repository. |
| 41 | + |
| 42 | +```html |
| 43 | +<script src="expect.js"></script> |
| 44 | +``` |
| 45 | + |
| 46 | +## API |
| 47 | + |
| 48 | +**ok**: asserts that the value is _truthy_ or not |
| 49 | + |
| 50 | +```js |
| 51 | +expect(1).to.be.ok(); |
| 52 | +expect(true).to.be.ok(); |
| 53 | +expect({}).to.be.ok(); |
| 54 | +expect(0).to.not.be.ok(); |
| 55 | +``` |
| 56 | + |
| 57 | +**be** / **equal**: asserts `===` equality |
| 58 | + |
| 59 | +```js |
| 60 | +expect(1).to.be(1) |
| 61 | +expect(NaN).not.to.equal(NaN); |
| 62 | +expect(1).not.to.be(true) |
| 63 | +expect('1').to.not.be(1); |
| 64 | +``` |
| 65 | + |
| 66 | +**eql**: asserts loose equality that works with objects |
| 67 | + |
| 68 | +```js |
| 69 | +expect({ a: 'b' }).to.eql({ a: 'b' }); |
| 70 | +expect(1).to.eql('1'); |
| 71 | +``` |
| 72 | + |
| 73 | +**a**/**an**: asserts `typeof` with support for `array` type and `instanceof` |
| 74 | + |
| 75 | +```js |
| 76 | +// typeof with optional `array` |
| 77 | +expect(5).to.be.a('number'); |
| 78 | +expect([]).to.be.an('array'); // works |
| 79 | +expect([]).to.be.an('object'); // works too, since it uses `typeof` |
| 80 | + |
| 81 | +// constructors |
| 82 | +expect(5).to.be.a(Number); |
| 83 | +expect([]).to.be.an(Array); |
| 84 | +expect(tobi).to.be.a(Ferret); |
| 85 | +expect(person).to.be.a(Mammal); |
| 86 | +``` |
| 87 | + |
| 88 | +**match**: asserts `String` regular expression match |
| 89 | + |
| 90 | +```js |
| 91 | +program.version.should.match(/[0-9]+\.[0-9]+\.[0-9]+/); |
| 92 | +``` |
| 93 | + |
| 94 | +**contain**: asserts indexOf for an array or string |
| 95 | + |
| 96 | +```js |
| 97 | +expect([1, 2]).to.contain(1); |
| 98 | +expect('hello world').to.contain('world'); |
| 99 | +``` |
| 100 | + |
| 101 | +**length**: asserts array `.length` |
| 102 | + |
| 103 | +```js |
| 104 | +expect([]).to.have.length(0); |
| 105 | +expect([1,2,3]).to.have.length(3); |
| 106 | +``` |
| 107 | + |
| 108 | +**empty**: asserts that an array is empty or not |
| 109 | + |
| 110 | +```js |
| 111 | +expect([]).to.be.empty(); |
| 112 | +expect([1,2,3]).to.not.be.empty(); |
| 113 | +``` |
| 114 | + |
| 115 | +**property**: asserts presence of an own property (and value optionally) |
| 116 | + |
| 117 | +```js |
| 118 | +expect(window).to.have.property('expect') |
| 119 | +expect(window).to.have.property('expect', expect) |
| 120 | +expect({a: 'b'}).to.have.property('a'); |
| 121 | +``` |
| 122 | + |
| 123 | +**key**/**keys**: asserts the presence of a key. Supports the `only` modifier |
| 124 | + |
| 125 | +```js |
| 126 | +expect({ a: 'b' }).to.have.key('a'); |
| 127 | +expect({ a: 'b', c: 'd' }).to.only.have.keys('a', 'c'); |
| 128 | +expect({ a: 'b', c: 'd' }).to.only.have.keys(['a', 'c']); |
| 129 | +expect({ a: 'b', c: 'd' }).to.not.only.have.key('a'); |
| 130 | +``` |
| 131 | + |
| 132 | +**throwException**: asserts that the `Function` throws or not when called |
| 133 | + |
| 134 | +```js |
| 135 | +expect(fn).to.throwException(); |
| 136 | +expect(fn2).to.not.throwException(); |
| 137 | +``` |
| 138 | + |
| 139 | +**within**: asserts a number within a range |
| 140 | + |
| 141 | +```js |
| 142 | +expect(1).to.be.within(0, Infinity); |
| 143 | +``` |
| 144 | + |
| 145 | +**greaterThan**/**above**: asserts `>` |
| 146 | + |
| 147 | +```js |
| 148 | +expect(3).to.be.above(0); |
| 149 | +expect(5).to.be.greaterThan(3); |
| 150 | +``` |
| 151 | + |
| 152 | +**lessThan**/**below**: asserts `<` |
| 153 | + |
| 154 | +```js |
| 155 | +expect(0).to.be.below(3); |
| 156 | +expect(1).to.be.lessThan(3); |
| 157 | +``` |
| 158 | + |
| 159 | +## Using with a test framework |
| 160 | + |
| 161 | +For example, if you create a test suite with |
| 162 | +[mocha](http://github.com/visionmedia.mocha). |
| 163 | + |
| 164 | +Let's say we wanted to test the following program: |
| 165 | + |
| 166 | +**math.js** |
| 167 | + |
| 168 | +```js |
| 169 | +function add (a, b) { return a + b; }; |
| 170 | +``` |
| 171 | + |
| 172 | +Our test file would look like this: |
| 173 | + |
| 174 | +```js |
| 175 | +describe('test suite', function () { |
| 176 | + it('should expose a function', function () { |
| 177 | + expect(add).to.be.a('function'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should do math', function () { |
| 181 | + expect(add(1, 3)).to.equal(4); |
| 182 | + }); |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +If a certain expectation fails, an exception will be raised which gets captured |
| 187 | +and shown/processed by the test runner. |
| 188 | + |
| 189 | +## Differences with should.js |
| 190 | + |
| 191 | +- No need for static `should` methods like `should.strictEqual`. For example, |
| 192 | + `expect(obj).to.be(undefined)` works well. |
| 193 | +- Some API simplifications / changes. |
| 194 | +- API changes related to browser compatibility. |
| 195 | + |
| 196 | +## Running tests |
| 197 | + |
| 198 | +Clone the repository and install the developer dependencies: |
| 199 | + |
| 200 | +``` |
| 201 | +git clone git://github.com/LearnBoost/expect.js.git expect |
| 202 | +cd expect && npm install |
| 203 | +``` |
| 204 | + |
| 205 | +### Node |
| 206 | + |
| 207 | +`make test` |
| 208 | + |
| 209 | +### Browser |
| 210 | + |
| 211 | +`make test-browser` |
| 212 | + |
| 213 | +and point your browser(s) to `http://localhost:3000` |
| 214 | + |
| 215 | +## Credits |
| 216 | + |
| 217 | +(The MIT License) |
| 218 | + |
| 219 | +Copyright (c) 2011 Guillermo Rauch <[email protected]> |
| 220 | + |
| 221 | +Permission is hereby granted, free of charge, to any person obtaining |
| 222 | +a copy of this software and associated documentation files (the |
| 223 | +'Software'), to deal in the Software without restriction, including |
| 224 | +without limitation the rights to use, copy, modify, merge, publish, |
| 225 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 226 | +permit persons to whom the Software is furnished to do so, subject to |
| 227 | +the following conditions: |
| 228 | + |
| 229 | +The above copyright notice and this permission notice shall be |
| 230 | +included in all copies or substantial portions of the Software. |
| 231 | + |
| 232 | +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| 233 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 234 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 235 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 236 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 237 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 238 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 239 | + |
| 240 | +### 3rd-party |
| 241 | + |
| 242 | +Heavily borrows from [should.js](http://github.com/visionmedia/should.js) by TJ |
| 243 | +Holowaychuck - MIT. |
0 commit comments