Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exclude Jest snapshots from git whitespace check

I am trimming whitespace from git commits using git diff-index --check --cached HEAD --. I want to add Jest tests using snapshots, but the snapshot files include whitespace and my tests will always fail if I remove it. So I want to exclude the *.js.snap files from the whitespace check. How do I tell git to exclude *.js.snap (or, alternatively, **/__snapshots/*) files from git diff-index? I'm using bash on OSX.

In the meantime, I'm working around the problem by changing my commit hook to be interactive:

# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
    # Allows us to read user input below, assigns stdin to keyboard
    exec < /dev/tty

    while true; do
        echo "Your commit introduces trailing whitespace.  Are you sure you want to commit? y/n"
        read yn
        case $yn in
            y ) exit 0;;
            n ) exit 1;;
        esac
    done
fi
like image 337
undefined Avatar asked Sep 16 '16 18:09

undefined


People also ask

Should I commit Jest snapshots?

Yes, all snapshot files should be committed alongside the modules they are covering and their tests. They should be considered part of a test, similar to the value of any other assertion in Jest. In fact, snapshots represent the state of the source modules at any given point in time.

How do I view snapshot Jest?

To review your snapshots, run npm run jest-html ( yarn run jest-html ). This launches your default browser and opens the jest-html application. By default, jest-html looks for snapshots under **/*. snap,!

What is the point of snapshot testing?

In snapshot testing, the output of a function is saved in a file (the “snapshot”), and when the test runs, it compares this saved output with the output of the function when it is run each time in the test suite.

What is React snapshot testing?

Snapshot testing has been created due to the need for an easier way to write tests for React components. Many React developers reported that they spend more time writing tests than the actual component. Therefore, snapshot testing allows React developers to quickly generate tests using its simple syntax.


2 Answers

Git does have a way of specifying paths to exclude, though it's poorly documented and apparently not very well known. It's known as pathspec, and in this case can be used as follows:

git diff-index --check --cached HEAD -- ':!*.js.snap' .

where : is the special character that specifies that the pathspec is not just a regular path, and '!' specifies that files matching the rest of the path should be excluded from the list of matches.

Not as straightforward as Mercurial's -X or --exclude, but it's there.

like image 188
undefined Avatar answered Oct 23 '22 23:10

undefined


As mentioned here, the path argument of git diff-index is of glob-style pattern, interpreted by the shell.

So this is more a shell issue than a git command problem.

See for instance "How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?"

You can either try and activate shopt extglob, or (simpler), do a post-step to your script, looking (with git status) for any modified files with **/__snapshots/* in their full pathname, and cancelling their modification (git checkout).

like image 31
VonC Avatar answered Oct 23 '22 22:10

VonC