DEV Community

aldrin312
aldrin312

Posted on

Unit testing and LLM mock tests.

For this lab I had to include unit testing and mock LLM responses with my project. I used mocha and chai for my unit testing. Both of them wasn't particularly hard to figure out. To briefly explain them, Mocha is a JavaScript test framework that runs on Node.js and in the browser, making asynchronous testing. It allows developers to write tests in a clear and organized way. While Chai is an assertion library that can be used with any JavaScript testing framework. It provides a variety of assertion styles (BDD, TDD) to suit different testing approaches.

These two are particularly easy to set up, all I need to do is run the function in the test.js file and compare them to what the expected result should be. For instance, if the function is a success, I compare its return value to what a success value should be. I can also test it for expected fails and errors. Like with my read file function, it can only receive specific file format. When I run it with an improper file format, it should result an error and then I compare that error to what the expected error should be. In this case its "Error: Improper file format".

As for mock testing with LLM responses, I first used nock but it turns out I cannot use this since I'm using Groq. Groq internally handles its Api endpoint, so I can't access that as far as I know. So instead I used sinon. Sinon allows you to create spies, stubs, and mocks, which are essential tools for testing code that has side effects, interacts with external services, or involves asynchronous operations. This one took a while to figure out. I'm not familiar to test cases with mock responses, so this took a bit more research. But the gist of it, I think, is that in order to compare something to LLM responses, I first need to make a mock expected response result. That's where Sinon comes in. I take the response from Sinon mock, which is the expected outcome and compare the LLM response with it. That's how I would test the LLM output responses of my project.

What I learned.

Generally unit testing is pretty straight forward, test function and classes with expected results. Both with success and fails. And mocking is used to check responses. This part I got stuck on for a while. Took me a while to understand what's going on. I think I understand the bit of it. At least be able to use it on my project.

I do think that testing is really important, especially on open source projects or a project with a huge team. It keeps the consistency of the project with each implementation and make sure that each one of them works on submission.

Top comments (0)