Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

should

Package Overview
Dependencies
Maintainers
1
Versions
114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

should - npm Package Compare versions

Comparing version 0.4.2 to 0.5.0

10

History.md
0.5.0 / 2012-01-12
==================
* Added string matching to `.throw()` [serby]
* Added regexp matching to `.throw()` [serby]
* Added `.includeEql()` [RubenVerborgh]
* Added `.should.be.html`
* Added `.should.be.json`
* Added optional description args to most matchers [Mike Swift]
0.4.2 / 2011-12-17

@@ -3,0 +13,0 @@ ==================

169

lib/should.js

@@ -35,3 +35,3 @@ /*!

exports.version = '0.4.2';
exports.version = '0.5.0';

@@ -277,10 +277,11 @@ /**

* @param {Mixed} val
* @param {String} description
* @api public
*/
eql: function(val){
eql: function(val, desc){
this.assert(
eql(val, this.obj)
, 'expected ' + this.inspect + ' to equal ' + i(val)
, 'expected ' + this.inspect + ' to not equal ' + i(val));
, 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : ""));
return this;

@@ -293,10 +294,11 @@ },

* @param {Mixed} val
* @param {String} description
* @api public
*/
equal: function(val){
equal: function(val, desc){
this.assert(
val === this.obj
, 'expected ' + this.inspect + ' to equal ' + i(val)
, 'expected ' + this.inspect + ' to not equal ' + i(val));
, 'expected ' + this.inspect + ' to equal ' + i(val) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not equal ' + i(val) + (desc ? " | " + desc : ""));
return this;

@@ -310,11 +312,12 @@ },

* @param {Number} finish
* @param {String} description
* @api public
*/
within: function(start, finish){
within: function(start, finish, desc){
var range = start + '..' + finish;
this.assert(
this.obj >= start && this.obj <= finish
, 'expected ' + this.inspect + ' to be within ' + range
, 'expected ' + this.inspect + ' to not be within ' + range);
, 'expected ' + this.inspect + ' to be within ' + range + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not be within ' + range + (desc ? " | " + desc : ""));
return this;

@@ -326,10 +329,12 @@ },

*
* @param {Mixed} type
* @param {String} description
* @api public
*/
a: function(type){
a: function(type, desc){
this.assert(
type == typeof this.obj
, 'expected ' + this.inspect + ' to be a ' + type
, 'expected ' + this.inspect + ' not to be a ' + type);
, 'expected ' + this.inspect + ' to be a ' + type + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' not to be a ' + type + (desc ? " | " + desc : ""));
return this;

@@ -341,11 +346,13 @@ },

*
* @param {Function} constructor
* @param {String} description
* @api public
*/
instanceof: function(constructor){
instanceof: function(constructor, desc){
var name = constructor.name;
this.assert(
this.obj instanceof constructor
, 'expected ' + this.inspect + ' to be an instance of ' + name
, 'expected ' + this.inspect + ' not to be an instance of ' + name);
, 'expected ' + this.inspect + ' to be an instance of ' + name + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' not to be an instance of ' + name + (desc ? " | " + desc : ""));
return this;

@@ -358,10 +365,11 @@ },

* @param {Number} n
* @param {String} description
* @api public
*/
above: function(n){
above: function(n, desc){
this.assert(
this.obj > n
, 'expected ' + this.inspect + ' to be above ' + n
, 'expected ' + this.inspect + ' to be below ' + n);
, 'expected ' + this.inspect + ' to be above ' + n + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to be below ' + n + (desc ? " | " + desc : ""));
return this;

@@ -374,10 +382,11 @@ },

* @param {Number} n
* @param {String} description
* @api public
*/
below: function(n){
below: function(n, desc){
this.assert(
this.obj < n
, 'expected ' + this.inspect + ' to be below ' + n
, 'expected ' + this.inspect + ' to be above ' + n);
, 'expected ' + this.inspect + ' to be below ' + n + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to be above ' + n + (desc ? " | " + desc : ""));
return this;

@@ -390,10 +399,11 @@ },

* @param {RegExp} regexp
* @param {String} description
* @api public
*/
match: function(regexp){
match: function(regexp, desc){
this.assert(
regexp.exec(this.obj)
, 'expected ' + this.inspect + ' to match ' + regexp
, 'expected ' + this.inspect + ' not to match ' + regexp);
, 'expected ' + this.inspect + ' to match ' + regexp + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' not to match ' + regexp + (desc ? " | " + desc : ""));
return this;

@@ -406,6 +416,7 @@ },

* @param {Number} n
* @param {String} description
* @api public
*/
length: function(n){
length: function(n, desc){
this.obj.should.have.property('length');

@@ -415,23 +426,7 @@ var len = this.obj.length;

n == len
, 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len
, 'expected ' + this.inspect + ' to not have a length of ' + len);
, 'expected ' + this.inspect + ' to have a length of ' + n + ' but got ' + len + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have a length of ' + len + (desc ? " | " + desc : ""));
return this;
},
/**
* Assert substring.
*
* @param {String} str
* @api public
*/
string: function(str){
this.obj.should.be.a('string');
this.assert(
~this.obj.indexOf(str)
, 'expected ' + this.inspect + ' to include ' + i(str)
, 'expected ' + this.inspect + ' to not include ' + i(str));
return this;
},
/**

@@ -442,9 +437,10 @@ * Assert property _name_ exists, with optional _val_.

* @param {Mixed} val
* @param {String} description
* @api public
*/
property: function(name, val){
property: function(name, val, desc){
if (this.negate && undefined !== val) {
if (undefined === this.obj[name]) {
throw new Error(this.inspect + ' has no property ' + i(name));
throw new Error(this.inspect + ' has no property ' + i(name) + (desc ? " | " + desc : ""));
}

@@ -454,4 +450,4 @@ } else {

undefined !== this.obj[name]
, 'expected ' + this.inspect + ' to have a property ' + i(name)
, 'expected ' + this.inspect + ' to not have a property ' + i(name));
, 'expected ' + this.inspect + ' to have a property ' + i(name) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have a property ' + i(name) + (desc ? " | " + desc : ""));
}

@@ -463,4 +459,4 @@

, 'expected ' + this.inspect + ' to have a property ' + i(name)
+ ' of ' + i(val) + ', but got ' + i(this.obj[name])
, 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val));
+ ' of ' + i(val) + ', but got ' + i(this.obj[name]) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have a property ' + i(name) + ' of ' + i(val) + (desc ? " | " + desc : ""));
}

@@ -476,10 +472,11 @@

* @param {String} name
* @param {String} description
* @api public
*/
ownProperty: function(name){
ownProperty: function(name, desc){
this.assert(
this.obj.hasOwnProperty(name)
, 'expected ' + this.inspect + ' to have own property ' + i(name)
, 'expected ' + this.inspect + ' to not have own property ' + i(name));
, 'expected ' + this.inspect + ' to have own property ' + i(name) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not have own property ' + i(name) + (desc ? " | " + desc : ""));
return this;

@@ -492,14 +489,31 @@ },

* @param {Mixed} obj
* @param {String} description
* @api public
*/
include: function(obj){
include: function(obj, desc){
this.assert(
~this.obj.indexOf(obj)
, 'expected ' + this.inspect + ' to include ' + i(obj)
, 'expected ' + this.inspect + ' to not include ' + i(obj));
, 'expected ' + this.inspect + ' to include ' + i(obj) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not include ' + i(obj) + (desc ? " | " + desc : ""));
return this;
},
/**
* Assert that an object equal to `obj` is present.
*
* @param {Array} obj
* @param {String} description
* @api public
*/
includeEql: function(obj, desc){
this.assert(
this.obj.some(function(item) { return eql(obj, item); })
, 'expected ' + this.inspect + ' to include an object equal to ' + i(obj) + (desc ? " | " + desc : "")
, 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj) + (desc ? " | " + desc : ""));
return this;
},
/**

@@ -611,2 +625,24 @@ * Assert that the array contains _obj_.

/**
* Assert that this response has content-type: application/json.
*
* @return {Assertion} for chaining
* @api public
*/
get json() {
this.obj.should.have.header('content-type', 'application/json; charset=utf-8');
},
/**
* Assert that this response has content-type: text/html.
*
* @return {Assertion} for chaining
* @api public
*/
get html() {
this.obj.should.have.header('content-type', 'text/html; charset=utf-8');
},
/**
* Assert that this function will or will not

@@ -619,5 +655,6 @@ * throw an exception.

throw: function(){
throw: function(message){
var fn = this.obj
, err = {}
, errorInfo = ''
, ok = true;

@@ -632,5 +669,21 @@

if (ok) {
if ('string' == typeof message) {
ok = message == err.message;
} else if (message instanceof RegExp) {
ok = message.test(err.message);
}
if (message && !ok) {
if ('string' == typeof message) {
errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'";
} else {
errorInfo = " with a message matching " + message + ", but got '" + err.message + "'";
}
}
}
this.assert(
ok
, 'expected an exception to be thrown'
, 'expected an exception to be thrown' + errorInfo
, 'expected no exception to be thrown, got "' + err.message + '"');

@@ -637,0 +690,0 @@

{ "name": "should"
, "description": "test framework agnostic BDD-style assertions"
, "version": "0.4.2"
, "version": "0.5.0"
, "author": "TJ Holowaychuk <tj@vision-media.ca>"

@@ -5,0 +5,0 @@ , "contributors": [ "Aseem Kishore <aseem.kishore@gmail.com>" ]

@@ -5,3 +5,3 @@ _should_ is an expressive, readable, test framework agnostic, assertion library for [node](http://nodejs.org).

_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** `asset.AssertionError`, meaning any test framework supporting this constructor will function properly with _should_.
_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_.

@@ -201,10 +201,2 @@ ## Example

## string
Substring assertion:
'foobar'.should.include.string('foo')
'foobar'.should.include.string('bar')
'foobar'.should.not.include.string('baz')
## property

@@ -235,9 +227,21 @@

res.should.have.header('content-length');
res.should.have.header('Content-Length', '123');
res.should.have.header('content-length', '123');
res.should.have.header('content-length');
res.should.have.header('Content-Length', '123');
res.should.have.header('content-length', '123');
## 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)
Assert that the given `obj` is present via `indexOf()`:
Assert that the given `obj` is present via `indexOf()`, so this works for strings, arrays, or custom objects implementing indexOf:

@@ -257,2 +261,10 @@ Assert array value:

## includeEql(obj)
Assert that an object equal to the given `obj` is present in an Array:
[[1],[2],[3]].should.includeEql([3])
[[1],[2],[3]].should.includeEql([2])
[[1],[2],[3]].should.not.includeEql([4])
## throw()

@@ -283,2 +295,14 @@

## Optional Error description
As it can often be difficult to assertain exactly where failed assertions are comming from in your tests, an optional description parameter can be passed to several should matchers. The description will follow the failed assertion in the error:
(1).should.eql(0, 'some useful description')
AssertionError: expected 1 to equal 0 | some useful description
at Object.eql (/Users/swift/code/should.js/node_modules/should/lib/should.js:280:10)
...
The methods that support this optional description are: `eql`, `equal`, `within`, `a`, `instanceof`, `above`, `below`, `match`, `length`, `property`, `ownProperty`, `include`, and `includeEql`.
## Express example

@@ -285,0 +309,0 @@

@@ -83,2 +83,6 @@

}, "expected 'test' not to be a string");
err(function(){
'test'.should.not.be.a('string', 'foo');
}, "expected 'test' not to be a string | foo");

@@ -90,2 +94,6 @@ (5).should.be.a('number');

}, "expected 5 not to be a number");
err(function(){
(5).should.not.be.a('number', 'foo');
}, "expected 5 not to be a number | foo");
},

@@ -100,2 +108,6 @@

}, "expected 3 to be an instance of Foo");
err(function(){
(3).should.an.instanceof(Foo, 'foo');
}, "expected 3 to be an instance of Foo | foo");
},

@@ -116,2 +128,10 @@

}, "expected 10 to be within 50..100");
err(function(){
(5).should.not.be.within(4,6, 'foo');
}, "expected 5 to not be within 4..6 | foo");
err(function(){
(10).should.be.within(50,100, 'foo');
}, "expected 10 to be within 50..100 | foo");
},

@@ -132,3 +152,34 @@

}, "expected 10 to be below 6");
err(function(){
(5).should.be.above(6, 'foo');
}, "expected 5 to be above 6 | foo");
err(function(){
(10).should.not.be.above(6, 'foo');
}, "expected 10 to be below 6 | foo");
},
'test below(n)': function(){
(2).should.be.below(5);
(2).should.be.lessThan(5);
(5).should.not.be.below(5);
(6).should.not.be.below(5);
err(function(){
(6).should.be.below(5);
}, "expected 6 to be below 5");
err(function(){
(6).should.not.be.below(10);
}, "expected 6 to be above 10");
err(function(){
(6).should.be.below(5, 'foo');
}, "expected 6 to be below 5 | foo");
err(function(){
(6).should.not.be.below(10, 'foo');
}, "expected 6 to be above 10 | foo");
},

@@ -146,2 +197,10 @@ 'test match(regexp)': function(){

}, "expected 'foobar' not to match /^foo/i");
err(function(){
'foobar'.should.match(/^bar/i, 'foo')
}, "expected 'foobar' to match /^bar/i | foo");
err(function(){
'foobar'.should.not.match(/^foo/i, 'foo')
}, "expected 'foobar' not to match /^foo/i | foo");
},

@@ -161,2 +220,11 @@

}, "expected 'asd' to not have a length of 3");
err(function(){
'asd'.should.have.length(4, 'foo');
}, "expected 'asd' to have a length of 4 but got 3 | foo");
err(function(){
'asd'.should.not.have.length(3, 'foo');
}, "expected 'asd' to not have a length of 3 | foo");
},

@@ -173,2 +241,10 @@

}, 'expected 4 to equal 3');
err(function(){
(4).should.eql(3, "foo");
}, 'expected 4 to equal 3 | foo');
err(function(){
(3).should.not.eql(3, "foo");
}, 'expected 3 to not equal 3 | foo');
},

@@ -187,2 +263,10 @@

}, "expected '4' to equal 4");
err(function(){
(3).should.equal(4, "foo");
}, "expected 3 to equal 4 | foo");
err(function(){
(4).should.not.equal(4, "foo");
}, "expected 4 to not equal 4 | foo");
},

@@ -215,2 +299,10 @@

}, "expected 'asd' to have a property 'foo'");
err(function(){
'asd'.should.have.property('foo', undefined, 'foo');
}, "expected 'asd' to have a property 'foo' | foo");
err(function(){
'asd'.should.not.have.property('length', undefined, 'foo');
}, "expected 'asd' to not have a property 'length' | foo");
},

@@ -237,2 +329,18 @@

}, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String]");
err(function(){
'asd'.should.have.property('length', 4, 'foo');
}, "expected 'asd' to have a property 'length' of 4, but got 3 | foo");
err(function(){
'asd'.should.not.have.property('length', 3, 'foo');
}, "expected 'asd' to not have a property 'length' of 3 | foo");
err(function(){
'asd'.should.not.have.property('foo', 3, 'foo');
}, "'asd' has no property 'foo' | foo");
err(function(){
'asd'.should.have.property('constructor', Number, 'foo');
}, "expected 'asd' to have a property 'constructor' of [Function: Number], but got [Function: String] | foo");
},

@@ -248,2 +356,10 @@

}, "expected { length: 12 } to not have own property 'length'");
err(function(){
({ length: 12 }).should.not.have.ownProperty('length', 'foo');
}, "expected { length: 12 } to not have own property 'length' | foo");
err(function(){
({ length: 12 }).should.have.ownProperty('foo', 'foo');
}, "expected { length: 12 } to have own property 'foo' | foo");
},

@@ -263,2 +379,10 @@

}, "expected 'foobar' to not include 'bar'");
err(function(){
'foobar'.should.include('baz', 'foo');
}, "expected 'foobar' to include 'baz' | foo");
err(function(){
'foobar'.should.not.include('bar', 'foo');
}, "expected 'foobar' to not include 'bar' | foo");
},

@@ -281,4 +405,35 @@

}, "expected [ 'bar', 'foo' ] to not include 'foo'");
err(function(){
['foo'].should.include('bar', 'foo');
}, "expected [ 'foo' ] to include 'bar' | foo");
err(function(){
['bar', 'foo'].should.not.include('foo', 'foo');
}, "expected [ 'bar', 'foo' ] to not include 'foo' | foo");
},
'test includeEql() with array': function(){
[['foo'], ['bar']].should.includeEql(['foo']);
[['foo'], ['bar']].should.includeEql(['bar']);
[['foo'], ['bar']].should.not.includeEql(['baz']);
[].should.not.includeEql(['baz']);
err(function(){
[['foo']].should.includeEql(['bar']);
}, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ]");
err(function(){
[['foo']].should.not.includeEql(['foo']);
}, "expected [ [ 'foo' ] ] to not include an object equal to [ 'foo' ]");
err(function(){
[['foo']].should.includeEql(['bar'], 'foo');
}, "expected [ [ 'foo' ] ] to include an object equal to [ 'bar' ] | foo");
err(function(){
[['foo']].should.not.includeEql(['foo'], 'foo');
}, "expected [ [ 'foo' ] ] to not include an object equal to [ 'foo' ] | foo");
},
'test keys(array)': function(){

@@ -350,3 +505,27 @@ ({ foo: 1 }).should.have.keys(['foo']);

}, 'expected no exception to be thrown, got "fail"');
},
'test throw() with regex message': function(){
(function(){ throw new Error('fail'); }).should.throw(/fail/);
err(function(){
(function(){}).should.throw(/fail/);
}, 'expected an exception to be thrown');
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 throw() with string message': function(){
(function(){ throw new Error('fail'); }).should.throw('fail');
err(function(){
(function(){}).should.throw('fail');
}, 'expected an exception to be thrown');
err(function(){
(function(){ throw new Error('error'); }).should.throw('fail');
}, "expected an exception to be thrown with a message matching 'fail', but got 'error'");
}
};
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc