Skip to content

Instantly share code, notes, and snippets.

@kadams54
Last active August 5, 2020 21:37
Show Gist options
  • Save kadams54/2ea47c36b85a1b9fcdd003f151abc4d7 to your computer and use it in GitHub Desktop.
Save kadams54/2ea47c36b85a1b9fcdd003f151abc4d7 to your computer and use it in GitHub Desktop.
StandardJS (ESLint + Pretter) config for Cypress projects

Style

This setup adheres to JavaScript Standard Style; unfortunately due to conflict between standard's ruleset and Chai assertions, we have to acheive this by using eslint directly + the Standard rules. Since we can't use standard directly, we also throw in Prettier for code formatting. Specifically, we use prettier-standard to keep all of the pieces working together nicely. What's that mean for you? Well, here's the scoop on setting up a few editors for linting/formatting fun.

Vim

If you're using Vim, you should be using ALE. It's a fantastic asynchronous linting engine (which is what ALE stands for). After installing, ALE's linting setup doesn't need any tweaking, but you'll need to explicitly specify a fixer for Javascript:

" In ~/.vim/ftplugin/javascript.vim, or somewhere similar.

" Fix files with prettier, and then ESLint.
let b:ale_fixers = ['prettier_standard']
" Equivalent to the above.
let b:ale_fixers = {'javascript': ['prettier_standard']}

Additionally, you may want to specify some additional global config in your .vimrc (if you have not done so already):

" Runs the fixer upon save; otherwise :ALEFix will need to be run manually
let g:ale_fix_on_save = 1
" Opens a list of the formatting/linting errors upon save
let g:ale_open_list = 'on_save'
" Switches the list of errors from a loclist to the quickfix list
let g:ale_set_loclist = 0
let g:ale_set_quickfix = 1

Spacemacs

Spacemacs needs the following configuration layers enabled in your ~/.spacemacs file:

dotspacemacs-configuration-layers
'(
  ...
  (javascript :variables node-add-modules-path t)
  syntax-checking
  ...
)

Additionally you'll need to disable js2-mode's built-in syntax checking by setting the following in the defun dotspacemacs/user-config () section:

(defun dotspacemacs/user-config ()
  ...
  (setq-default
    ...
    js2-mode-show-parse-errors nil
    js2-mode-show-strict-warnings nil
    ...
  )
  ...
)

Note that this only enables linting; code formatting with Prettier isn't covered. That's not to say it can't be done, just that we haven't figured out how to do it yet. Consquently you'll still need to duck out to the command line and run npm run-script format or yarn format to tidy up your code.

Visual Studio Code

Coming soon.

Atom

  1. Install the prettier-atom package (including, if necessary, the linter dependency)
  2. In prettier-atom's settings, enable the following:
    • ESLint Integration
    • Format Files on Save
  3. Install the linter-eslint package
  4. In linter-eslint's settings, enable the following:
    • Ignore fixable rules while typing
  5. Restart Atom and install any necessary dependencies
{
"scripts": {
"format": "prettier-standard './**/*.{json,js,md}'",
"format:check": "prettier-standard --check './**/*.{json,js,md}'",
"lint": "eslint --fix './**/*.js'",
"lint:check": "eslint './**/*.js'",
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-chai-friendly": "^0.5.0",
"eslint-plugin-cypress": "^2.9.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-mocha": "^6.3.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"prettier-standard": "^16.1.0"
},
}
{
"env": {
"browser": true,
"mocha": true
},
"rules": {
"mocha/no-hooks-for-single-case": "off",
"mocha/no-exclusive-tests": "error"
},
"extends": [
"standard",
"plugin:cypress/recommended",
"plugin:chai-friendly/recommended",
"plugin:mocha/recommended"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment