We have an application that leverages 3rd party API's (in our instance Hubspot) to run logic on the data. As I'm building out Unit Tests for internal methods, the internal methods have a clear "expected" assertion "reality" type of tests where we expect a certain output for a certain input. (e.g. `add(2,2).should.equal(4)`).
As I'm trying to build tests to make sure our external API calls would be continually functional, what's the best way to structure the assertion in a unit test to make it work?
For instance we have a method that pulls a single contact from Hubspot. Matching the JSON object property by property seems unreasonable and ineffective.
Should I... match just the properties of interest in the test? (e.g. `contact.name === 'Phillip Chan'`)? Or somehow assert how the shape of the JSON object should be?
How would you build a reliable and effective unit test?
P.s. I'm unit testing in Node using Mocha.
Instead of writing unit tests directly you can use API contracts.
Spring Cloud Contracts- JavaorPact - Multiple languages supported(URL).API contract will define the request and response style tests for establishing the API contracts.
Further Pact and Spring Cloud Contracts can be injected into the DevOps tool-chain to automate the entire process .
For JS :
Pact is compatible with Jest (Node), Mocha (JS), Karma (JS), Jasmine(JS)
Sample Pact Test :
With non specific response values :
When "creating a user with an invalid username" POST /users { "username": "bad_username", ... } Then Response is 400 Bad Request Response body is { "error": "<any string>" }With Specific response values :
When "creating a user with a username containing numbers" POST /users { "username": "us3rn4me", email: "...", ... } Then Expected Response is 400 Bad Request Expected Response body is { "error": "username can only contain letters" }