JavaScript: Run Test Suite before building

I would like to run my unit tests before the app builds. I’ve read a few articles but couldn’t work out the proper way to do it. Currently I’ve amended my build process from yarn build to yarn test && yarn build. If the test suite fails then the build will fail and if the test suite passes then the build will run.

This is the desired outcome, I was just wondering if there is a better way to do this?

You can have a more fine grained control over you build process steps with the new Netlify Build Plugin this is in beta, but i would highly recommend using it if you have scenarios when you need to test something in the site after the site was built but not deployed (preDeploy step).
There are other steps which can be used pending on your use cases. check the link above :point_up_2:

If your tests are not dependent on the generated output from the SSG you could use github actions an stop the flow before it hits Netlify.
This is useful to check spelling errors or linting for instance, so stuff which are more bound to code (code quality, security issues, formatting, etc)

2 Likes

Thanks dude, I’ll check out Github actions.

Although I was slow to suggest it, this article covers a bunch of test patterns but also validates yours as a good practice here:

1 Like