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

127
Views
Match complete object using objectContaining instance in jest

I am trying to do a complete match using objectContaining instance in jest.

Below is the example object I am trying to match:

 const queryCommandInput = {
          KeyConditionExpression: '---keyConditionExpression---',
          ProjectionExpression: '---projectionExpression----',
          FilterExpression: '---filterExpression----',
          ExpressionAttributeValues: { //[1] - Unable to match from here
            'value1': { S: 'example1' },
            'value2': { S: 'example2' },
            'value3': { N: 12345 },
          },
          TableName: 'tableName'
        }

const queryCommand = new QueryCommand(queryCommandInput);
//[2]
 expect(DynamoDBClient.prototype.send).toBeCalledWith(expect.objectContaining({input: expect.objectContaining(queryCommandInput) }))

[1] - Unable to do a multilevel match without using multi objectContaining ([2])

I could have another object containing, and then another objectContaining after that. But not able to find any other better way to automatically iterate and match all the key/values

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Found the solution. I am sure this will be helpful for a lot of developers out there.

Destructuring and overriding the behaviour for the properties which we know a match would fail, works!!

In this scenario, queryCommand is an instance of a class QueryCommand. The below statement would fail because even though the inputs are the same, the instances are different.

expect(DynamoDBClient.prototype.send).toBeCalledWith(queryCommand) //the queryCommand is a different instance from the instance being sent to the send method of DynamoDBClient conrtuctor, hence it fails.

That is why I was using the objectContaining class, but that only matches one level. If the object is nested, multiple nested objectContaining must be constructed. The best way is to not use an objectContaining constructor, but to match the instances itself and restructure it so that we only check the types of the properties which we know will change, and complete/deep check the properties that we want.

expect(DynamoDBClient.prototype.send).toBeCalledWith({ ...queryCommand, middlewareStack: expect.any(Object) })

about 4 years ago · Santiago Gelvez 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!