javascript - Why is my mocha/should Array throwing test failing? -


the following code snippet simple (from https://mochajs.org/#synchronous-code). feels stupidbut, why [1,2,3] evaluates undefined when used literal notation, , not when used in myarray variable?

var assert = require('assert')  // "mocha": "^3.0.2" var should = require('should')  // "should": "^11.1.0"  describe('array', function () {   describe('#indexof()', function () {     var myarray = [1, 2, 3]     it('should return -1 when value not present', function () {       myarray.indexof(0).should.equal(-1)     // - success       [1, 2, 3].indexof(0).should.equal(-1)   // b - fails test     })   }) }) 

when run test, line 'b' fails follows:

array   #indexof()     1) should return -1 when value not present   1) array #indexof() should return -1 when value not present:     typeerror: cannot read property 'indexof' of undefined      ... error trace points line fails, nothing else ... 

i appreciate light on disturbing, surely easy-to-answer, question. cheers.

fiddle allows test out:

your missing semi-colon , it's disrupting tests. i'm not expert @ edge cases can read them online: why should use semicolon after every function in javascript?

myarray.indexof(0).should.equal(-1) ;    [1, 2, 3].indexof(0).should.equal(-1);   

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -