global
assertion assert
assertion bool
assertion chaining
assertion contain
assertion equality
assertion errors
assertion matching
assertion numbers
assertion property
assertion strings
assertion types
Assertion#Number
Assertion#arguments
Assertion#Arguments
→ Assertion#arguments
Assertion#type
Assertion#instanceof
Assertion#instanceOf
→ Assertion#instanceof
Assertion#Function
Assertion#Object
Assertion#String
Assertion#Array
Assertion#Boolean
Assertion#Error
Assertion#null
Assertion#Null
→ Assertion#null
“global” Members
should(obj)
Our function should
obj
(*): Object to assert
(should.Assertion)
:Returns new Assertion for beginning assertion chain
var should = require('should');
should('abc').be.a.string;
should.config
Object with configuration.
It contains such properties:
checkProtoEql
boolean - Affect if .eql
will check objects prototypesvar a = { a: 10 }, b = Object.create(null);
b.a = 10;
a.should.be.eql(b);
//not throws
should.config.checkProtoEql = true;
a.should.be.eql(b);
//throws AssertionError: expected { a: 10 } to equal { a: 10 } (because A and B have different prototypes)
should.extend([propertyName], [proto])
Allow to extend given prototype with should property using given name. This getter will unwrap all standard wrappers like Number
, Boolean
, String
.
Using should(obj)
is the equivalent of using obj.should
with known issues (like nulls and method calls etc).
[propertyName]
(string): Name of property to add. Default is 'should'
.
[proto]
(Object): Prototype to extend with. Default is Object.prototype
.
({ name: string, descriptor: Object, proto: Object })
:Descriptor enough to return all back
var prev = should.extend('must', Object.prototype);
'abc'.must.startWith('a');
var should = should.noConflict(prev);
should.not.exist(Object.prototype.must);
should.noConflict([desc])
Delete previous extension. If desc
missing it will remove default extension.
[desc]
({ name: string, descriptor: Object, proto: Object }): Returned from should.extend
object
(Function)
:Returns should function
var should = require('should').noConflict();
should(Object.prototype).not.have.property('should');
var prev = should.extend('must', Object.prototype);
'abc'.must.startWith('a');
should.noConflict(prev);
should(Object.prototype).not.have.property('must');
should.use(f)
Simple utility function for a bit more easier should assertion extension
f
(Function): So called plugin function. It should accept 2 arguments: should
function and Assertion
constructor
(Function)
:Returns should
function
should.use(function(should, Assertion) {
Assertion.add('asset', function() {
this.params = { operator: 'to be asset' };
this.obj.should.have.property('id').which.is.a.Number;
this.obj.should.have.property('path');
})
})
“assertion” Members
Assertion.add(name, func, [isGetter])
Way to extend Assertion function. It uses some logic
to define only positive assertions and itself rule with negative assertion.
All actions happen in subcontext and this method take care about negation.
Potentially we can add some more modifiers that does not depends from state of assertion.
name
(String): Name of assertion. It will be used for defining method or getter on Assertion.prototype
func
(Function): Function that will be called on executing assertion
[isGetter]
(Boolean): If this assertion is getter. By default it is false.
Assertion.add('asset', function() {
this.params = { operator: 'to be asset' };
this.obj.should.have.property('id').which.is.a.Number;
this.obj.should.have.property('path');
});
Assertion.alias(from, to)
Create alias for some Assertion
property
from
(String): Name of to map
to
(String): Name of alias
Assertion.alias('true', 'True');
Assertion#assert(expr)
Base method for assertions. Before calling this method need to fill Assertion#params object. This method usually called from other assertion methods.Assertion#params
can contain such properties:
operator
- required string containing description of this assertionobj
- optional replacement for this.obj, it usefull if you prepare more clear object then givenmessage
- if this property filled with string any others will be ignored and this one used as assertion messageexpected
- any object used when you need to assert relation between given object and expected. Like given == expected (== is a relation)details
- additional string with details to generated messageexpr
(*): Any expression that will be used as a condition for asserting.
var a = new should.Assertion(42);
a.params = {
operator: 'to be magic number',
}
a.assert(false);
//throws AssertionError: expected 42 to be magic number
Assertion#fail()
Shortcut for Assertion#assert(false)
.
var a = new should.Assertion(42);
a.params = {
operator: 'to be magic number',
}
a.fail();
//throws AssertionError: expected 42 to be magic number
“assertion assert” Members
should.fail(actual, expected, message, operator)
Node.js standard assert.fail
.
actual
(*): Actual object
expected
(*): Expected object
message
(string): Message for assertion
operator
(string): Operator text
should.equal(actual, expected, [message])
Node.js standard assert.equal
.
actual
(*)
expected
(*)
[message]
(string)
should.notEqual(actual, expected, [message])
Node.js standard assert.notEqual
.
actual
(*)
expected
(*)
[message]
(string)
should.deepEqual(actual, expected, [message])
Node.js standard assert.deepEqual
.
actual
(*)
expected
(*)
[message]
(string)
should.notDeepEqual(actual, expected, [message])
Node.js standard assert.notDeepEqual
.
actual
(*)
expected
(*)
[message]
(string)
should.strictEqual(actual, expected, [message])
Node.js standard assert.strictEqual
.
actual
(*)
expected
(*)
[message]
(string)
should.notStrictEqual(actual, expected, [message])
Node.js standard assert.notStrictEqual
.
actual
(*)
expected
(*)
[message]
(string)
should.throws(block, [error], [message])
Node.js standard assert.throws
.
block
(Function)
[error]
(Function)
[message]
(String)
should.doesNotThrow(block, [message])
Node.js standard assert.doesNotThrow
.
block
(Function)
[message]
(String)
“assertion bool” Members
Assertion#true
Assert given object is exactly true
.
Assertion#True
(true).should.be.true;
false.should.not.be.True;
({ a: 10}).should.not.be.true;
“assertion chaining” Members
“assertion contain” Members
Assertion#containEql(other)
Assert that given object contain something that equal to other
. It uses should-equal
for equality checks.
If given object is array it search that one of elements was equal to other
.
If given object is string it checks if other
is a substring - expected that other
is a string.
If given object is Object it checks that other
is a subobject - expected that other
is a object.
other
(*): Nested object
[1, 2, 3].should.containEql(1);
[{ a: 1 }, 'a', 10].should.containEql({ a: 1 });
'abc'.should.containEql('b');
'ab1c'.should.containEql(1);
({ a: 10, c: { d: 10 }}).should.containEql({ a: 10 });
({ a: 10, c: { d: 10 }}).should.containEql({ c: { d: 10 }});
({ a: 10, c: { d: 10 }}).should.containEql({ b: 10 });
// throws AssertionError: expected { a: 10, c: { d: 10 } } to contain { b: 10 }
// expected { a: 10, c: { d: 10 } } to have property b
Assertion#containDeepOrdered(other)
Assert that given object is contain equally structured object on the same depth level.
If given object is an array and other
is an array it checks that the eql elements is going in the same sequence in given array (recursive)
For string it is working as Assertion#containEql
If given object is an object it checks that the same keys contain deep equal values (recursive)
On other cases it try to check with
.eql`
other
(*): Nested object
[ 1, 2, 3].should.containDeepOrdered([1, 2]);
[ 1, 2, [ 1, 2, 3 ]].should.containDeepOrdered([ 1, [ 2, 3 ]]);
'123'.should.containDeepOrdered('1')
({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({a: 10});
({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {c: 10}});
({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {d: [1, 3]}});
“assertion equality” Members
Assertion#eql(val, [description])
Deep object equality comparison. For full spec see should-equal tests
.
val
(*): Expected value
[description]
(string): Optional message
(10).should.be.eql(10);
('10').should.not.be.eql(10);
(-0).should.not.be.eql(+0);
NaN.should.be.eql(NaN);
({ a: 10}).should.be.eql({ a: 10 });
[ 'a' ].should.not.be.eql({ '0': 'a' });
“assertion errors” Members
Assertion#throw([message], [properties])
Assert given function throws error with such message.
Assertion#throwError
[message]
(string|RegExp|Function|Object): Message to match or properties
[properties]
(Object): Optional properties that will be matched to thrown error
(function(){ throw new Error('fail') }).should.throw();
(function(){ throw new Error('fail') }).should.throw('fail');
(function(){ throw new Error('fail') }).should.throw(/fail/);
(function(){ throw new Error('fail') }).should.throw(Error);
var error = new Error();
error.a = 10;
(function(){ throw error; }).should.throw(Error, { a: 10 });
(function(){ throw error; }).should.throw({ a: 10 });
“assertion matching” Members
Assertion#match(other, [description])
Asserts if given object match other
object, using some assumptions:
First object matched if they are equal,
If other
is a regexp and given object is a string check on matching with regexp
If other
is a regexp and given object is an array check if all elements matched regexp
If other
is a regexp and given object is an object check values on matching regexp
If other
is a function check if this function throws AssertionError on given object or return false - it will be assumed as not matched
If other
is an object check if the same keys matched with above rules
All other cases failed
other
(*): Object to match
[description]
(string): Optional message
'foobar'.should.match(/^foo/);
'foobar'.should.not.match(/^bar/);
({ a: 'foo', c: 'barfoo' }).should.match(/foo$/);
['a', 'b', 'c'].should.match(/[a-z]/);
(5).should.not.match(function(n) {
return n < 0;
});
(5).should.not.match(function(it) {
it.should.be.an.Array;
});
({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should
.match({ a: 10, b: /c$/, c: function(it) {
return it.should.have.property('d', 10);
}});
[10, 'abc', { d: 10 }, 0].should
.match({ '0': 10, '1': /c$/, '2': function(it) {
return it.should.have.property('d', 10);
}});
Assertion#matchEach(other, [description])
Asserts if given object values or array elements all match other
object, using some assumptions:
First object matched if they are equal,
If other
is a regexp - matching with regexp
If other
is a function check if this function throws AssertionError on given object or return false - it will be assumed as not matched
All other cases check if this other
equal to each element
other
(*): Object to match
[description]
(string): Optional message
[ 'a', 'b', 'c'].should.matchEach(/\w+/);
[ 'a', 'a', 'a'].should.matchEach('a');
[ 'a', 'a', 'a'].should.matchEach(function(value) { value.should.be.eql('a') });
{ a: 'a', b: 'a', c: 'a' }.should.matchEach(function(value) { value.should.be.eql('a') });
“assertion numbers” Members
Assertion#Infinity
Assert given object is not finite (positive or negative)
(10).should.not.be.Infinity;
NaN.should.not.be.Infinity;
Assertion#within(start, finish, [description])
Assert given number between start
and finish
or equal one of them.
start
(number): Start number
finish
(number): Finish number
[description]
(string): Optional message
(10).should.be.within(0, 20);
Assertion#approximately(value, delta, [description])
Assert given number near some other value
within delta
value
(number): Center number
delta
(number): Radius
[description]
(string): Optional message
(9.99).should.be.approximately(10, 0.1);
“assertion property” Members
Assertion#enumerable(name, [val])
Asserts given object has enumerable property with optionally value
name
(string): Name of property
[val]
(*): Optional property value to check
({ a: 10 }).should.have.enumerable('a');
Assertion#property(name, [val])
Asserts given object has property with optionally value. On success it change given object to be value of property.
name
(string): Name of property
[val]
(*): Optional property value to check
({ a: 10 }).should.have.property('a');
Assertion#properties(names)
Asserts given object has properties. On this method affect .any modifier, which allow to check not all properties.
names
(Array|...string|Object): Names of property
({ a: 10 }).should.have.properties('a');
Assertion#length(n, [description])
Asserts given object has property length
with given value n
Assertion#lengthOf
n
(number): Expected length
[description]
(string): Optional message
[1, 2].should.have.length(2);
Assertion#ownProperty(name, [description])
Asserts given object has own property. On success it change given object to be value of property.
Assertion#hasOwnProperty
name
(string): Name of property
[description]
(string): Optional message
({ a: 10 }).should.have.ownProperty('a');
Assertion#empty
Asserts given object is empty. For strings, arrays and arguments it checks .length property, for objects it checks keys.
''.should.be.empty;
[].should.be.empty;
({}).should.be.empty;
Assertion#keys([keys])
Asserts given object has exact keys.
Assertion#key
[keys]
(Array|...string|Object): Keys to check
({ a: 10}).should.have.keys('a');
({}).should.have.keys();
“assertion strings” Members
“assertion types” Members
Assertion#type(type, [description])
Assert given object has some type using typeof
type
(string): Type name
[description]
(string): Optional message