1, 2, 3, Testing: Setting Up Tests
Tags: beginners, javascript, opensource
Published: 2022-11-11 15:56:29

## This week's goal
For my [static site generator](https://github.com/cychu42/staticSiteCon), I was trying to set up tests that can be ran easily.
I mean, it takes effort to run the same test manually and hope you don't forget to test all the specifics.
Don't worry, you can can set up tests to run with one command instead!

## How?
Depends on what you are working with, you can try different choices of tools. For this repo, I'm using JavaScript, so I picked [Jest](https://jestjs.io), since it's rather close to what I know of writing tests for JavaScript without a lot of code to set up the tests.
Plus, having some passing familiarity with the tool already helps me use it.

## Steps (See [Jest documentation](https://jestjs.io/docs/getting-started) for more details)

1. In your project's root folder, run `npm install --save-dev jest`. If you are on Windows, you might need to install it globally by running `npm install -g jest`, for Jest commands to be recognized.
2. In your `package.json` file, make sure script's `"test"` is set to `"jest"`. If you don't have the file, run `npm init` in your project's root folder first.
3. Write a test. For example, if you want to make a test file for `writer.js`, create a `writer.test.js` file in a `tests` folder (or anywhere you like in your project), and make sure all tests within `writer.test.js` are about code inside `writer.js`. For syntax, look into [Jest's own guide](https://jestjs.io/docs/using-matchers).
4. Run `npm test` to run all test and fix your code or test accordingly. To run individual tests, use `npm test [test-path]`. For example, `npm test ./tests/parser`. The `.test.js` extension is not required for the command.

## What I learned
It was mostly straight forward as I have some idea about what I want my functions to do and don't do. The tricky part was refactoring my code to make it easier to write tests for.
For example, the course line parsing part of my code was dependent and intertwined with functions from different modules, which makes it hard to isolate the code for testing. IT took me a bit of time to figure out how to isolate those into a function to be exposed for testing.

As for interesting bug...this isn't about the code I had, but I discovered something in the process of writing a test.
If you declared a variable without defining it, and then try to use it to concatenate with a string, it will store the `undefined` text plus the string. It makes more sense now I'm typing it out, but it didn't occur to me because JavaScript isn't strongly typed and often convert things for you, so I didn't expect this to happen while writing code for a test itself. I thought it would just try it as an empty string and then concatenate with a string.

## Conclusion
I learned a lot about how to set up tests for codes from this. I have done tests before, but it's mostly manual testing.
This is definitely easier and more reliable than testing thing manual ly, so I think I will do so in the future projects. 
One thing that changes the way I look at developing code is the idea that one should consider how easy it is to test the code when writing it. This is something I would definitely look out for in the future.