Disable Jasmine's fdescribe() and fit() based on environment

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

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


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