Node Code Quality

GitbookCourses2021-01-01


💡 code quality

🪕


📑 Code Quality Fundamentals


  • comment conventions
  • whitespace conventions
  • naming
  • error controls
  • Linters
  • Unit, integration, and functional testing

📑 TDD (test-driven development) vs BDD (behavior)


  • TDD describes when, while BDD describes how and why.
  • teardown (after) 拆除
  • DSL: domain-specific language. An example of a DSL for testing framework interfaces is BDD
  • Assertions library (node.js built-in)

    typeOf = Object.prototype.toString
    instanceOf = instance of constructor
  • Assetion Library Examples | Assertion Library | Notes | | ----------------- | --------------------------- | | Assert | native, included in Node.js | | Chai | chaijs.com | | should.js | shouldjs.github.io | | code | github.com/hapijs/code |

📑 EditorConfig


  • used by: Node.js, React, webpack
  • Named .editorconfig
  • INI format ?
  • All properties and values are case insensitive

📑 Linters, ESLint


  • syntax checkeers and validators
  • node.js: built-in to node CLI: node —check script.js
  • 3 linters: JSLint, JSHint, ESLint
  • ESLint, extensible with plugins, most popular, most flexible, docs
  • EsLint Shareable Configs:

    shareable configs Notes
    eslint-config-airbnb React style Guide, default
    eslint-config-standard JS standard Style
    eslint-config-google Google Style
  • .eslintignore: ESLint can skip files and directories listed by globs.
  • multiple .eslintrc.json files in sub-dirs to overwrite: ESLint configurations can inherit from a parent and override locally with a inherit.
  • VSCode eslint plugins

📑 Unit Testing


  • framework: AVA, Jasmine, Jest(React support), Mocha, tape
  • library: Assert, Chai, code, should.js
  • Chai: BDD and TDD assertion library
  • Mocha + Chai
  • should not use a done callback when testing a Promise
  • chai-as-promised as optional with >Mocha 3
  • context() is just an alias for describe(), and behaves the same way
  • mocha default test folder is test/
  • The BDD interface provides describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach().
  • The TDD interface provides suite(), test(), suiteSetup(), suiteTeardown(), setup(), and teardown().

📑 Test Doubles (测试替代): Stubs (存根), Spies, Mocks


  • sinon.js: Sinon 通过所谓的测试替代(test-double) 轻松消除测试的复杂度。测试替代,顾名思义,测试中用到的是真实代码逻辑的替代品。
  • sinon-chai: extends Chai with Sinon.js TDD assertions, not required to use sinon.js
  • spies: 主要用途是收集有关函数调用的信息。一个 spies 可以告诉我们调用一个函数的次数、每次调用的参数、返回的值、抛出的错误等。
  • stubs: 拥有 spies 的所有功能,不是监视某个函数的调用情况,而是完全取代了这个函数。换句话说,当使用 spies 时,原始函数仍然运行,但是当使用 stub 时,函数将不具有原始的功能,而是替换后的函数。
  • mocks: 一个对象中的多个方法.

📑 Override dependencies with Proxyquire


  • Proxyquire: prevent calls to original dependency, simulates module absences, cache behavior lik reload. Use proxyquire on app.js
  • Mockery: replace dependency modules
  • Revire: Overrides and inspects variables

📑 Code Coverage istanbul


  • watch codes execution
  • track how well unit tests exeercise codebase
  • CLI interface is nyc
  • support mocha, AVA, and others
  • The four most typical types of code coverage are statements, branches, functions and lines.
  • the nyc tool can generate coverage reports in formats including text, html and LCOV.

📑 Function testing Tools


  • PhantomJS
  • Selenium WebDriver
  • SuperAgent: chai-http: uses a callback to provide an error and response.

    if('should rest work', (done) => {
      chai.request(app)
        .post('/rest')
        .set('content-type','application/x-www-form-urlencoded')
        .send({body: JSON.stringify(body)})
        .end(err => done(err))
    })

🪕 Coverage with continous integration


  • Jenkins
  • Strider CD: written in Node.js
  • continous integration as a Service: TravisCI

🪕 Summary


  • Quality: ESLint, Prettier, Airbnb, EditorConfig
  • Unit Testing: Mocha + Chai
  • Test Doubles: Sinonjs
  • Code coverage generated: Istanbul, nyc
  • Coverage with continuous integration: Jenkins, TravisCI