代码之家  ›  专栏  ›  技术社区  ›  Radex

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

  •  22
  • Radex  · 技术社区  · 9 年前

    您能提供一个使用Jest库的示例吗?

    const location = () => {
      return {
        locationId: 5128581, // nyc usa
        geo: [-74.006, 40.7143],
        isFetching: false
      }
    }
    
    export default location
    
    1 回复  |  直到 5 年前
        1
  •  50
  •   GibboK    9 年前

    尝试使用 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)
            }))
          })
        })