Comparing version
2.0.0 / 2013-10-10 | ||
================== | ||
* breaking change: #a now getter like #an. Was replaced with #type(str) | ||
* breaking change: #empty does not check length for objects. Now it check if object do not have own properties. | ||
* #properties check if object have some properties | ||
* util.inspect now exposed as should.inspect | ||
* assertions for NaN, Infinity, Array, Object, String, Boolean, Number, Error, Function | ||
* #equal got alias #exactly | ||
1.3.0 / 2013-09-13 | ||
@@ -3,0 +13,0 @@ ================== |
// Taken from node's assert module, because it sucks | ||
// and exposes next to nothing useful. | ||
var util = require('./util'); | ||
@@ -8,26 +9,2 @@ module.exports = _deepEqual; | ||
function isBuffer(arg) { | ||
return arg instanceof Buffer; | ||
} | ||
function isDate(d) { | ||
return isObject(d) && objectToString(d) === '[object Date]'; | ||
} | ||
function objectToString(o) { | ||
return Object.prototype.toString.call(o); | ||
} | ||
function isObject(arg) { | ||
return typeof arg === 'object' && arg !== null; | ||
} | ||
function isRegExp(re) { | ||
return isObject(re) && objectToString(re) === '[object RegExp]'; | ||
} | ||
function isNullOrUndefined(arg) { | ||
return arg == null; | ||
} | ||
function _deepEqual(actual, expected) { | ||
@@ -38,3 +15,3 @@ // 7.1. All identical values are equivalent, as determined by ===. | ||
} else if (isBuffer(actual) && isBuffer(expected)) { | ||
} else if (util.isBuffer(actual) && util.isBuffer(expected)) { | ||
if (actual.length != expected.length) return false; | ||
@@ -50,3 +27,3 @@ | ||
// equivalent if it is also a Date object that refers to the same time. | ||
} else if (isDate(actual) && isDate(expected)) { | ||
} else if (util.isDate(actual) && util.isDate(expected)) { | ||
return actual.getTime() === expected.getTime(); | ||
@@ -57,3 +34,3 @@ | ||
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). | ||
} else if (isRegExp(actual) && isRegExp(expected)) { | ||
} else if (util.isRegExp(actual) && util.isRegExp(expected)) { | ||
return actual.source === expected.source && | ||
@@ -67,3 +44,3 @@ actual.global === expected.global && | ||
// equivalence is determined by ==. | ||
} else if (!isObject(actual) && !isObject(expected)) { | ||
} else if (!util.isObject(actual) && !util.isObject(expected)) { | ||
return actual == expected; | ||
@@ -82,8 +59,5 @@ | ||
function isArguments (object) { | ||
return objectToString(object) === '[object Arguments]'; | ||
} | ||
function objEquiv (a, b) { | ||
if (isNullOrUndefined(a) || isNullOrUndefined(b)) | ||
if (util.isNullOrUndefined(a) || util.isNullOrUndefined(b)) | ||
return false; | ||
@@ -94,4 +68,4 @@ // an identical 'prototype' property. | ||
// Converting to array solves the problem. | ||
if (isArguments(a)) { | ||
if (!isArguments(b)) { | ||
if (util.isArguments(a)) { | ||
if (!util.isArguments(b)) { | ||
return false; | ||
@@ -98,0 +72,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
exports.isWrapperType = function(obj) { | ||
return obj instanceof Number || obj instanceof String || obj instanceof Boolean; | ||
return isNumber(obj) || isString(obj) || isBoolean(obj); | ||
} | ||
@@ -34,2 +34,68 @@ | ||
return a; | ||
}; | ||
}; | ||
function isNumber(arg) { | ||
return typeof arg === 'number' || arg instanceof Number; | ||
} | ||
exports.isNumber = isNumber; | ||
function isString(arg) { | ||
return typeof arg === 'string' || arg instanceof String; | ||
} | ||
function isBoolean(arg) { | ||
return typeof arg === 'boolean' || arg instanceof Boolean; | ||
} | ||
exports.isBoolean = isBoolean; | ||
exports.isString = isString; | ||
function isBuffer(arg) { | ||
return typeof Buffer !== 'undefined' && arg instanceof Buffer; | ||
} | ||
exports.isBuffer = isBuffer; | ||
function isDate(d) { | ||
return isObject(d) && objectToString(d) === '[object Date]'; | ||
} | ||
exports.isDate = isDate; | ||
function objectToString(o) { | ||
return Object.prototype.toString.call(o); | ||
} | ||
function isObject(arg) { | ||
return typeof arg === 'object' && arg !== null; | ||
} | ||
exports.isObject = isObject; | ||
function isRegExp(re) { | ||
return isObject(re) && objectToString(re) === '[object RegExp]'; | ||
} | ||
exports.isRegExp = isRegExp; | ||
function isNullOrUndefined(arg) { | ||
return arg == null; | ||
} | ||
exports.isNullOrUndefined = isNullOrUndefined; | ||
function isArguments(object) { | ||
return objectToString(object) === '[object Arguments]'; | ||
} | ||
exports.isArguments = isArguments; | ||
exports.isFunction = function(arg) { | ||
return typeof arg === 'function' || arg instanceof Function; | ||
}; | ||
function isError(e) { | ||
return isObject(e) && objectToString(e) === '[object Error]'; | ||
} | ||
exports.isError = isError; |
{ "name": "should" | ||
, "description": "test framework agnostic BDD-style assertions" | ||
, "version": "1.3.0" | ||
, "version": "2.0.0" | ||
, "author": "TJ Holowaychuk <tj@vision-media.ca>" | ||
@@ -5,0 +5,0 @@ , "repository": { "type": "git", "url": "git://github.com/visionmedia/should.js.git" } |
157
Readme.md
@@ -0,1 +1,3 @@ | ||
# should.js | ||
_should_ is an expressive, readable, test framework agnostic, assertion library for [node](http://nodejs.org). | ||
@@ -5,6 +7,6 @@ | ||
_should_ literally extends 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_. | ||
## Example | ||
```javascript | ||
var should = require('should'); | ||
var user = { | ||
@@ -19,4 +21,4 @@ name: 'tj' | ||
// or without Object.prototype, for guys how did Object.create(null) | ||
should(user).have.property('name', 'tj'); | ||
should(true).ok; | ||
@@ -31,19 +33,13 @@ someAsyncTask(foo, function(err, result){ | ||
$ npm install should | ||
$ npm install should --save-dev | ||
## assert extras | ||
## In browser | ||
As mentioned above, _should_ extends node's _assert_. The returned object from `require('should')` is thus similar to the returned object from `require('assert')`, but it has one extra convenience method: | ||
```javascript | ||
should.exist('hello') | ||
should.exist([]) | ||
should.exist(null) // will throw | ||
If you want to use _should_ in browser, use version that is in root of repository. It is build with browserify (see [Makefile](https://github.com/visionmedia/should.js/blob/master/Makefile) about how it is build). To build fresh version: | ||
```bash | ||
# you should have browserify | ||
npm install -g browserify | ||
make browser | ||
``` | ||
This is equivalent to `should.ok`, which is equivalent to `assert.ok`, but reads a bit better. It gets better, though: | ||
```javascript | ||
should.not.exist(false) | ||
should.not.exist('') | ||
should.not.exist({}) // will throw | ||
``` | ||
We may add more _assert_ extras in the future... ;) | ||
@@ -60,38 +56,26 @@ ## chaining assertions | ||
``` | ||
our dummy getters such as _and_ also help express chaining: | ||
our dummy getters such as _a_, _an_, _be_ and _have_ make tests more readable while the getters _and_ and _with_ helps express chaining: | ||
```javascript | ||
user.should.be.a('object').and.have.property('name', 'tj') | ||
user.should.be.an.instanceOf(Object).and.have.property('name', 'tj') | ||
user.should.have.property('pets').with.a.lengthOf(4) | ||
``` | ||
## exist (static) | ||
The returned object from `require('should')` is the same object as `require('assert')`. So you can use `should` just like `assert`: | ||
## static | ||
For some rare cases should can be used statically, without `Object.prototype`. | ||
It can be replacement for node assert module (and it uses the same `AssertionError`): | ||
```javascript | ||
should.fail('expected an error!') | ||
should.strictEqual(foo, bar) | ||
assert.fail(actual, expected, message, operator) // just write wrong should assertion | ||
assert(value, message), assert.ok(value, [message]) // should(value).ok | ||
assert.equal(actual, expected, [message]) // should(actual).eql(expected, [message]) | ||
assert.notEqual(actual, expected, [message]) // should(actual).not.eql(expected, [message]) | ||
assert.deepEqual(actual, expected, [message]) // should(actual).eql(expected, [message]) | ||
assert.notDeepEqual(actual, expected, [message]) // should(actual).not.eql(expected, [message]) | ||
assert.strictEqual(actual, expected, [message]) // should(actual).equal(expected, [message]) | ||
assert.notStrictEqual(actual, expected, [message]) // should(actual).not.equal(expected, [message]) | ||
assert.throws(block, [error], [message]) // should(block).throw([error]) | ||
assert.doesNotThrow(block, [message]) // should(block).not.throw([error]) | ||
assert.ifError(value) // should(value).Error (to check if it is error) or should(value).not.ok (to check that it is falsy) | ||
``` | ||
In general, using the Object prototype's _should_ is nicer than using these `assert` equivalents, because _should_ gives you access to the expressive and readable language described above: | ||
```javascript | ||
foo.should.equal(bar) // same as should.strictEqual(foo, bar) above | ||
``` | ||
The only exception, though, is when you can't be sure that a particular object exists. In that case, attempting to access the _should_ property may throw a TypeError: | ||
```javascript | ||
foo.should.equal(bar) // throws if foo is null or undefined! | ||
``` | ||
For this case, `require('should')` extends `require('assert')` with an extra convenience method to check whether an object exists: | ||
```javascript | ||
should.exist({}) | ||
should.exist([]) | ||
should.exist('') | ||
should.exist(0) | ||
should.exist(null) // will throw | ||
should.exist(undefined) // will throw | ||
``` | ||
You can also check the negation: | ||
```javascript | ||
should.not.exist(undefined) | ||
should.not.exist(null) | ||
should.not.exist('') // will throw | ||
should.not.exist({}) // will throw | ||
``` | ||
Once you know an object exists, you can safely use the _should_ property on it. | ||
@@ -136,7 +120,10 @@ ## ok | ||
Asserts that length is 0: | ||
Asserts that given object is empty | ||
```javascript | ||
[].should.be.empty | ||
''.should.be.empty | ||
({ length: 0 }).should.be.empty | ||
({}).should.be.empty | ||
(function() { | ||
arguments.should.be.empty; | ||
})() | ||
``` | ||
@@ -150,3 +137,3 @@ ## eql | ||
``` | ||
## equal | ||
## equal and exactly | ||
@@ -160,2 +147,3 @@ strict equality: | ||
[1,2,3].should.not.equal([1,2,3]) | ||
(4).should.be.exactly(4) | ||
``` | ||
@@ -174,8 +162,8 @@ ## within | ||
``` | ||
## a | ||
## type | ||
Assert __typeof__: | ||
```javascript | ||
user.should.be.a('object') | ||
'test'.should.be.a('string') | ||
user.should.have.type('object') | ||
'test'.should.have.type('string') | ||
``` | ||
@@ -203,2 +191,14 @@ ## instanceof and instanceOf | ||
``` | ||
## NaN | ||
Assert numeric valus is NaN: | ||
```javascript | ||
(undefined + 0).should.be.NaN; | ||
``` | ||
## Infinity | ||
Assert numeric valus is Infinity: | ||
```javascript | ||
(1/0).should.be.Infinity; | ||
``` | ||
## match | ||
@@ -216,2 +216,3 @@ | ||
user.pets.should.have.a.lengthOf(5) | ||
({ length: 10}).should.have.length(10); | ||
``` | ||
@@ -229,2 +230,10 @@ Aliases: _lengthOf_ | ||
``` | ||
## properties | ||
Assert given properties exists: | ||
```javascript | ||
user.should.have.properties('name', 'age'); | ||
user.should.have.properties(['name', 'age']); | ||
``` | ||
## ownProperty | ||
@@ -263,5 +272,5 @@ | ||
## include(obj) | ||
## include(obj) or contain(obj) | ||
Assert that the given `obj` is present via `indexOf()`, so this works for strings, arrays, or custom objects implementing indexOf. | ||
Assert that the given `obj` is present via `indexOf()`, so this works for strings, arrays, or custom objects implementing indexOf. Also it can assert if given object will have some sub-object. | ||
@@ -317,3 +326,3 @@ Assert array value: | ||
``` | ||
Assert exepection message matches string: | ||
Assert exception message matches string: | ||
@@ -371,2 +380,12 @@ ```js | ||
## type assertions | ||
```javascript | ||
({}).should.be.an.Object; | ||
(1).should.be.an.Number; | ||
[].should.be.an.Array; | ||
(true).should.be.a.Boolean; | ||
''.should.be.a.String; | ||
``` | ||
## Optional Error description | ||
@@ -384,18 +403,21 @@ | ||
## Express example | ||
## Mocha example | ||
For example you can use should with the [Expresso TDD Framework](http://github.com/visionmedia/expresso) by simply including it: | ||
For example you can use should with the [Mocha test framework](http://visionmedia.github.io/mocha/) by simply including it: | ||
var lib = require('mylib') | ||
, should = require('should'); | ||
```javascript | ||
var should = require('should'); | ||
var mylib = require('mylib'); | ||
module.exports = { | ||
'test .version': function(){ | ||
lib.version.should.match(/^\d+\.\d+\.\d+$/); | ||
} | ||
}; | ||
describe('mylib', function () { | ||
it('should have a version with the format #.#.#', function() { | ||
lib.version.should.match(/^\d+\.\d+\.\d+$/); | ||
} | ||
}); | ||
``` | ||
## Running tests | ||
To run the tests for _should_ simply update your git submodules and run: | ||
To run the tests for _should_ simply run: | ||
@@ -413,2 +435,3 @@ $ make test | ||
Copyright (c) 2010-2011 TJ Holowaychuk <tj@vision-media.ca> | ||
Copyright (c) 2011 Aseem Kishore <aseem.kishore@gmail.com> | ||
@@ -415,0 +438,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
441
5.5%23213
-62.81%9
-30.77%220
-85.1%