如何使用Jest检查对象的对象属性是否匹配?

Rad*_*dex 3 javascript jestjs

我有以下代码,当它被调用时返回一个对象.我想编写一个测试用例,检查对象是否具有相应命名的树属性,它们的值是number,array和bool.

能否请您使用Jest库提供示例?

const location = () => {
  return {
    locationId: 5128581, // nyc usa
    geo: [-74.006, 40.7143],
    isFetching: false
  }
}

export default location
Run Code Online (Sandbox Code Playgroud)

Gib*_*boK 11

尝试使用expect.objectContaining()expect.any()检查每种属性类型.

    import location from './whatever'
    describe('location', () => {
      it('should return location object', () => {
        expect(location()).toEqual(expect.objectContaining({
          locationId: expect.any(Number),
          geo: expect.any(Array),
          isFetching: expect.any(Boolean)
        }))
      })
    })
Run Code Online (Sandbox Code Playgroud)