Communicating API Semantics Through Unit Tests | Task

Ole Ersoy
Aug - 12  -  1 min

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 this link.

For the corresponding tests see this link.

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