Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created April 8, 2014 22:40
Show Gist options
  • Save Raynos/10203887 to your computer and use it in GitHub Desktop.
Save Raynos/10203887 to your computer and use it in GitHub Desktop.

Fast pre-commit tests

pre-commit allows you to run linting & unit tests in a pre commit hook.

Generally a pre-commit hook should only test & lint that which has changed.

Linting what has changed.

This one is easy.

jshint --verbose $(git diff --cached --name-only | grep '\\.js$')

Testing what has changed.

To be able to test what has changed we need a command called node-parents

node-parents $(git diff --cached --name-only | grep '\\.js$') \
  | grep 'test' \
  | grep -v 'node_modules' \
  | xargs -L node

node-parents will take a list of files and print all the parents & grandparents & etc that require it recursively.

This allows you to find test/foo.js that requires the changed file directly or indirectly

For node-parents to be useful in this context it should have an mtime based stat cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment