Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 258
Guy Avatar asked Jul 08 '15 21:07

Guy


People also ask

What is fit in Jasmine?

fit will focus on a test or a set of them. so if you have 5 tests, 3 it and 2 fit , only the 2 with fit will run by Jasmine.

What is Fdescribe?

fdescribe: FunctionLike describe , but instructs the test runner to only run the test cases in this group. This is useful for debugging. See http://jasmine.github.io/ for more details.


2 Answers

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
like image 97
Dominik Ehrenberg Avatar answered Sep 30 '22 02:09

Dominik Ehrenberg


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

like image 37
Alan Avatar answered Sep 30 '22 03:09

Alan