Skip to content

Commit

Permalink
Merge pull request tj#57 from gabrielfalcao/master
Browse files Browse the repository at this point in the history
adding an alias for `throw`as `crash`, avoiding problems with jshint checks in strict mode
  • Loading branch information
tj committed May 31, 2012
2 parents 15b06c8 + 465f838 commit 6a47b14
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
32 changes: 22 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
_should_ is an expressive, readable, test framework agnostic, assertion library for [node](http://nodejs.org).

It extends the Object prototype with a single non-enumerable getter that allows you to express how that object should behave.

_should_ literally extends node's _assert_ module, in fact, it is node's assert module, for example `should.equal(str, 'foo')` will work, just as `assert.equal(str, 'foo')` would, and `should.AssertionError` **is** `assert.AssertionError`, meaning any test framework supporting this constructor will function properly with _should_.
Expand All @@ -13,7 +13,7 @@ _should_ literally extends node's _assert_ module, in fact, it is node's assert

user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4);

someAsyncTask(foo, function(err, result){
should.not.exist(err);
should.exist(result);
Expand Down Expand Up @@ -161,12 +161,12 @@ Assert __typeof__:
user.should.be.a('object')
'test'.should.be.a('string')

## instanceof
## instanceof and instanceOf

Assert __instanceof__:
Assert __instanceof__ or __instanceOf__:

user.should.be.an.instanceof(User)
[].should.be.an.instanceof(Array)
[].should.be.an.instanceOf(Array)

## above

Expand Down Expand Up @@ -229,13 +229,13 @@ Assert own property (on the immediate object):
## json

Assert that Content-Type is "application/json; charset=utf-8"

res.should.be.json

## html

Assert that Content-Type is "text/html; charset=utf-8"

res.should.be.html

## include(obj)
Expand Down Expand Up @@ -288,7 +288,7 @@ Assert an exception is not thrown:

```js
(function(){

}).should.not.throw();
```
Assert exepection message matches string:
Expand All @@ -307,6 +307,18 @@ Assert exepection message matches regexp:
}).should.throw(/^fail/);
```

## throwError()

An alias of `throw`, its purpose is to be an option for those who run
[jshint](https://github.com/jshint/node-jshint/) in strict mode.

```js
(function(){
throw new Error('failed to baz');
}).should.throwError(/^fail.*/);
```


## keys

Assert own object keys, which must match _exactly_,
Expand Down Expand Up @@ -334,7 +346,7 @@ For example you can use should with the [Expresso TDD Framework](http://github.c

var lib = require('mylib')
, should = require('should');

module.exports = {
'test .version': function(){
lib.version.should.match(/^\d+\.\d+\.\d+$/);
Expand All @@ -351,7 +363,7 @@ To run the tests for _should_ simply update your git submodules and run:

Yes, yes it does, with a single getter _should_, and no it won't break your code, because it does this **properly** with a non-enumerable property.

## License
## License

(The MIT License)

Expand Down
3 changes: 3 additions & 0 deletions lib/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,10 @@ Assertion.prototype = {
return this;
}
};

Assertion.prototype.instanceOf = Assertion.prototype.instanceof;
Assertion.prototype.throwError = Assertion.prototype.throw;

/**
* Aliases.
*/
Expand Down
40 changes: 40 additions & 0 deletions test/should.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,5 +573,45 @@ module.exports = {
err(function(){
(function(){ throw new Error('error'); }).should.throw('fail');
}, "expected an exception to be thrown with a message matching 'fail', but got 'error'");
},

'test throwError()': function(){
(function(){}).should.not.throwError();
(function(){ throw new Error('fail') }).should.throwError();

err(function(){
(function(){}).should.throwError();
}, 'expected an exception to be thrown');

err(function(){
(function(){
throw new Error('fail');
}).should.not.throwError();
}, 'expected no exception to be thrown, got "fail"');
},

'test throwError() with regex message': function(){
(function(){ throw new Error('fail'); }).should.throwError(/fail/);

err(function(){
(function(){}).should.throwError(/fail/);
}, 'expected an exception to be thrown');

err(function(){
(function(){ throw new Error('error'); }).should.throwError(/fail/);
}, "expected an exception to be thrown with a message matching /fail/, but got 'error'");
},

'test throwError() with string message': function(){
(function(){ throw new Error('fail'); }).should.throwError('fail');

err(function(){
(function(){}).should.throwError('fail');
}, 'expected an exception to be thrown');

err(function(){
(function(){ throw new Error('error'); }).should.throwError('fail');
}, "expected an exception to be thrown with a message matching 'fail', but got 'error'");
}

};

0 comments on commit 6a47b14

Please sign in to comment.