20

fdescribe() and fit() are great for reducing noise when you're working on a subset of tests. I sometimes forget to change them back to describe()/it() before merging my branch into master. (It's okay to have them in separate branch while working on code - i.e. a pre-commit check wouldn't work for me.)

My CI environment is Codeship. Is there a solution to this problem that would fail the tests in Codeship if it came across any focused methods?

Using something like no-focused-tests would be okay. Any idea how to enable this rule as an error in Codeship and disable it locally?

Guy
  • 59,547
  • 93
  • 241
  • 306
  • 2
    `no-focused-tests` saved me so often before :). How about something like a pre-merge hook? (http://stackoverflow.com/questions/19102714/how-would-i-write-a-pre-merge-hook-in-git) – alecxe Jul 11 '15 at 02:14
  • Unfortunately the merge takes place on GitHub so unless it can be triggered as rule on the GitHub servers I don't think that would work. Thanks for the idea though! +1 – Guy Jul 11 '15 at 15:06
  • 2
    I run jshint as part of my build it catches this sort of thing. – sam schonstal Jan 17 '16 at 02:14

6 Answers6

6

Edit 14.11.19:

To make things easier I created an installable package you can find at https://www.npmjs.com/package/tslint-jasmine

Original post:

If you're using TSLint and (like me) found that all the defocus and tslint-jasmine-noSkipOrFocus checkers are not working for you, I created a Gist for that: https://gist.github.com/djungowski/7d9126bb79970446b4ffeb5656c6bf1f

How to use:

  1. Save Gist in a a folder called TSLint/Rules as noJasmineFocusRule.js
  2. Add the Rules folder to your TSLint config: rulesDirectory: 'TSLint/Rules'
  3. Enable option with "no-jasmine-focus": true
Dominik Ehrenberg
  • 1,087
  • 1
  • 11
  • 8
  • That's an elegant way of doing it. Thank's. – ibenjelloun Feb 21 '19 at 09:54
  • 1
    This works great with husky "pre-push" hook to prevent an `fdescribe` or `fit` from limiting your deployment pipeline automated tests. e.g. package.json `"husky": { "hooks": { "pre-push": "ng lint && ng test --watch=false --browsers=ChromeHeadless" } }` – Andrew Mar 19 '19 at 06:48
3

Using something like no-focused-tests would be okay. Any idea how to enable this rule as an error in Codeship and disable it locally?

You could use a combination of environment variables and redefining the fdescribe/fit global functions:

  1. npm i --save cross-env

  2. package.json:

    "scripts": {
      "test": "jasmine",
      "test-safe": "cross-env FOCUSED_TESTS=off jasmine"
    },
    
  3. disableFocusedTestsIfNecessary.js (included after jasmine defines its globals):

    if (process.env.FOCUSED_TESTS === "off") {
      console.log("Focused tests must be off");
      global.fdescribe = global.fit = function() {
        throw new Error("fdescribe and fit are disabled in this environment");
      };
    }
    else {
      console.log("Focused tests enabled");
    }
    
  4. Tell codeship to run npm run test-safe instead of npm run test

Alan
  • 255
  • 2
  • 12
3

For those interested, if you are using jasmine and eslint, you can use this plugin to ensure no focused tests: https://github.com/tlvince/eslint-plugin-jasmine.

  1. First install eslint globally npm install -g eslint.
  2. Then install the eslint-plugin-jasmine library npm install --save-dev eslint-plugin-jasmine.
  3. Create a .eslintrc file which would look something like this:

    {
      "rules": {
        "semi": 2
      },
      "plugins": ["jasmine"],
      "env": {
        "jasmine": true
      },
      "extends": "plugin:jasmine/recommended",
    }
    
  4. Then you are ready to run the linter eslint -c ./.eslintrc app.js

TheNastyOne
  • 717
  • 7
  • 15
0

This isn't the best solution. But it works for my needs.

To setup:

npm i lodash
npm i minimist

I call this from my gulp tasks:

node .\\build\\throwIfFocusedTest.js e2e/
node .\\build\\throwIfFocusedTest.js src/

throwIfFocusedTest.js:

const walkSync = require('./walkSync').default;
const _ = require('lodash');
const argv = require('minimist')(process.argv);
const fs = require('fs');

if (argv._.length !== 3) {
    throw 'expecting 1 command line argument';
}

const directory = argv._[2];

const files = walkSync(directory);
const scriptFiles = _.filter(files, f => f.endsWith('.js') || f.endsWith('.ts'));

const invalidStrings = [
    'fdescribe',
    'fit',
];

_.each(scriptFiles, fileName => {
    const contents = fs.readFileSync(fileName, 'utf8');
    invalidStrings.forEach(is => {
        if (contents.includes(is)) {
            console.error(`throwIfFocusedTest: ${directory}: File contains ${is}: ${fileName}`);
            process.exit(1);
        }
    });
});
console.log(`throwIfFocusedTest: ${directory}: No files contain: ${invalidStrings.join(', ')}`);

walkSync.js:

/**
 * From: https://gist.github.com/kethinov/6658166 
 */
exports.default = function walkSync(dir, filelist) {
    var fs = fs || require('fs'),
        files = fs.readdirSync(dir);
    filelist = filelist || [];
    files.forEach(function (file) {
        var path = dir + file;
        if (fs.statSync(dir + file).isDirectory()) {
            filelist = walkSync(dir + file + '/', filelist);
        }
        else {
            filelist.push(path);
        }
    });
    return filelist;
};
Jesus is Lord
  • 13,696
  • 9
  • 54
  • 84
0

I'm late to the party.

I had a similar issue with my builds. We don't use ts / eslint so I just wrote a quick script to throw an error that would fail my dockerfile / build.

Here it is.

#!/bin/sh
files=$(find "./.." -type f -name '*.spec*')
errored=false

echo "Checking for focused tests"

for file in $files
do
    if grep -E "fdescribe|fit" $file; [ $? -eq 0 ]; then
        echo "-Focusing a test in the file $file"
        errored=true
    fi
done

if $errored; then
    echo "Some tests were focused"
    exit 1
else
    echo "No tests were focused"
fi
Questioning
  • 1,500
  • 22
  • 40
0

If you're willing to fail on when tests are marked for focus or skip (fit + xit), there's a relatively new Karma feature that solves the problem with no plugins. Karma now supports a failOnSkippedTests config file / CLI option, which, per the docs, causes "failure on tests deliberately disabled, eg fit() or xit()".

Coderer
  • 21,290
  • 22
  • 71
  • 124