Communicating API Semantics Through Unit Tests

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation