小编Jea*_*ERY的帖子

用 jest 测试对象结构

我需要一些建议,目前我想测试对象集合中的每个对象是否都具有正确的结构,所以我这样做:

const allExercises = listAllExercises()

describe("listAllExercises | should return all exercises", () => {
  test("Each exercise should have the right structure", () => {
    const keys = [
      "name",
      "execution",
      "level",
      "is_poly_articular",
      "practice",
      "gender",
      "body_focus",
    ]

    allExercises.forEach((exercise) => {
      expect(Object.keys(exercise).sort()).toEqual(keys.sort())
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

它工作得很好,但我想知道这是否是正确的方法,你有更好的解决方案来用笑话测试对象的键吗?

javascript typescript jestjs ts-jest

2
推荐指数
1
解决办法
1669
查看次数

标签 统计

javascript ×1

jestjs ×1

ts-jest ×1

typescript ×1