DEV Community

mpa-LHutchinson
mpa-LHutchinson

Posted on

Week 10: Lab 7: Automated testing

Introduction

For this weeks lab, I introduced a testing framework to my release 0.1 project, writing test cases for all the commands that were available. This included successful inputs of each command as well as well as their respective error cases if they had any. In this blog, I will discuss the testing framework I used, along with the troubles I had with making tests for this lab, since not everything ended up working out unfortunately.

Which testing framework/tools did you choose? Why did you choose them? Provide links to each tool and briefly introduce them.

The testing framework I choose for my project is jest, which I found from this list of popular node testing frameworks:

https://developer.okta.com/blog/2020/01/27/best-nodejs-testing-tools

Jest is a javascript testing tool that allows for the creation of easy yet powerful test suites. I choose jest because, since I was new to testing in javascript, I wanted to use one of the most popular tools available so I had as much available documentation on it as possible.

How did you set them up in your project? Be detailed so that other developers could read your blog and get some idea how to do the same.

Setting up node.js in a project is simple. First, you need to install it through npm

npm install --save-dev jest

Then, for simplicity, add a script in the package.json file of your project, like so:

"scripts": {
"test": "jest"
}

once you have this, you can run npm test and any files with the name "test" will have their test cases ran. It's recommended to keep them in a folder names tests or something along those names.

Next, to create a test suite, set it up like this:

`describe('index.js (nonLLMtests)', () => {

test('displays welcome message if only two arguments are provided', () => {
const output = execSync('node ./index.js').toString().trim();
expect(output).toBe(
'Hi! Welcome to Auto-ReadMe. For help, run this command: node index.js --h. Enjoy!'
);
});
};`

Here, "describe" creates a test suite, and "test" creates a test case. In the example, I test my basic welcome command by creating the proper arguments and "expecting" a certain output.

How did you handle LLM response mocking?

This is where I got stuck this lab, as I wasn't really able to figure out how mocking could work for my program. I tired using nock to create a mock HTTPS server, but no matter what I couldn't figure out a way to get it to create a request. So For the purposes of this blog, I couldn't handle LLM response mocking. I will figure this out in the future, so for now I just had my LLM related test cases use the real LLM.

What did you learn while writing your test cases? Did you have any "aha!" moments or get stuck?

As discussed in the previous section, I did get stuck on the response mocking and was unable to complete it for this lab. However, creating the other test cases made me realize that my program needs a proper configuration file where variables used across multiple files is defined (including the test cases). Doing this made changing the code for testing much simpler, and working with the code will be much easier in the future.

Did your tests uncover any interesting bugs or edge cases?

Interestingly enough my tests, once properly implemented, did not show any bugs or edge cases. When the tests were written properly, they gave the expected output with the proper input.

What did you learn from this process? Had you ever done testing before? Do you think you'll do testing on projects in the future?

In this process, I learned how to create tests with jest and how to best utilize them. I have done testing before, mainly with unittest in python, but I have never done it with jest in a javascript project. In the future, I will implement tests as long as the project warrants them, since the process of creating test cases is very long and tedious. But ultimately having them greatly helps the project and automates the process of finding problems/bugs with code.

Top comments (0)