Mocha: Have option to make tests warn or fail on describe.only, it.only, xdescribe or xit

Created on 3 Feb 2015  路  12Comments  路  Source: mochajs/mocha

There have been many a time when I have glanced over describe.only, it.only, xdescribe or xit in a PR. Unfortunately, these PRs get merged and can cause problems with tests that should fail going undetected.

It would be tremendously helpful if there was an option or environment variable to automatically exit with a non-zero code upon detecting these methods in tests. This would run in CI and automatically fail the build.

Most helpful comment

Sample git pre-commit hook that might be useful:

#!/bin/sh

echo "Running pre-commit hook..."

results="$(grep --color=always -r '\.only' test)"
exit_code=$?

if [[ $exit_code -eq 0 ]]; then
    red='\033[0;31m'
    plain='\033[0m'
    echo "${red}[!] Some Mocha tests may be using .only():${plain}"
    echo $results
    exit 1
fi

All 12 comments

Regarding the first paragraph, PR reviewers are responsible for whatever gets merged into a repo.

A grep can easily do what you want. Maybe we can wait for +1's, but I don't think many people will ask for this.

What are your thoughts?

That makes sense - grep would work in this case. Apologies for overengineering!

No worries!

I personally would find this useful as well. The value is faster feedback (fail at CI level) instead of at pull request (where it might be missed) and not needing to write extra tooling around it. That said, we are going to use something like the grep route. Thanks for your work on this project!

Sample git pre-commit hook that might be useful:

#!/bin/sh

echo "Running pre-commit hook..."

results="$(grep --color=always -r '\.only' test)"
exit_code=$?

if [[ $exit_code -eq 0 ]]; then
    red='\033[0;31m'
    plain='\033[0m'
    echo "${red}[!] Some Mocha tests may be using .only():${plain}"
    echo $results
    exit 1
fi

You're welcome @panozzaj, and thanks for the sample hook!

+1
An option that make test fail when inclusive or exclusive tests exists, could be very handy option to be run on CI servers in order to be sure that no test is skipping mistakenly.

We've run into the same issue. Would be great to have some sort of setting for this we can configure via the environment, e.g: throwIfFocused(true). RSpec handles it via tags, though not sure if that would work here.

I eventually made a package for this: https://github.com/dasilvacontin/dot-only-hunter.

+1

@kumavis hello buddy, can you open a new issue for this to avoid getting this issue raised from the dead?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DinisCruz picture DinisCruz  路  38Comments

Almad picture Almad  路  41Comments

quangv picture quangv  路  38Comments

jbnicolai picture jbnicolai  路  37Comments

teckays picture teckays  路  84Comments