Giter VIP home page Giter VIP logo

should.js's Introduction

should.js's People

Contributors

alfred-nsh avatar alsotang avatar aseemk avatar btd avatar eldargab avatar erkan-yilmaz avatar forbeslindesay avatar fredr avatar gabrielfalcao avatar guileen avatar ichernev avatar lackac avatar mcandre avatar msemenistyi avatar msievers83 avatar nbrownus avatar ndastur avatar rafmagana avatar rixth avatar seejohnrun avatar serby avatar theycallmeswift avatar titarenko avatar tj avatar tjwudi avatar tonylukasavage avatar tootallnate avatar travisjeffery avatar tvjg avatar vlad-shatskyi 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  avatar

should.js's Issues

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.

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?

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.

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 ?

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.

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.

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.

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).

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]

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.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 :)

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".

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);

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

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 }

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?

`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 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?

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

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

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;
   },

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.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?

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)

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

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

`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.

"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

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;

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)

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?

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

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.

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

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.