Skip to content

Instantly share code, notes, and snippets.

@unclebean
Last active January 28, 2019 14:52
Show Gist options
  • Save unclebean/fcdde8a89d2d8a092a4cd8703c708543 to your computer and use it in GitHub Desktop.
Save unclebean/fcdde8a89d2d8a092a4cd8703c708543 to your computer and use it in GitHub Desktop.
config for node js

Setup nodejs project

    # initial project
    npm init
    # install unit test dependencies, -D means --save-dev
    npm install -D mocha chai sinon chai-http nyc
    # integrate eslint & prettier & airbnb style guide
    npm install -D eslint prettier
    # peerdeps means to install corresponding airbnb version for eslint
    npx install-peerdeps --dev eslint-config-airbnb
    # install prettier eslint integration tools
    npm install -D eslint-config-prettier eslint-plugin-prettier

Setup eslint & prettier config

    # create .eslintrc.json
    {
        "env": {
            "node": true,
            "mocha": true
        },
        "extends": ["airbnb", "prettier"],
        "plugins": ["prettier"],
        "rules": {
            "prettier/prettier": ["error"]
        }
    }
    # create .prettierrc
    {
        "printWidth": 100,
        "singleQuote": true
    }

Add eslint task to package.json

    "scripts": {
        "eslint": "eslint src/*"
    }

Add nyc config to package.json

    "nyc": {
        "all": true,
        "check-coverage": false,
        "per-file": true,
        "include": [
            "src/**/*.js"
        ],
        "exclude": [
            "test/**/*.test.js"
        ],
        "reporter": [
            "lcov"
        ]
    }

Create unit test

    "scripts": {
        "test": "nyc mocha"
    }

    npm test

Make an API call

    # install node-fetch
    npm install node-fetch --save
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"'${workspaceFolder}/{,!(node_modules)/}*/*.test.js'"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/nodejs/for-sharing/common-lib/node_modules/.bin/_mocha",
"args": [
"--timeout",
"999999",
"--colors",
"${file}"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
{
"workbench.colorTheme": "Palenight Theme",
"workbench.iconTheme": "vscode-icons",
"vsicons.projectDetection.autoReload": true,
"vsicons.dontShowNewVersionMessage": true,
"vim.disableAnnoyingNeovimMessage": true,
"java.errors.incompleteClasspath.severity": "ignore",
"typescript.check.tscVersion": false,
"team.showWelcomeMessage": false,
"javascript.updateImportsOnFileMove.enabled": "always",
"python.formatting.autopep8Path": "black",
"python.formatting.provider": "black",
"python.unitTest.unittestEnabled": true,
"python.unitTest.pyTestEnabled": true,
"python.unitTest.nosetestsEnabled": false,
"workbench.startupEditor": "newUntitledFile",
"window.zoomLevel": 0,
"prettier.eslintIntegration": true,
"vim.autoindent": true,
"prettier.bracketSpacing": false,
"prettier.tslintIntegration": true,
"editor.formatOnSave": true,
"files.insertFinalNewline": true,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment