Last active
October 15, 2019 07:23
-
-
Save SHaTRO/77583ec03685ad2cedc84e88a0ed4bcf to your computer and use it in GitHub Desktop.
Configuring: Typescript + Jest with Code Coverage + ESLint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = { | |
| env: { | |
| browser: true, | |
| amd: true, | |
| node: true, | |
| jest: true, | |
| }, | |
| plugins: [ 'jest' ], | |
| rules: { | |
| 'array-bracket-spacing': [ 'error', 'always' ], | |
| 'block-spacing': [ 'error' ], | |
| 'camelcase': [ 'error', {allow: [ '(_[0-9]+)+$' ]} ], | |
| 'comma-dangle': [ 'error', 'always-multiline' ], | |
| 'comma-spacing': [ 'error', { 'before': false, 'after': true } ], | |
| 'curly': [ 'error', 'all' ], | |
| 'eol-last': [ 'error', 'unix' ], | |
| 'no-extra-parens': [ 'warn', 'all', {'conditionalAssign': false} ], | |
| 'no-multiple-empty-lines': [ 'error', { max: 2, maxBOF: 1, maxEOF: 1 } ], | |
| 'quotes': [ 'error', 'single', 'avoid-escape' ], | |
| }, | |
| overrides: [ | |
| { | |
| files: [ '**/*.js' ], | |
| extends: [ 'eslint:recommended' ], | |
| rules: { | |
| 'brace-style': [ | |
| 'error', | |
| '1tbs', | |
| { | |
| allowSingleLine: true, | |
| }, | |
| ], | |
| // 2 space indent, no tabs | |
| indent: [ 'error', 2 ], | |
| }, | |
| }, | |
| { | |
| files: [ '**/*.ts' ], | |
| parser: '@typescript-eslint/parser', | |
| extends: [ | |
| 'eslint:recommended', | |
| 'plugin:@typescript-eslint/recommended', | |
| ], | |
| parserOptions: { | |
| ecmaVersion: 2018, | |
| sourceType: 'module', | |
| }, | |
| rules: { | |
| // Brace Style (one true brace style, with single line allowed) | |
| 'brace-style': 'off', | |
| '@typescript-eslint/brace-style': [ | |
| 'error', | |
| '1tbs', | |
| { | |
| allowSingleLine: true, | |
| }, | |
| ], | |
| // 2 space indent, no tabs | |
| indent: 'off', | |
| '@typescript-eslint/indent': [ 'error', 2 ], | |
| // I really LOVE turning off the misguided and poor tradition | |
| // of enforcing camelCase without suffixes. Even with camelCase | |
| // you need to disambiguate certain functions that represent | |
| // variations based on version. | |
| // | |
| // For example: "version 3.2" (camelCaseFunction3_2) vs | |
| // "version 32" (camelCaseFunction32); both would be the same | |
| // using camelCase and there would be no way to disambiguate | |
| // without great verbosity or difficulty. We still want to | |
| // disallow "non_camelCase3_2" and this rule does that as well. | |
| camelcase: 'off', | |
| '@typescript-eslint/camelcase': [ 'error', {allow: [ '(_[0-9]+)+$' ]} ], | |
| // turn off interface name prefix rule | |
| '@typescript-eslint/interface-name-prefix': 'off', | |
| // quotes - enforced as single where used to avoid escaping | |
| 'quotes': 'off', | |
| '@typescript-eslint/quotes': [ 'error', 'single', 'avoid-escape' ], | |
| // warn but allow for extra parentheses | |
| 'no-extra-parens': 'off', | |
| '@typescript-eslint/no-extra-parens': [ 'warn', 'all', {'conditionalAssign': false} ], | |
| // stylistically it is not all that bad to be verbose, unnecessary != wrong | |
| '@typescript-eslint/no-inferrable-types': 'off', | |
| // non null assertion should be disallowed, not just a warning | |
| '@typescript-eslint/no-non-null-assertion': 'error', | |
| }, | |
| }, | |
| ], | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| coverage | |
| dist | |
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Follow these directions: | |
| 1) From your project directory run "npm init" if you haven't already done so or don't have a package.json file. | |
| 2) From your project directory run the commands in "npm_installs.sh" or just run the file. | |
| 3) Add the ".eslintrc.js" file to your project directory. | |
| 4) Merge the fragments in "fragment_package.json" into your package.json file. | |
| 5) Include the contents of .gitignore in your .gitignore file. | |
| 6) Add the "tsconfig.json" file to your project directory or include the pertinent configurations. | |
| The following are the versions of modules installed in the reference project: | |
| "@types/jest": "^24.0.18", | |
| "@typescript-eslint/eslint-plugin": "^2.3.3", | |
| "@typescript-eslint/parser": "^2.3.3", | |
| "eslint": "^6.5.1", | |
| "eslint-plugin-jest": "^22.17.0", | |
| "jest": "^24.9.0", | |
| "ts-jest": "^24.1.0", | |
| "typescript": "^3.6.4", | |
| "@types/node": "^12.7.12" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| ... | |
| "scripts": { | |
| ... | |
| "clean": "rm -rf ./dist/*", | |
| "compile": "tsc", | |
| "build": "npm run clean && npm run compile", | |
| "test": "./node_modules/.bin/jest --coverage", | |
| "watch": "./node_modules/.bin/jest --watch --coverage", | |
| "lint": "./node_modules/.bin/eslint {src,test}/**/*{.ts,.tsx,.js}", | |
| "lint-fix": "./node_modules/.bin/eslint {src,test}/**/*{.ts,.tsx,.js} --fix" | |
| }, | |
| "jest": { | |
| "moduleFileExtensions": [ | |
| "ts", | |
| "tsx", | |
| "js" | |
| ], | |
| "transform": { | |
| ".(ts|tsx)": "ts-jest" | |
| }, | |
| "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| npm install --save-dev jest @types/jest ts-jest typescript | |
| npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-jest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
| { | |
| "compilerOptions": { | |
| "module": "commonjs", | |
| "outDir": "dist", | |
| "sourceMap": true, | |
| "target": "esnext", | |
| }, | |
| "include": [ | |
| "src/**/*.ts" | |
| ], | |
| "lib": [ | |
| "ES2018" | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment