Advice for including JavaScript unit tests

Has anyone had good experience adding unit tests for JavaScript on a Jekyll site? I want to avoid breaking functionality when making changes and I believe unit tests could help with that. I’ve never written JS tests. I found some testing frameworks, but I find it looks quite complex to add them to a Jekyll project.

Unit tests is a great idea.

At the simplest, use a framework like jest and test your JS files manually with a test command and push if it passes.

You can setup GH Actions to run custom steps. Here I have one which runs an action to deploy to GH pages.

If you find add some steps before around npm install and npm run test then you can make sure the deploy to GH pages only happens if tests pass. Check the sample NPM workflow on the page where you add a new action.

You can also have multiple “jobs” items in the workflow file. You could look at tests and deploy as jobs and make deploy dependent on tests.

I find the syntax heavy to get all that stuff setup.

In Netlify its really simple. One line.

build: npm run test && jekyll build

You don’t even have to install. It figures that out for npm and jekyll based on package files.

Some JS test bootstraps

1 Like

That’s great advice thanks. I’m not using GH Pages, but I can obvs include the testing in my deployment script. Probably a lot faster than doing that as a build step too.

1 Like