Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Git Pre-Commit Hook That Fails if "it.only" Used (Jest/Jasmine)

DZone's Guide to

Git Pre-Commit Hook That Fails if "it.only" Used (Jest/Jasmine)

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

One of the annoying things with Jest is that while it enables you to run only a single test by using it.only, it does not report this in any noticeable way. Thus you can end up in the same situation as we did, not running many tests without knowing it. (Oh yeah, if we only did review the code properly …).

This git pre-commit hook will fail when you introduce it.only into the code:

#!/bin/sh
# A git pre-commit hook that verifies that the change does not introduce
# the use of a Jest/Jasmine exclusive test via 'it.only(..)', which would
# prevent most other tests from being run without any clear indication thereof

# Redirect output to stderr.
exec 1>&2

ADDED_IT_ONLY=$(git diff -U0 --cached -S"(\W|^)it\.only\s+?\(" --pickaxe-regex | egrep "(^\+.*it.only)|\+{3}")

if [ -n "$ADDED_IT_ONLY" ]; then
    echo "PRE-COMIT CHECK FAILED: You have added calls to it.only(..) thus preventing other tests from running, please fix: $ADDED_IT_ONLY"
    exit 1
fi

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs

Topics:

Published at DZone with permission of Jakub Holý, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}