question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documenting running tests in precommit hook

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
luftywiranda13commented, Jun 21, 2017

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

you can then implement what I suggested in https://github.com/facebookincubator/create-react-app/issues/2336#issuecomment-303349467

"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!

0reactions
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supported hooks - pre-commit
php-unit - Run the full php unit test. Checks which PHPUnit executable is available first and then runs it. Preference order is vendor/bin,...
Read more >
Git Pre-Commit Hooks and Specta's Focused Examples
A Git pre-commit hook to prevent accidentally committing a Specta test suite with a focused examples (fit or fdescribe).
Read more >
Automate Your Python Code Documentation with Pre-commit ...
Automate Your Python Code Documentation with Pre-commit Hooks. A hands-on guide to automating docstrings checks and auto-generating ...
Read more >
Git Hooks - Git SCM
The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to...
Read more >
Useful git pre-commit hooks for R - README
Pre-commit hooks are tests that run each time you attempt to commit. If the tests pass, the commit will be made, otherwise not....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found