2

I have some json data and I would like to validate the data against a defined json schema. I'm using the ajv library for the validation. On the json schema, there are constraint you can specify like required properties.

Should I still create unit tests with invalid schema and expect it to be invalid or should I just rely on the fact that the library is already well tested and should tell me all the validation errors?

Currently I'm writing unit tests for all constraint to make sure that it will tell me it is invalid, but it is lots of unit tests to maintain.

Thanks,

2
  • What makes you think the library is well tested? It can be insightful on the answer to it. Commented Apr 23, 2019 at 18:56
  • To be honest, I'm assuming it is, since it is made by quality engineer and they have CI in place. It is open source so I guess I could go and check it out. But from what I'm seeing so far, it looks to be well tested ...
    – pdiddy
    Commented Apr 23, 2019 at 19:02

1 Answer 1

3

Unit tests, by the book, are meant to exercise the code you write, isolating it from dependencies (SO calls, external libraries and even language built-in libraries if important for your code) through mocking.

What you may need is integration tests, which means checks that will evaluate if two components can communicate. They are different than integrated tests, which bundles two or more components and evaluate the output of them as a unit.

In integration tests, you rely on the unit tests of the components and exercise the communication:

Does service A send the data firstName and service B reads the data firstName, rather than FirstName or name?

In your case, specially because of JavaScript, you can do type checking as a form of integration testing, because this type of error can escape from unit testing on both components.

If you don't have such conviction on the unit test quality of the library or if you believe that the authors of the library will not respond to bugs in a timely manner for your project, you should surely do a small amount of integrated testing, by building your app without mocking the library and inputting some special data you want to be more sure.

3
  • So If I'm convinced the author of the library does a good job with their tests then I won't need to write tests that checks given an invalid json it should give some validation error, because I feel like I'm writing unit tests for the library ....
    – pdiddy
    Commented Apr 23, 2019 at 19:23
  • 1
    In a certain way, yes, but the integrated tests will be aimed to check if your project-specific needs are satisfied. Let's way a library says it handles millions of items, but your project just handles hundreds. In this case, you will write integrated tests for the few hundreds cases. Commented Apr 23, 2019 at 19:26
  • Note that integration tests can be really helpful when something changes, ie, the maintainer stops maintaining the library, or someone else joins the project, or . . . if you depend on a library, you need a way to validate it does, and is still doing, what you want. So it's not necessarily about testing the library directly, it's about making sure that your application gets the responses it needs from the library. Look at all of the issues nom has had in this area. Commented Apr 25, 2019 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.