Match partial objects in Chai assertions?

前端 未结 8 2074
谎友^
谎友^ 2021-01-07 18:07

I am looking for the best way to match the following:

expect([
    {
        C1: \'xxx\',
        C0: \'this causes it not to match.\'
    }
]).to.deep.incl         


        
8条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 18:45

    chai-subset or chai-fuzzy might also perform what you're looking for.

    Chai-subset should work like this:

    expect([
      {
        C1: 'xxx',
        C0: 'this causes it not to match.'
      }
    ]).to.containSubset([{C1: 'xxx'}]);
    

    Personally if I don't want to include another plugin I will use the property or keys matchers that chai includes:

    ([
      {
        C1: 'xxx',
        C0: 'this causes it not to match.'
      }
    ]).forEach(obj => {
      expect(obj).to.have.key('C1'); // or...
      expect(obj).to.have.property('C1', 'xxx');
    });
    

提交回复
热议问题