Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one do a deepEqual assertion with should.js?

I've tried to make a deepEqual assertion with should.js (the latest version) and have not had any success. I can get things to work with equal but not with deepEqual. In fact I am seeing that there is no deepEqual method.

Here's what I've tried:

> require('should')
{...}
> > var x = Number(8)
undefined
> x.should.equal(8)
{ obj: 8 }
> x.should.equal(9)
AssertionError: expected 8 to equal 9
at ....
> x.should.deepEqual(8)
TypeError: Object #<Object> has no method 'deepEqual'

Fair enough. Now looking into should, I see it is a getter:

> Object.getOwnPropertyDescriptor(Object.prototype, 'should')
{ get: [Function],
  set: [Function],
  enumerable: false,
  configurable: true }

Since it is a getter, how to I examine its keys? This almost works:

> Object.keys(Object.prototype.should)
[ 'obj' ]

But then I see

> Object.getOwnPropertyDescriptor(should.obj)
{ value: undefined,
  writable: false,
  enumerable: false,
  configurable: false }

So I'm rather stuck at this point. I would just like to see what things can follow should.

I did read the docs and it says that should.js literally extends node's assert module, but node's assert does allow deepEqual.

> assert = require('assert')
> assert.deepEqual
[Function: deepEqual]

The should docs don't even mention deepEqual at all, which really has me confused. To make things even more confusing, I do see a deepEqual when I enter should on the node REPL. But it is buried in an ok element, as far as I can tell.

TL;DR: How do I call assertEqual or its equivalent from should?

like image 922
Ray Toal Avatar asked Oct 07 '13 21:10

Ray Toal


People also ask

What is assert deepEqual?

The assert. deepEqual() method tests if two objects, and their child objects, are equal, using the == operator. If the two objects are not equal, an assertion failure is being caused, and the program is terminated. To compare the objects using the === operator, use the assert.

What are Javascript assertions?

Assertions include boundaries, which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that a match is possible (including look-ahead, look-behind, and conditional expressions).

What is an assertion error Javascript?

js comes with an assert class as part of its core modules set, allowing simple assertion tests to be performed. When such an assertion fails an AssertionError is thrown to indicate what went wrong.


1 Answers

I think you should (pun intended) use the eql method.

https://github.com/visionmedia/should.js/#eql

({ foo: 'bar' }).should.eql({ foo: 'bar' })
like image 129
pllee Avatar answered Oct 29 '22 02:10

pllee