Match partial objects in Chai assertions?

前端 未结 8 2068
谎友^
谎友^ 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');
    });
    
    0 讨论(0)
  • 2021-01-07 18:46

    Clean, functional and without dependencies

    simply use a map to filter the key you want to check

    something like:

    const array = [
        {
            C1: 'xxx',
            C0: 'this causes it not to match.'
        }
    ];
    
    expect(array.map(e=>e.C1)).to.include("xxx");
    

    https://www.chaijs.com/api/bdd/

    0 讨论(0)
提交回复
热议问题