Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting running tests in precommit hook #2587

Closed
vivekkalyan opened this issue Jun 21, 2017 · 8 comments
Closed

Documenting running tests in precommit hook #2587

vivekkalyan opened this issue Jun 21, 2017 · 8 comments

Comments

@vivekkalyan
Copy link

I want a git pre-commit hook that runs test on staged files before commits (similar to this)

This will require access to jest configs such as bail and findRelatedTests.

currently i am running this pre-commit

#!/bin/sh
PASS=true
echo "running tests"
CI=true npm test
if [[ "$?" == 0 ]]; then
    echo "\t\033[32mJest Tests Passed\033[0m"
else
    echo "\t\033[41mJest Tests Failed\033[0m"
    PASS=false
fi

but this results in every single test being run, which takes a long time. i want to effectively run

#!/bin/sh
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep "js$")
if [[ "$STAGED_FILES" = "" ]]; then
    exit 0
fi

PASS=true
echo "running tests"
jest --bail --findRelatedTests $STAGED_FILES
if [[ "$?" == 0 ]]; then
    echo "\t\033[32mJest Tests Passed\033[0m"
else
    echo "\t\033[41mJest Tests Failed\033[0m"
    PASS=false
fi

I believe allowing for this will encourage better practices and am willing to take a stab at it if the maintainers think that it would be useful.

(also open to other solutions that people might use to achieve the same functionality)

@luftywiranda13
Copy link
Contributor

any plan to use https://github.com/okonet/lint-staged?

you can then implement what I suggested in
#2336 (comment)

"scripts": {
  "precommit": "lint-staged",
  "test:staged": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"
}

then

"lint-staged": {
  "src/**/*.js": [
    "test:staged",
    "git add"
  ]
}

for the env variable, use cross-env.. it is cross platform

hope it helps!

@vivekkalyan
Copy link
Author

@luftywiranda13 thanks for that it works like a charm. i read that issue but must have glazed over ur solution, my bad!

should this be documented? seems like something that people would want to use as their apps getting bigger.

@gaearon
Copy link
Contributor

gaearon commented Jun 22, 2017

Yes, would be great to document this.

@luftywiranda13
Copy link
Contributor

@bowsplinter @gaearon ok, i'll make a PR 😊

@gaearon gaearon changed the title Support for configuration of jest Documenting running tests in precommit hook Jan 8, 2018
@dan-weaver
Copy link

dan-weaver commented Jan 24, 2018

I may be doing something wrong, but I'm finding that my precommit hook fails (rejects the commit) when there are no tests found via the jest --findRelatedTests <file-name>. Anyone managed to fix this in their setups? Should jest not throw an error when there are no tests? That would seem more reasonable. If so, I can file an issue over there

@luftywiranda13
Copy link
Contributor

@dan-weaver jest v22.1.0 exits without an error when no tests are found changelog

or you can pass --passWithNoTests

@dan-weaver
Copy link

Just saw that thanks.. Sorry for the additional noise here :)

@stale
Copy link

stale bot commented Nov 2, 2018

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs.

@stale stale bot added the stale label Nov 2, 2018
@Timer Timer closed this as completed Nov 2, 2018
@lock lock bot locked and limited conversation to collaborators Jan 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants