mjackson / expect

Write better assertions
MIT License
2.29k stars 117 forks source link

expect 'array' toBeAn 'object' pass the test #214

Closed paulvollmer closed 7 years ago

paulvollmer commented 7 years ago
const expect = require('expect')

// this pass as expected
describe('array toBeAn array', () => {
  it('ok', () => {
    const data = []
    expect(data).toBeAn('array')
  })
})

// this pass as expected
describe('object toBeAn object', () => {
  it('ok', () => {
    const data = {}
    expect(data).toBeAn('object')
  })
})

// this should fail but doesn't <----------------------------------------------
describe('array toBeAn object', () => {
  it('ok', () => {
    const data = []
    expect(data).toBeAn('object')
  })
})

// this fail as expected
describe('object toBeAn array', () => {
  it('ok', () => {
    const data = {}
    expect(data).toBeAn('array')
  })
})

looks like a bug that an array to be an object pass the test.

ljharb commented 7 years ago

Arrays are objects; this is correct.

paulvollmer commented 7 years ago

yes, this is the ecmascript declaration but if you write Array.isArray({foo: 'bar'}) it return false. if you call this an array is not an object...

ljharb commented 7 years ago

Because Array.isArray is asking "is it an array?" An array is an object - an object is not necessarily an array.

paulvollmer commented 7 years ago

ok. how can i check if it is an object. i expect that the given data should be only an object, nothing else

ljharb commented 7 years ago

x && typeof x === 'object' && !Array.isArray(x).