mardi 11 juin 2019

Custom specific error messages for failed tests with Chai+Mocha?

I'm writing a unit test for a function and I am having it check against a few dozen pieces of test data. If the function does not work with one of the test cases, I would like it to display the particular piece of data where the function failed.

In my case, I have about a hundred functions in the backend I am writing tests for, and for each I am writing a few Chai.js assert() cases that check against many pieces of test data. I don't want to have it print out the individual tests of the same function across dozens of pieces of data as I would like to keep the log readable when running the tests across the entire backend.

Here is my current way of writing the tests:

context('replaces relative urls with absolute urls', function () {
  it('test data should contain relative urls', async function () {
    assert(testHTML.every(containsRelativeURLs));
  });
  // (run testHTML through my function here)
  it('test data should not contain relative url after running through convRel', async function () {
    assert(!testHTML.every(containsRelativeURLs));
  })
});

Please let me know if I am misunderstanding the proper way to write unit tests. If there is a cleaner or more standard way to do this, I would love to know.

Aucun commentaire:

Enregistrer un commentaire