Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Test object structure with jest

I would need some advice, currently I would like to test that every object in an object collection has the correct structure so I do this:

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())
    })
  })
})

It works great, but I wanted to know if this was the right way to go, do you have a better solution for testing an object's keys with jest?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can check it with arrayContaining(array)

const expectedKeys = [
    "name",
    "execution",
    "level",
    "is_poly_articular",
    "practice",
    "gender",
    "body_focus",
];   

expect(array).toEqual(expect.arrayContaining(expectedKeys));

It checks the expected array as a subset of the received value, so it works with a different order, but the same keys present.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!