5

As in the title, at the moment I have configured my app to run tests every time a git push is executed,

"husky": {
    "hooks": {
      "pre-push": "npm run test:unit"
    }
  }

but evidently it does not scale well, the more tests the more time it will take to push, so is there a way I can trigger the tests only if there are changes in the files?


eventually I've found that Jest has a nice flag called --changedSince so you can have something like this in your package.json file:

"test:unit": "test:unit --changedSince=@{push}"

that will execute only the tests of the files that have been changed making a comparison with the remote HEAD since last push

Andrea
  • 351
  • 1
  • 3
  • 9

1 Answers1

2

A good option to achieve this is the prepush-if-changed package.
It allows you to specify a match pattern for files for which to trigger the hook when changed.

  "husky": {
    "hooks": {
      "pre-push": "prepush-if-changed"
    }
  },
  "prepush-if-changed": {
    "src/**/*.js?(x)": "npm run test:unit"
  }
Laurens Deprost
  • 1,653
  • 5
  • 16
  • Thank you for the answer, I have ended up making a small script and using Jest flag `--changedSince=` somthing like this `"test:unit-master": "vue-cli-service test:unit --changedSince=origin/master",` `"test:unit-remote": "vue-cli-service test:unit --changedSince=@{push}",` – Andrea Sep 11 '20 at 04:14
  • @Andrea I'm getting an error for this command "fatal: no upstream configured for branch " – Gayan Charith Apr 27 '22 at 05:29