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

168
Views
jest unit test javascript

I have a unit test I am trying check the field but cannot find anything in the jest documentation to ignore the one value in this case, randomNumber.

const expected = [
  {
    model: 'string',
    type: 'string',
    randomNumber: 43,
    name: 'string',
  },
]

The random number changes every test so I can't hard code the test to check that number. I would like to test to make sure the expected array has all of the fields, but ignore the values.

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

0

const requiredKeys = ["model", "type", "randomNumber", "name"];

const actual = [
    {
      model: 'string',
      type: 'string',
      randomNumber: 43,
      name: 'string',
    },
    {    
      type: 'string',
      randomNumber: 43,
      name: 'string',
  },
]

actual.forEach(x => {
  const keys = Object.keys(x);
  const hasAllKeys = requiredKeys.every(key => keys.includes(key));

  expect(hasAllkeys).toBeTruthy();
});

about 4 years ago · Juan Pablo Isaza Report

0

You can do something along the lines of this

const data =  {
    model: 'string',
    type: 'string',
    randomNumber: 43,
    name: 'string',
 }

const dataExpected = {
    model: 'string',
    type: 'string',
    name: 'string',
}

const {randomNumber, ...randomNumOmittedData} = data;

expect(randomNumOmittedData).toEqual(dataExpected);


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!