Giter Club home page Giter Club logo

should.js's Introduction

Repo was moved to own organization. See https://github.com/shouldjs/should.js.

should.js

should is an expressive, readable, test framework agnostic, assertion library. Main goals of this library to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.

It extends the Object.prototype with a single non-enumerable getter that allows you to express how that object should behave, also it returns itself when required with require.

Example

var should = require('should');

var user = {
    name: 'tj'
  , pets: ['tobi', 'loki', 'jane', 'bandit']
};

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

// if the object was created with Object.create(null)
// then it doesn't inherit `Object` and have the `should` getter
// so you can do:

should(user).have.property('name', 'tj');
should(true).ok;

someAsyncTask(foo, function(err, result){
  should.not.exist(err);
  should.exist(result);
  result.bar.should.equal(foo);
});

To begin

  1. Install it:

    $ npm install should --save-dev
  2. Require it and use:

    var should = require('should');
    
    (5).should.be.exactly(5).and.be.a.Number;

In browser

Well, even when browsers by complains of authors has 100% es5 support, it does not mean it has not bugs. Please see wiki for known bugs.

If you want to use should in browser, use the should.js file in the root of this repository, or build it yourself. It is built with browserify (see Makefile). To build a fresh version:

# you should have browserify
$ npm install -g browserify
$ make browser

The script is exported to window.Should. It is the same as using should statically:

Should(5).be.exactly(5)

Also, in the case of node.js, Object.prototype is extended with should (hence the capital S in window.Should):

window.should.be.exactly(window);
// the same
// window is host object
should.be.exactly(window);
// you should not really care about it

(5).should.be.exactly(5);

should.js uses EcmaScript 5 very extensively so any browser that support ES5 is supported. (IE <=8 not supported). See kangax's compat table to know which exactly.

You can easy install it with npm or bower:

npm install should --save-dev
# or
bower install visionmedia/should.js

Static should and assert module

For some rare cases should can be used statically, without Object.prototype. It can be a replacement for the node assert module:

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)

.not

.not negate current assertion.

.any

.any allow for assertions with multiple parameters to assert on any of parameters (not all)

Assertions

chaining assertions

Every assertion will return a should.js-wrapped Object, so assertions can be chained. To help chained assertions read more clearly, you can use the following helpers anywhere in your chain: .an, .of, .a, .and, .be, .have, .with, .is, .which. Use them for better readability; they do nothing at all. For example:

user.should.be.an.instanceOf(Object).and.have.property('name', 'tj');
user.pets.should.be.instanceof(Array).and.have.lengthOf(4);

Almost all assertions return the same object - so you can easy chain them. But some (eg: .length and .property) move the assertion object to a property value, so be careful.

.ok

Assert if chained object is truthy in javascript (ie: not '', null, undefined, 0 , NaN).

Assert truthfulness:

true.should.be.ok;
'yay'.should.be.ok;
(1).should.be.ok;
({}).should.be.ok;

or negated:

false.should.not.be.ok;
''.should.not.be.ok;
(0).should.not.be.ok;

Warning: No assertions can be done on null and undefined. e.g.

  undefined.should.not.be.ok;

will give you Uncaught TypeError: Cannot read property 'should' of undefined).

In order to test for null use

(err === null).should.be.true;

.true

Assert if chained object === true:

true.should.be.true;
'1'.should.not.be.true;

.false

Assert if chained object === false:

false.should.be.false;
(0).should.not.be.false;

.eql(otherValue)

Assert if chained object is equal to otherValue. The object is compared by its actual content, not just reference equality.

({ foo: 'bar' }).should.eql({ foo: 'bar' });
[1,2,3].should.eql([1,2,3]);
// see next example it is correct, even if it is different types, but actual content the same
[1, 2, 3].should.eql({ '0': 1, '1': 2, '2': 3 });

.equal(otherValue) and .exactly(otherValue)

Assert if chained object is strictly equal to otherValue (using === - no type conversion for primitive types and reference equivalence for reference types).

(4).should.equal(4);
'test'.should.equal('test');
[1,2,3].should.not.equal([1,2,3]);
(4).should.be.exactly(4);

.startWith(str)

Assert that a string starts with str.

'foobar'.should.startWith('foo');
'foobar'.should.not.startWith('bar');

.endWith(str)

Assert that a string ends with str.

'foobar'.should.endWith('bar');
'foobar'.should.not.endWith('foo');

.within(from, to)

Assert inclusive numeric range (<= to and >= from):

user.age.should.be.within(5, 50);
(5).should.be.within(5, 10).and.within(5, 5);

.approximately(num, delta)

Assert floating point number near num within delta margin:

(99.99).should.be.approximately(100, 0.1);

.above(num) and .greaterThan(num)

Assert numeric value above the given value (> num):

user.age.should.be.above(5);
user.age.should.not.be.above(100);
(5).should.be.above(0);
(5).should.not.be.above(5);

.below(num) and .lessThan(num)

Assert numeric value below the given value (< num):

user.age.should.be.below(100);
user.age.should.not.be.below(5);
(5).should.be.below(6);
(5).should.not.be.below(5);

.NaN

Assert numeric value is NaN:

(undefined + 0).should.be.NaN;

.Infinity

Assert numeric value is Infinity:

(1/0).should.be.Infinity;

.type(str)

Assert given object is of a particular type (using typeof operator):

user.should.be.type('object');
'test'.should.be.type('string');

.instanceof(constructor) and .instanceOf(constructor)

Assert given object is an instance of constructor (using instanceof operator):

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

.arguments

Assert given object is an Arguments:

var args = (function(){ return arguments; })(1,2,3);
args.should.be.arguments;
[].should.not.be.arguments;

.Object, .Number, .Array, .Boolean, .Function, .String, .Error

Assert given object is instance of the given constructor (shortcut for .instanceof assertion).

({}).should.be.an.Object;
(1).should.be.a.Number;
[].should.be.an.Array.and.an.Object;
(true).should.be.a.Boolean;
''.should.be.a.String;

.enumerable(name[, value])

Assert a property exists, is enumerable, and has optional value (compare using .eql):

'asd'.should.not.have.enumerable('0');
user.should.have.enumerable('name');
user.should.have.enumerable('age', 15);
user.should.not.have.enumerable('rawr');
user.should.not.have.enumerable('age', 0);
[1, 2].should.have.enumerable('0', 1);

.property(name[, value])

Assert property exists and has optional value (compare using .eql):

user.should.have.property('name');
user.should.have.property('age', 15);
user.should.not.have.property('rawr');
user.should.not.have.property('age', 0);
[1, 2].should.have.property('0', 1);

NB .property changes the chain's object to the given property's value, so be careful when chaining after .property!

.properties(propName1, propName2, ...) or .properties([propName1, propName2, ...]) or .properties(obj)

obj should be an object that maps properties to their actual values.

Assert all given properties exist and have given values (compare using .eql):

user.should.have.properties('name', 'age');
user.should.have.properties(['name', 'age']);
user.should.have.properties({
    name: 'denis',
    age: 24
});

.length(number) and .lengthOf(number)

Assert length property exists and has a value of the given number (shortcut for .property('length', number)):

user.pets.should.have.length(5);
user.pets.should.have.a.lengthOf(5);
({ length: 10}).should.have.length(10);

NB .length and .lengthOf change the chain's object to the given length value, so be careful when chaining!

.ownProperty(str) and .hasOwnProperty(str)

Assert given object has own property (using .hasOwnProperty):

({ foo: 'bar' }).should.have.ownProperty('foo').equal('bar');

NB .ownProperty and .hasOwnProperty change the chain's object to the given property value, so be careful when chaining!

.empty

Assert given value is empty. Strings, arrays, arguments with a length of 0, and objects without their own properties, are considered empty.

[].should.be.empty;
''.should.be.empty;
({}).should.be.empty;
(function() {
  arguments.should.be.empty;
})();

.keys([key1, key2, ...]) and .keys(key1, key2, ...) and .key(key)

Assert own object keys, which must match exactly, and will fail if you omit a key or two:

var obj = { foo: 'bar', baz: 'raz' };
obj.should.have.keys('foo', 'baz');
obj.should.have.keys(['foo', 'baz']);
({}).should.have.keys();
({}).should.have.keys('key'); //fail AssertionError: expected {} to have key 'key'missing keys: 'key'

.containEql(otherValue)

Assert given value to contain something .eql to otherValue. See examples to understand better:

'hello boy'.should.containEql('boy');
[1,2,3].should.containEql(3);
[[1],[2],[3]].should.containEql([3]);
[[1],[2],[3, 4]].should.not.containEql([3]);

({ b: 10 }).should.containEql({ b: 10 });
([1, 2, { a: 10 }]).should.containEql({ a: 10 });
[1, 2, 3].should.not.containEql({ a: 1 });

[{a: 'a'}, {b: 'b', c: 'c'}].should.containEql({a: 'a'});
[{a: 'a'}, {b: 'b', c: 'c'}].should.not.containEql({b: 'b'});

When .containEql check arrays it check elements to be in the same order in otherValue and object just to be presented.

.containDeep(otherValue)

Assert given value to contain something .eql to otherValue within depth. Again see examples:

'hello boy'.should.containDeep('boy');
[1,2,3].should.containDeep([3]);
[1,2,3].should.containDeep([1, 3]);
//but not
[1,2,3].should.containDeep([3, 1]);

({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}).should
  .containDeep({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}});

[1, 2, 3, { a: { b: { d: 12 }}}].should.containDeep([{ a: { b: {d: 12}}}]);

[[1],[2],[3]].should.containDeep([[3]]);
[[1],[2],[3, 4]].should.containDeep([[3]]);
[{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep([{a: 'a'}]);
[{a: 'a'}, {b: 'b', c: 'c'}].should.containDeep([{b: 'b'}]);

It does not search somewhere in depth it check all pattern in depth. Objects are checked by properties key and value; arrays are checked like sub sequences. Everyting is compared using .eql. Main difference with .containEql is that this assertion requires full type chain - if asserted value is an object, otherValue should be also an object (which is sub object of given). The same is true for arrays, otherValue should be an array which compared to be subsequence of given object.

When .containDeep check arrays it check elements to be in the same order (as arrays ordered collections) in otherValue and object just to be presented.

.match(otherValue)

Assert given object matches otherValue.

Given: String, otherValue: regexp. Uses RegExp#exec(str):

username.should.match(/^\w+$/)

Given: Array, otherValue: regexp - assert each value match to regexp.

['a', 'b', 'c'].should.match(/[a-z]/);
['a', 'b', 'c'].should.not.match(/[d-z]/);

Given: Object, otherValue: regexp - assert own property's values to match regexp.

({ a: 'foo', c: 'barfoo' }).should.match(/foo$/);
({ a: 'a' }).should.not.match(/^http/);

Given: Anything, otherValue: function - assert if given value matched to function.

Function can use .should inside or return 'true' or 'false', in all other cases it do nothing. If you return value that return assertion, you will receive better error messages.

(5).should.match(function(n) { return n > 0; });
(5).should.not.match(function(n) { return n < 0; });
(5).should.not.match(function(it) { it.should.be.an.Array; });
(5).should.match(function(it) { return it.should.be.a.Number; });

Now compare messages:

(5).should.not.match(function(it) { it.should.be.a.Number; });
//AssertionError: expected 5 not to match [Function]
(5).should.not.match(function(it) { return it.should.be.a.Number; });
//AssertionError: expected 5 not to match [Function]
//	expected 5 to be a number

Given: object, otherValue: another object - assert that object properties match to properties of another object in meaning that describe above cases. See examples:

({ 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); } });

[10, 'abc', { d: 10 }, 0].should
    .match([10, /c$/, function(it) { return it.should.have.property('d', 10); }]);

.matchEach(otherValue)

Assert given property keys and values each match given check object.

If otherValue is RegExp, then each property value checked to match it:

(['a', 'b', 'c']).should.matchEach(/[a-c]/);

If otherValue is Function, then check each property value and key matched it:

[10, 11, 12].should.matchEach(function(it) { return it >= 10; });
[10, 11, 12].should.matchEach(function(it) { return it >= 10; });

In other cases it checks that each property value is .eql to otherValue:

[10, 10].should.matchEach(10);

.throw() and throwError()

Assert an exception is thrown:

(function(){
  throw new Error('fail');
}).should.throw();

Assert an exception is not thrown:

(function(){

}).should.not.throw();

Assert exception message matches string:

(function(){
  throw new Error('fail');
}).should.throw('fail');

Assert exepection message matches regexp:

(function(){
  throw new Error('failed to foo');
}).should.throw(/^fail/);

If you need to pass arguments and/or context to execute function use Function#bind(context, arg1, ...):

function isPositive(n) {
    if(n <= 0) throw new Error('Given number is not positive')
}

isPositive.bind(null, 10).should.not.throw();
isPositive.bind(null, -10).should.throw();

If you need to check something in an asynchronous function, you must do it in 2 steps:

// first we need to check that function is called
var called = false;
collection.findOne({ _id: 10 }, function(err, res) {
    called = true;

    //second we test what you want
    res.should.be....
});

called.should.be.true;

In case you are using something like Mocha, you should use an asynchronous test, and call done() in the proper place to make sure that your asynchronous function is called before the test finishes.

collection.findOne({ _id: 10 }, function(err, res) {
    if(err) return done(err);
    //second we test what you want
    res.should.be....

    done();
});

In general, if you need to check that something is executed, you are best using spies. A good example is sinon.

.status(code)

Asserts that .statusCode is code:

res.should.have.status(200);

Not included in browser build.

.header(field[, value])

Asserts that a .headers object with field and optional value are present:

res.should.have.header('content-length');
res.should.have.header('Content-Length', '123');

Not included in browser build.

.json

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

res.should.be.json

Not included in browser build.

.html

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

res.should.be.html

Not included in browser build.

Optional Error description

As it can often be difficult to ascertain exactly where failed assertions are coming 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: 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, instanceof, above, below, match, length, property, ownProperty.

Mocha example

For example you can use should with the Mocha test framework by simply including it:

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


describe('mylib', function() {
  it('should have a version with the format #.#.#', function() {
    lib.version.should.match(/^\d+\.\d+\.\d+$/);
  });
});

Contributions

Actual list of contributors if you want to show it your friends.

To run the tests for should simply run:

$ make test

See also CONTRIBUTING.

OMG IT EXTENDS OBJECT???!?!@

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

MIT ยฉ 2010-2014 TJ Holowaychuk

should.js's People

Contributors

alsotang avatar aseemk avatar bodenr avatar btd avatar faridnsh avatar fredr avatar gabrielfalcao avatar jsdevel avatar msemenistyi avatar nbrownus avatar ndastur avatar rafmagana avatar raynos avatar rixth avatar rubenverborgh avatar rudolf avatar skovalyov avatar spencesellers avatar stimberm avatar theycallmeswift avatar tj avatar tonylukasavage avatar tootallnate avatar travisjeffery avatar tricknotes avatar ulikoehler avatar vlad-shatskyi avatar yamadapc avatar zarino avatar zlatanvasovic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

should.js's Issues

Objects created with Object.create(null) don't have should mixin

ie

 var foo = Object.create(null);
 assert.ok(foo.should === undefined);

Could this be fixed by overriding the Object.prototype.create looking for a 'null' argument[0] then internally creating the null-prototype object before adding the should mixin and returning?

Even if that did work, would you want to?

should.have.property('name', undefined)

I want to actually check that I have a property with value undefined.

I think right now the property check is using undefinedness to determine if the second argument was passed in (when instead it should probably check arguments.length).

equals() fails for multidimensional arrays

[[1, 2, 3], [4, 5, 6]].should.equal [[1, 2, 3], [4, 5, 6]]

This fails.


     AssertionError: expected [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] to equal [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
      at Object.equal (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/should/lib/should.js:303:10)
      at Context.<anonymous> (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/test/utils_test.coffee:12:46)
      at Test.run (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runnable.js:168:32)
      at Runner.runTest (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runner.js:295:10)
      at /Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runner.js:339:12
      at next (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runner.js:223:14)
      at /Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runner.js:232:7
      at next (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runner.js:182:23)
      at Array.0 (/Users/simplyianm/Desktop/p/savagegames/savagegames-site/node_modules/mocha/lib/runner.js:200:5)
      at EventEmitter._tickCallback (node.js:192:40)

should.be.null and should.be.undefined?

Right now I am using should.equal null and should.equal undefined.

Would it be better if you could use these assertions directly?

UPDATE: I just realized that you cannot have methods to null and undefined :)

Better names/aliases for equality

I'm thinking of adding these aliases into my fork to improve readability for equality:

foo.should.be(bar);
// strict equality; equivalent to foo.should.equal(bar)
// overloads be() to be both a dummy property and a function

foo.should.match(bar);
// deep equality; equivalent to foo.should.eql(bar)
// overloads match() for non-regexp args

I'm thinking of doing this because it's not obvious at a glance at all the distinction between equal vs eql, and also because should's equal (strict) is different than the native assert's equal (loose).

I kept be as strict equal since, at least to me, strict equality is way more useful, esp. in test code, than loose equality. The result is that both be() and match() are nice semantic names. =)

What do you think? I feel like this improves the already-rocking should grammar.

Cheers,
Aseem

P.S. Overloading be() will be a lot harder than match() so I might not be able to implement be() just yet. Implementing issue #9 though may help here.

passing lint

It would be nice if it passed lint.

1 20,29: Expected an assignment or function call and instead saw an expression.
    email.should.not.be.empty;

Suggestion: should.eql() static method

Sometimes it would be convenient, when you don't know the argument types, to have a static version of not-strict-equals.

So that you can do this:

{a:1,b:2}.should.eql({b:2,a:1});

or do this

should.eql({a:1,b:2},  {b:2,a:1});

as well as

should.eql(null, 0);
should.eql(undefined, 0);
should.eql(undefined, null);
should.eql("1", 1);
...

etc.

should.throw() neglected

i have following abstract class defined:

var util = require('util')
    ,EventEmitter = require('events').EventEmitter;

var GuideFetcher = function() {
    var _self = this;
    EventEmitter.call(this);

    this.getFestivals = function() { throw new Error('Overwrite getFestivals!'); };
    this.getArtists = function() { throw new Error('Overwrite getArtists!'); };
};
util.inherits(GuideFetcher, EventEmitter);

module.exports = GuideFetcher;

the test cases are implemented with mocha using should.js as followed:

var GuideFetcher = require('../guidefetcher/guidefetcher');

describe('GuideFetcher', function() {
    var guideFetcher = new GuideFetcher();

    describe('#getFestivals()', function() {
        it('should throw an "Overwrite getFestivals()" exception', function() {
            guideFetcher.getFestivals().should.throw();
        })
    })

    describe('#getArtists()', function() {
        it('should throw an "Overwrite getArtists()" exception', function() {
            guideFetcher.getArtists().should.throw();
        })
    })
})

unfortunatly, the error is not catched by should.js, instead it goes directly to my console output.

is this an known issue?

thanks,
manuel

Should causes async Mocha tests to timeout

I'm running Mocha in watch mode, compiling CoffeeScript tests.

# This fails by timing out
it 'should be able to grab qba6sms/published from Typekit', (done) ->
    $.getJSON 'http://typekit.com/api/v1/json/kits/qba6sms/published?callback=?', (data, txt, xhr) ->
      data.should.be.ok
      done()
# This does not fail
it 'should be able to grab qba6sms/published from Typekit', (done) ->
    $.getJSON 'http://typekit.com/api/v1/json/kits/qba6sms/published?callback=?', (data, txt, xhr) ->
      assert.ok data
      done()

I'm not sure what's up โ€“ am I doing something wrong, or is it a bug?

Mixin own extensions

Hi! Is it possible to add extensions?
I would like to have something like underscore's mixin function.

should.mixin({ integer: function(val) { return val % 1 == 0 } });

should.have.length() always gives length = 1

Seems it doesn't work. lenghtOf() too. node 0.4.10, should 0.3.2, mocha 0.0.8:

describe('Array length', function() {
  it('fails', function() {
    [1,2].should.have.length(2);
  });

  it('passes', function() {
    [1,2].length.should.equal(2);
  });
});

instanceof(Date) fails for dates

Problem

Using should v0.6.3, the instanceof() method returns an incorrect result when called on a Date object and passed Date as its argument.

Expected behavior

Calling instanceof(Date) on a Date object should not throw an AssertionError.

Steps to reproduce

$ node
> require('should');
{ [Function: ok]
  AssertionError: 
   { [Function: AssertionError]
     super_: 
      { [Function: Error]
        captureStackTrace: [Function: captureStackTrace],
        stackTraceLimit: 10 } },
  fail: [Function: fail],
  ok: [Circular],
  equal: [Function: equal],
  notEqual: [Function: notEqual],
  deepEqual: [Function: deepEqual],
  notDeepEqual: [Function: notDeepEqual],
  strictEqual: [Function: strictEqual],
  notStrictEqual: [Function: notStrictEqual],
  throws: [Function],
  doesNotThrow: [Function],
  ifError: [Function],
  version: '0.6.3',
  exist: [Function],
  not: { exist: [Function] },
  Assertion: [Function: Assertion] }
> (new Date()).should.be.an.instanceof(Date);
AssertionError: expected 1335798857282 to be an instance of Date
    at Object.<anonymous> (/home/gollom/projects/surveycalc-node/node_modules/should/lib/should.js:355:10)
    at repl:1:27
    at REPLServer.eval (repl.js:80:21)
    at repl.js:190:20
    at REPLServer.eval (repl.js:87:5)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)

Work-around

$ node
> require('should')
{ [Function: ok]
  AssertionError: 
   { [Function: AssertionError]
     super_: 
      { [Function: Error]
        captureStackTrace: [Function: captureStackTrace],
        stackTraceLimit: 10 } },
  fail: [Function: fail],
  ok: [Circular],
  equal: [Function: equal],
  notEqual: [Function: notEqual],
  deepEqual: [Function: deepEqual],
  notDeepEqual: [Function: notDeepEqual],
  strictEqual: [Function: strictEqual],
  notStrictEqual: [Function: notStrictEqual],
  throws: [Function],
  doesNotThrow: [Function],
  ifError: [Function],
  version: '0.6.3',
  exist: [Function],
  not: { exist: [Function] },
  Assertion: [Function: Assertion] }
> (new Date() instanceof Date).should.be.true;
{ obj: true }

"Throw Assertion Error" suggestion

Hey. I'm new to this great library "should" and while I was browsing trough examples I thought I could test one myself. The thing is I saw that if any the condition fails for whatever we would test, it would throw an Assertion Error. I might suggest creating one small "callback" function for the fail condition to insert a custom message, because it can get pretty confusing on large files of code. For example :

function get(i) {
should.exist(i).or('Please specify a property for the getter');
}

Thanks a lot.

Kindest regards,

Liviu

`should.fail` doesn't set message property

This is really a bug/quirk in the native assert module, but we could patch it in should.

try {
    should.fail('hello world');
} catch (err) {
    should.exist(err.message);    // fails
    console.log(err);
}

If you comment out the should.exist, the console log gets:

{ name: 'AssertionError',
  message: undefined,
  actual: 'hello world',
  expected: undefined,
  operator: undefined,
  stack: [Getter/Setter] }

So if you actually want to use the message, right now you have to do:

should.fail(null, null, 'hello world');

What do you think about patching fail to accept just a message?

Should.js for the browser

I would like to use Mocha BDD and run specs in the browser. Would be nice to have a build of should.js that doesn't rely on Node's stuff.

Ignore \r in equals

Would you consider making the default behaviour of equals be to ignore the difference between \r and \r\n in strings except where an explicit option to include-r is added. My feeling is that it's only ever really worthwhile checking for that difference when unit testing a library intended to swap \r\n for \n or visa versa. It breaks the unit tests of loads and loads of libraries when you run them on windows but pretty much never causes a problem with the actual code.

Typo in the README

The 'bar' has been replaced by 'baz' and vice versa.

In sectiion: Assert own object keys, which must match exactly, and will fail if you omit a key or two:

obj.should.have.keys('foo', 'bar'); should be obj.should.have.keys('foo', 'baz')
obj.should.have.keys(['foo', 'bar']); should be obj.should.have.keys(['foo', 'baz']);
obj.should.include.keys('bar'); should be obj.should.include.keys('baz');
obj.should.not.include.keys('baz'); should be obj.should.not.include.keys('bar');

Should causes Mocha async test to stop

Hi,

I think this might be related to issue #71. When I fail a test using should from an async call it causes Mocha to abort the entire test suite.
Example:

describe("Mocha async behaviour", function () {

   it("failing test", function (done) {
      setTimeout(function () {
         false.should.be.ok;
      }, 1);
   });

   it("passing test 1", function (done) {

      done();
   });

});

In this case the second "it" would not run at all.
I am using Mocha 1.2.2 and should 0.6.3

Thanks,
Boris

Wrong version of code being pulled from npm

Using JsHint so am trying to use ThrowError instead of Error. But the version that Npm is calling down of should.js is missing the alias of ThrowError, Anyone else having this problem to?

Edit: Could this be due to the new Alais being added and the version number not being incremented?

require ('should') only works once.

here is a weird one.
should = require('should')
should2 = require('should') // should return the same object as previous call.
should.should.eql(should2)

expected { AssertionError: ....} to equal undefined

would be a clever trick if you did it on purpose.

support test for NaN

As far as I can tell, there is no simple way to test for NaN.

The following works, but then the error is a not so useful "expected false to be true" when the assertion fails

isNaN = function(value) {
  (value != value).should.be.true
};
isNaN(NaN);

Browserification?

This may be a BCAK error, but trying to browserify my tests (in mocha + should), I get

SyntaxError: Error while loading entry file "/samples/gbL.jsMop/node_modules/should/lib/should.js": Unexpected token: punc (()
  at line 0:1045 in expression:


var util=require("util"),http=require("http"),assert=require("assert"),AssertionError=assert.AssertionError,statusCodes=http.STATUS_CODES,eql=require("./eql"),i=util.inspect;exports=module.exports=assert;exports.version="0.6.3";exports.exist=function(obj,msg{if(null==obj){throw new AssertionError({message:msg||"expected "+i(obj)+" to exist",stackStartFunction:should.exist})}};exports.not={};exports.not.exist=function(obj,msg){if(null!=obj){throw new AssertionError({message:msg||"expected "+i(obj)+" to not exist",stackStartFunction:should.not.exist})}};Object.defineProperty(Object.prototype,"should",{set:function(){},get:function(){return new Assertion(Object(this).valueOf())},configurable:true});var Assertion=exports.Assertion=function Assertion(obj){this.obj=obj};Assertion.prototype={exports:exports,assert:function(expr,msg,negatedMsg,expected){var msg=[this.negate?negatedMsg:msg][0],ok=[this.negate?!expr:expr][0];if(!ok){throw new AssertionError({message:msg,actual:this.obj,expected:expected,stackStartFunction:this.assert})}},(get an(){return this}),

which is complaining about a part of should.js here:

/**
   * Dummy getter.
   *
   * @api public
   */

  get an() {
    return this;
  },

Do you think this is an issue with what I am trying to do, with should, or with browserify?

.should.have.a.lengthOf works?

In the Readme

user.pets.should.have.a.lengthOf(5)

does .should.have.a.lengthOf works? or is it only .should.have.lengthOf ?

Strings converting to hashes using equal?

I opened up a project that I hadn't touched for months today and I ran my mocha tests that leverage should. All tests were failing. I then noticed on should.equal, strings were being converted to hashes like so...

From the repl...

> var should = require('should');
undefined
> 'Blah'.should.equal('Blah');
AssertionError: expected { '0': 'B', '1': 'l', '2': 'a', '3': 'h' } to equal 'Blah'
    at Object.Assertion.equal (/Users/kevinohara80/dev/projects/node/elkington/node_modules/should/lib/should.js:297:10)
    at repl:1:15
    at REPLServer.self.eval (repl.js:111:21)
    at rli.on.e (repl.js:260:20)
    at REPLServer.self.eval (repl.js:118:5)
    at Interface.<anonymous> (repl.js:250:12)
    at Interface.EventEmitter.emit (events.js:88:17)
    at Interface._onLine (readline.js:183:10)
    at Interface._line (readline.js:502:8)
    at Interface._ttyWrite (readline.js:720:14)

Any idea what is happening? The only thing that has changed is my version of node (on v0.8.5).

asynchronous throw

What's the best way to test for throw() (great addition btw) asynchronously? using with mocha.

Example...

it('works sync', function() {
  (function() {
    throw {message: 'error'};
  }).should.throw();
});

this works.
it('should throw async', function(done) {
  (function() {
    setTimeout(function() {
      throw {message: 'error'};
    }, 200);
  }).should.throw();
});     

AssertionError: expected an exception to be thrown

should.throw doesn't stop exceptions from printing out

I'm using 'should.throw' in the current fashion:

app
  .get('/')
  .end(function (res) {
    done();
  }).should.throw('fail');

This catches the exception and passes the test, but the exception still prints out to the console. This doesn't seem right to me. Am I missing something here?

should.equal fails when comparing dates

Hi guys,

Using should.equal with Date objects seems to be failing.

var date = new Date();
date.should.equal(date);

AssertionError: expected 1335443601886 to equal Thu, 26 Apr 2012 12:33:21 GMT

This happens because "should" value (the actual value) is obtained using the valueOf() function (should.js line 81) and the "equal" function does not follow the same approach, comparing the valufOf(actual) with the actual object directly (should.js line 303). In case of date objects since valueOf returns a number the equality fails.

I fix it by performing the same process of value comparison changing the equal function to (line 303):

        Object(val).valueOf() === this.obj

Not sure if this has broader implication on the framework thou.

Cheers,

Eric

Stubs and mocks

Hi!

I would love to have the ability to be able to stub or mock any object in Should.js. I could implement this myself, but as I have limited time I would rather not waste it if no-one things stubs or mocks should be a part of Should.js at all. Before anyone says anything, yes I know I can use a third party framework like Sinon, but I'd rather this kind of functionality would be a part of the actual assert-framework than anything else.

Implementing this kind of functionality into Should allows us to automatically revert stubs when the test is done, without imposing additional syntax.

I was thinking having something like object.should.be.stubbed().should.return(42).if.called.with("id", "peterson");
which is basically three operations, stub the object, return value of 42 when called, only return already set return value if called with arguments "id" and "peterson". Then at the end of the test you might have a object.should.be.called.correctly() if it was actually called with arguments "id" and "peterson".

What do people think? Is this something people would want as a part of the Should framework, or as a standalone framework?

includeEql throws "Object has no method 'some'"

Looks like somebody had underscore installed when they wrote this method and forgot to test in isolation. No method some exists so this throws an error:

['blah', 'foo', 'bar'].should.includeEql('bar') // throws!

This is the culprit:

  includeEql: function(obj){
     this.assert(
       this.obj.some(function(item) { return eql(obj, item); })
       , 'expected ' + this.inspect + ' to include an object equal to ' + i(obj)
       , 'expected ' + this.inspect + ' to not include an object equal to ' + i(obj));
     return this;
   },

instanceof is a reserved word

instanceof is a reserved word, and shouldn't be used for a function name.
It should be renamed to "instanceOf" or maybe "instance.of".

Does str1.should.include.string(str2) work anymore?

I'm getting errors in my test files and it looks like it's because should.include.string doesn't exist anymore. It's still in the docs, but it doesn't look like you're testing for it in should's test files.

Is should.include replacing should.include.string? Are they equivalent?

Thanks!
Matt

`status` assertion doesn't work

the error it gives is the following:

ReferenceError: statusCodes is not defined

I checked the code, and it seems that the method expects a statusCodes dictionary to be defined but I couldn't find it anywhere in the project.

should.not.ifError missing

As should passes the assert module's functions, you can call

should.ifError(...);

What can NOT be done at the moment is:

should.not.ifError(...);

It would be great if this was possible as well, to have the same experience for should's own functions as for the "derived" functions.

All assertions should be methods

I love the clever use of getters in should.js, but one request: the API would feel cleaner and test code would be more understandable/maintainable/readable if all assertions were methods.

E.g. foo.should.be.ok; as a standalone statement reads like a bug to me in the same way that window.navigator.userAgent; as a standalone statement does. I instead want to be able to write foo.should.be.ok().

I realize there's a lot of should code that would break if the current way didn't keep working. But I think it's possible to support both this new style and the older style:

  • have the Assertion object inherit from Function, so that it can be called; and
  • have calling it do nothing (so the assertion still happens in the "get" of the ok property) and return itself (arguments.callee or this, not sure if there would be a difference).

What do you think? Am I missing something? I've made a fork and am ready to try this out; will let you know if I succeed or fail. =)

Cheers,
Aseem

[edited language to be a bit clearer]

Allow _(obj).should.not.exist syntax?

Right now You has to include should = require 'should' at the top of every spec file, to be able to use it later like should.exist(obj) or should.not.exist(obj).

It's not very convenient, and also it's style differs from all the other assertions. Maybe it's worth to allow alternate syntax

_(obj).should.not.exist

It may be added with this code:

Object.defineProperty should.Assertion.prototype, 'exist',
  set: () ->
  get: () ->
    unless this.negate
      should.exist this.obj._wrapped    
    else
      should.not.exist this.obj._wrapped
  configurable: true

null

i suppose there's probably not much we can do here , but it's a shame we can't handle null :

null.should.not.be.ok

results in "Cannot read property 'should' of null"

i was wondering if we could support some kind of alternate calling scheme where the should comes first :

should.be.ok(null)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.