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:
- Save Gist in a a folder called
TSLint/Rules
asnoJasmineFocusRule.js
- Add the Rules folder to your TSLint config:
rulesDirectory: 'TSLint/Rules'
- 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:
npm i --save cross-env
package.json:
"scripts": { "test": "jasmine", "test-safe": "cross-env FOCUSED_TESTS=off jasmine" },
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"); }
Tell codeship to run
npm run test-safe
instead ofnpm 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.
- First install eslint globally
npm install -g eslint
. - Then install the eslint-plugin-jasmine library
npm install --save-dev eslint-plugin-jasmine
. Create a
.eslintrc
file which would look something like this:{ "rules": { "semi": 2 }, "plugins": ["jasmine"], "env": { "jasmine": true }, "extends": "plugin:jasmine/recommended", }
Then you are ready to run the linter
eslint -c ./.eslintrc app.js