Communicating API Semantics Through Unit Tests

Ole Ersoy
1 min readSep 12, 2022

--

Image by Manuela_Burkhalter from Pixabay

Scenario

We have the URL.

let urlC = "http://example.com"

We want to communicate via our unit tests that new URL(urlC).pathname results in / .

Approach

Since there is no path in the URL we may expect the path to be "" , however it is / , which is good for our API users to know.

let urlA = "http://example.com/aa/bb/"
let urlB = "http://example.com/"
let urlC = "http://example.com"
it(`should be true`, () => {
expect(isURLPathsEqual(urlA, "/aa/bb/").value).toBeTruthy();
expect(isURLPathsEqual(urlB, "/").value).toBeTruthy();
expect(isURLPathsEqual(urlC, "/").value).toBeTruthy();
})
it(`should be false`, () => {
expect(isURLPathsEqual(urlA, "/aa/bb/cc").value).toBeFalsy();
expect(isURLPathsEqual(urlC, "").value).toBeFalsy();
})

For the isURLPathsEqual implementation see.

For the corresponding tests see.

And for the NPM repository containing the validators and sanitizers see.

--

--