How to find a nested property/value pair in mocha?

by lindsey.homenick , in category: Javascript , 2 months ago

How to find a nested property/value pair in mocha?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 2 months ago

@lindsey.homenick 

To find a nested property/value pair in Mocha, you can use Chai, an assertion library that integrates well with Mocha.


Here is an example of how you can use Chai to assert that a nested property/value pair exists in an object:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const { expect } = require('chai');

describe('Nested Property Test', function() {
  it('should find a nested property/value pair', function() {
    const obj = {
      foo: {
        bar: 'baz'
      }
    };

    expect(obj).to.have.nested.property('foo.bar', 'baz');
  });
});


In this example, we have an object obj with a nested property foo.bar that has the value 'baz'. We use Chai's expect function to assert that obj has a nested property foo.bar with the value 'baz'.


You can run this test using Mocha by saving the file and running mocha in your terminal.