简体   繁体   中英

mocha - chai : how to list all errors during a test

I have a unit test to contol a list of elements.

ex :

arr = [
{ element : "aaa",
  validation : false
},
{ element: "bbbb",
  validation: true
},
{ element: "ccc",
  validation: false
}

During my unit test , i want to list all invalid elements but with mocha and chai, he stop on the first invalid element. How to force mocha to make a test with error ?

My code "it" :

it('Read element', () => {
    let length = arr.length - 1;
      for (let i =0; i<= length; i++ ) {
    assert.equal(arr[i].validation, true, 'route ' + arr[i].element+ ' should be valid);
      }
});

You could create a separate test per array item:

describe('Read element', () => {
  arr.forEach(item => {
    it('route ' + item.element + ' should be valid', () => {
      assert.equal(item.validation, true);
    });
  });
});

You could use the deepEqual matcher instead of looping over your array and construct an array to match against.

let validationArray = arr = [
{ element : "aaa",
  validation : true
},
{ element: "bbbb",
  validation: true
},
{ element: "ccc",
  validation: true
}];

assert.deepEqual(arr, validationArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM