Skip to content
Advertisement

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

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement