Search code examples
vue.jsvuejs3vitevitest

How to chain vite build and tests?


It seems that vite build command doesn't run tests (unit tests written with vitest, for example) before building the application for production. This is quite unsafe.

Is there a way to configure vite build to run tests before trying to build ?

I couldn't find anything in Build Options.


Solution

  • What do you have in your package.json, scripts section? Don't you have some test or alike?

    build is used to bundle your app and ship it to production. It's not supposed to run any tests by default.
    You could achieve this by doing something like

    "scripts": {
      "build": "vite build && npm run test:unit && npm run test:e2e",
      "test:unit": "vitest --environment jsdom",
      "test:e2e": "start-server-and-test preview http://localhost:4173/ 'cypress open --e2e'",
    },
    

    If you generate a new project via the CLI, you will have most of them already written for you, then it's a matter of properly chaining them with && to assure that they are all succeeding before proceeding further.

    enter image description here


    You can also add some Git hooks with something like husky + lintstaged so that your flow is using something by default before even pushing it to a remote repo.

    Otherwise, it's part of your CI. Either it be a Docker compose file, some Github actions, Gitlab pipelines or anything your devops team could have setup for your deploy environments.