MyAI Written Tests for Eleventy JavaScript Files

I followed several JavaScript courses. They taught my about the process, and how to think about functions, but they rarely touched upon writing tests to ensure that the functionality remains when you refactor or make changes. While experimenting with Eleventy and refactoring I tidied up some code whilst in the process removing functionality that I didn’t want to remove. It took a while to fix.

That’s why I then grabbed the opportunity to begin dabbling in the realm of tests to ensure that site functionality was not downgraded from one revision to the next.

The process is interesting. You present MyAI with a Javascript file and it will write functions that test to see if the functions are behaving as they are meant to. Each time a new test file is created you can run the tests and it will see if there are errors and if there are it sees whether to change the code, or to change the test logic to match what the function is meant to do.

The danger, in refactoring, and experimenting with JavaScript is that it’s easy to remove a function that breaks the build which is noticable, or changes a behaviour that you don’t notice. The key example is posts no longer having an excerpt or the archives page only having two items. With tests both are checked for consistency.

Method

I provided MyAI with the JS file with a list of functions. In some cases it understood the logic and wrote a test for each function. It then got me to run the test, and depending on whether there was an error message or not, it provided feedback on how to adapt the test or the function. This was just a cursory glance into how it dealt with writing tests. I’m curious to push further.

And Finally

When you’re refactoring it’s easy to break functionality so it’s good to check that you don’t break it. It’s also interesting to see what tests look like, as an initiation into the realm of Test Driven Design. From now on I experiment with a different design methodology.

I want to explore this in more depth.