How to Match Some Values of Object Using Sinon Stub

I've been writing test using sinon. During the same I wrote stub where some input parameters are passed and an object is returned. This object returns some values and a random date value generated by system at the time of execution. So need guidance on following.

  1. How can I handle the same as the matching arguments are static in nature and I don't know the possible value of the date generated by actual code.
  2. How can we skip certain key values of an object using sinon. i.e. say object has following values. const object = {name: "abc", employeeNumber : "123"} I only want to check if name is "abc" and don't need to match employeeNumber.

1 Answer

From the sinon.match docs:

Requires the value to be not null or undefined and have at least the same properties as expectation.


From the sinon.assert.match docs:

Uses sinon.match to test if the arguments can be considered a match.


Example:

test('partial object match', () => {
  const object = { name: "abc", employeeNumber : "123" };
  sinon.assert.match(object, { name: 'abc' });  // SUCCESS
})
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest