We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Normalize URLs

Test-driven development is a popular method of writing software. The idea is that you write tests for your code first, then you write the code that gets the tests to pass. We're going to approach this project using a bit of TDD.

Normalizing URLs

We need a function that accepts a URL string as input and returns a "normalized" URL. To "normalize" means to "make the same". For example, all of these URLs are the "same page" according to most websites and HTTP standards:

  • https://www.boot.dev/blog/path/
  • https://www.boot.dev/blog/path
  • http://www.boot.dev/blog/path/
  • http://www.boot.dev/blog/path

We want our normalizeURL() function to map all of those same inputs to a single normalized output: www.boot.dev/blog/path.

Keep in mind, the normalized URL isn't going to be used to make requests, it's just going to be used to compare URLs to see if they are the same page.

Stub the Function

In test driven development (TDD), you typically follow this flow:

  • Stub out the function
  • Write the tests
  • Write the code that passes the tests

You are going to be using the Vitest testing library for TypeScript.

Assignment

npm install -D vitest
...
"scripts": {
  "start": "tsx ./src/index.ts",
  "test": "vitest run"
},
...

I'd recommend you read up on Vitest's getting started guide here. By default, the vitest command will look for files that end in .test.ts, so write your first tests in a file called crawl.test.ts in the src directory.

Run and submit the CLI tests from the root of the module.