Page MenuHomeDevCentral

Add root package.json with unified test script
Open, HighPublic

Description

No way to run all tests from the project root. Create a root package.json with npm test running both backend (Jest) and frontend (Vitest) via --prefix.

Add test:backend and test:frontend shortcuts.


  • Backend: cd backend && npm test (Jest)
  • Frontend: cd frontend && npm run test:unit (Vitest)

We could add a root package.json with a unified script. It would look like:

{
  "name": "servpulse",
  "private": true,
  "scripts": {
    "test": "npm test --prefix backend && npm run test:unit --prefix frontend",
    "test:backend": "npm test --prefix backend",
    "test:frontend": "npm run test:unit --prefix frontend"
  }
}

Then npm test from the project root runs everything.

Event Timeline

ieli triaged this task as Normal priority.Mon, Feb 16, 15:30
ieli created this task.
ieli raised the priority of this task from Normal to High.Mon, Feb 16, 15:33
ieli updated the task description. (Show Details)

Copying from Discord:

  • If we want to run LOCALLY anything before commit to have an opportunity to fix it, we need to use a pre-commit hook for Git
  • Then, we can also, but that's for later and probably for T2244, run the same script also after a diff is sent (main goal is to ensure it runs when external contributors submit a change or if we forget to run it locally)

Reference to configure it: https://dev.to/akdevcraft/git-pre-hook-pre-commit-hook-with-npm-project-44p2

As pre-commit is an individual setting, not something we share, I'd suggest a two steps plan:

(1) we add to the repository, for example in a support/ folder, a script to run the tests
(2) we document how to call that script as git pre-commit


For example if the run tests script is support/run-tests.sh, we need to document that content is to be added to .git/hooks/pre-commit:

#!/bin/sh

REPO_PATH=$(git rev-parse --show-toplevel)
sh -c "$REPO_PATH/support/run-tests.sh"

Important: both test runner script AND pre-commit are just executables, so we can do them in any language we want, like in Node.js or in shell script.

Finally, if we want to share the hook for everyone, a popular way to do this for projects is https://pre-commit.com/