Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kung Fu Moves for Front-End Hero

Kung Fu Moves for Front-End Hero

Presented at NationJS, Dec 1 2017 in Washington, DC.

Ariya Hidayat

December 01, 2017
Tweet

More Decks by Ariya Hidayat

Other Decks in Programming

Transcript

  1. Kung Fu Moves for Front-End Hero

    View Slide

  2. View Slide

  3. What shall I do next?

    View Slide

  4. T D D B D D

    View Slide

  5. T D D B D D
    P D D

    View Slide

  6. You either die a JavaScript hero, or you live long
    enough to read those JavaScript fatigue articles.

    View Slide

  7. Sample Project

    View Slide

  8. Test Runner
    Test Library Assertion Library
    Style Checker
    github.com/ariya/from-zero-to-hero

    View Slide

  9. > npm start
    > [email protected] start /home/ariya/from-hero-to-zero
    > http-server -p 9000
    Starting up http-server, serving ./
    Available on:
    http://127.0.0.1:9000
    http://192.168.1.69:9000
    Hit CTRL-C to stop the server

    View Slide

  10. Prevent regressions with
    Git hooks

    View Slide

  11. git commit
    Run pre-commit hook
    Exit
    code
    = 0
    ?
    Continue
    Abort the
    commit process
    Y
    N

    View Slide

  12. git push
    Run pre-push hook
    Exit
    code
    = 0
    ?
    Continue
    Abort the
    push process
    Y
    N

    View Slide

  13. cat << EOF >> .git/hooks/pre-commit
    npm run jscs
    EOF
    Create a pre-commit hook script
    chmod +x .git/hooks/pre-commit
    Ensure that it is executable

    View Slide

  14. > git commit -a -m "Fix bug #666"

    View Slide

  15. > git commit -a -m "Fix bug #666"
    > [email protected] jscs /home/ariya/from-hero-to-zero
    > jscs -p idiomatic js/*.js
    Invalid quote mark found at js/todoModel.js :
    1 |var app = app || {};
    2 |(function() {
    3 | 'use strict';
    ------------^
    4 | var Utils = app.Utils;
    5 | // Generic "model" object. You can use whatever
    1 code style error found.

    View Slide

  16. Manage
    code complexity

    View Slide

  17. View Slide

  18. View Slide

  19. http://ariya.ofilabs.com/2013/05/continuous-monitoring-of-javascript-code-complexity.html

    View Slide

  20. > cr -f plain search.js

    View Slide

  21. > cr -f plain search.js
    search.js
    Function: searchConsultant
    Line No.: 1
    Physical LOC: 17
    Logical LOC: 13
    Parameter count: 0
    Cyclomatic complexity: 6
    Cyclomatic complexity density: 46.15384615384615%
    Halstead difficulty: 15.235294117647058
    Halstead volume: 371.5647232790157
    Halstead effort: 5660.897842897945

    View Slide

  22. Monitor and track
    code coverage

    View Slide

  23. Istanbul
    Instrumenter + Coverage analysis

    View Slide

  24. Source
    Instrumented Source

    View Slide

  25. Source
    Instrumented Source
    Tests

    View Slide

  26. Source
    Instrumented Source
    Tests
    Coverage Report

    View Slide

  27. Karma configuration
    module.exports = function(config) {
    config.set({
    basePath: '..',
    autoWatch: true,
    frameworks: ['mocha', 'chai'],
    files: ['js/*.js', 'test/*.js' ],
    plugins: [ 'karma-mocha', 'karma-chai', 'karma-coverage',
    'karma-phantomjs-launcher'],
    browsers: ['PhantomJS'],
    singleRun: true,
    preprocessors: { 'js/*.js': ['coverage'] },
    reporters: ['progress', 'coverage'],
    coverageReporter: {
    dir : 'coverage/',
    reporters: [{ type: 'html', subdir: 'html' },]
    }
    });
    };

    View Slide

  28. Coverage Report

    View Slide

  29. Coverage Report
    Statistics
    Uncovered
    Statements

    View Slide

  30. Code Coverage Dashboard

    View Slide

  31. Repository

    View Slide

  32. Repository
    Build system

    View Slide

  33. Repository
    Build system
    Cobertura
    Report

    View Slide

  34. Repository
    Build system
    Dashboard
    Cobertura
    Report

    View Slide

  35. Coverage Tracking in Pull Request
    http://ariya.ofilabs.com/2015/08/javascript-code-coverage-dashboard-with-codecov-io.html

    View Slide

  36. “If you think JSLint hurts
    your feelings, wait until you
    use Istanbul.”
    @davglass

    View Slide

  37. ...I gave you my heart
    But the very next day you gave it away...

    View Slide

  38. Run quick smoke tests with
    evergreen browsers

    View Slide

  39. http://ariya.ofilabs.com/2015/09/javascript-testing-with-latest-firefox-and-chrome-on-appveyor.html

    View Slide

  40. appveyor.yml
    install:
    - choco install firefox
    - choco upgrade firefox
    - choco install googlechrome
    - choco upgrade googlechrome
    - ps: Install-Product node $env:nodejs_version
    - node --version
    - npm --version
    - npm install
    test_script:
    - npm test

    View Slide

  41. https://chocolatey.org/packages/Firefox

    View Slide

  42. Running Install scripts
    choco install firefox
    Installing the following packages:
    firefox
    By installing you accept licenses for the packages.
    Firefox v41.0.2 already installed.
    Use --force to reinstall, specify a version to install, or try upgrade.
    Chocolatey installed 0/1 package(s). 0 package(s) failed.

    View Slide

  43. Running Install scripts
    choco install firefox
    Installing the following packages:
    firefox
    By installing you accept licenses for the packages.
    Firefox v41.0.2 already installed.
    Use --force to reinstall, specify a version to install, or try upgrade.
    Chocolatey installed 0/1 package(s). 0 package(s) failed.
    choco upgrade firefox
    Upgrading the following packages:
    firefox
    By upgrading you accept licenses for the packages.
    You have Firefox v41.0.2 installed. Version 44.0.2 is available based on
    your source(s).
    Firefox v44.0.2
    Downloading Firefox 32 bit
    Installing Firefox...
    Firefox has been installed.
    The upgrade of firefox was successful.

    View Slide

  44. Test your
    downstream projects

    View Slide

  45. Google V8
    Node.js
    Express

    View Slide

  46. Library
    Framework
    Application

    View Slide

  47. function test_downstream_project(project, repo) {
    console.log('Cloning', repo, '...');
    execute('git clone --depth 1 ' + repo + ' ' + project);
    process.chdir(project);
    console.log();
    execute('npm install');
    console.log();
    console.log('Replacing awesome with a fresh one...');
    copy_file(source, './node_modules/awesome/awesome.js');
    console.log();
    try {
    execute('npm test');
    } catch (e) {
    console.log('Failing: ', e.toString());
    process.exit(1);
    }
    process.chdir('..');
    }

    View Slide

  48. function test_downstream_project(project, repo) {
    console.log('Cloning', repo, '...');
    execute('git clone --depth 1 ' + repo + ' ' + project);
    process.chdir(project);
    console.log();
    execute('npm install');
    console.log();
    console.log('Replacing awesome with a fresh one...');
    copy_file(source, './node_modules/awesome/awesome.js');
    console.log();
    try {
    execute('npm test');
    } catch (e) {
    console.log('Failing: ', e.toString());
    process.exit(1);
    }
    process.chdir('..');
    }
    Clone a downstream project
    Replace the library with
    a fresh one
    Install dependencies
    Run the test suite

    View Slide

  49. Run different tests in parallel

    View Slide

  50. Verify code coverage

    View Slide

  51. Verify code coverage
    Run browser tests

    View Slide

  52. Verify code coverage
    Run browser tests
    Check downstream projects

    View Slide

  53. Parallel Execution
    Verify code coverage
    Monitor complexity
    Run browser tests
    Check downstream projects

    View Slide

  54. Parallel Execution
    Verify code coverage
    Monitor complexity
    Run browser tests
    Check downstream projects

    View Slide

  55. This is the hero you’re looking for

    View Slide

  56. Thank You
    Some artworks are from http://openclipart.org.
    @ariyahidayat
    shapesecurity.com
    speakerdeck.com/ariya

    View Slide