How to configure husky pre-push hook to run tests

16k Views Asked by At

I am trying to configure husky pre-push hook to run tests before push. Everything looks fine. After run git push origin hook is fired and tests are runing. Problem is that when tests are done and even if operation is successed, push is freezed and nothing else happened.

package.json

  "scripts": {
    ...
    "pre-commit": "npm run lint",
    "pre-push": "npm run test"
  }

.husky/pre-push

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-push

enter image description here

3

There are 3 best solutions below

1
On BEST ANSWER

By default unit tests run in watch mode. You can override this by doing ng test --watch=false

0
On

This is what worked for me

npm test -- --watchAll=false

I've added that to the pre-commit file that was generated in the husky folder in the repo

This is the whole content of the file

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm test -- --watchAll=false
1
On

If you re using Vitest:

yarn vitest --run