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

Hugs.js

lawrencec
September 11, 2016

 Hugs.js

A deck describing the what, why and how of Hugs.js.

lawrencec

September 11, 2016
Tweet

Other Decks in Programming

Transcript

  1. HUGS.JS/WHAT-IS-HUGS.JS WHAT IS HUGS.JS? ▸ It is not a conference

    or charity for frustrated javascript developers.
  2. HUGS.JS/WHAT-IS-HUGS.JS WHAT IS HUGS.JS? ▸ It is not a conference

    or charity for frustrated javascript developers. (Though those should **totally** exist already!)
  3. HUGS.JS/WHAT-IS-HUGS.JS WHAT IS HUGS.JS? ▸ It is a wrapper (geddit?)

    around common test runners, mocking, and assertion libraries to facilitate more consistent writing of tests across projects.
  4. HUGS.JS/WHAT-IS-HUGS.JS WHAT IS HUGS.JS? ▸ It is a wrapper (geddit?)

    around common test runners, mocking, and assertion libraries to facilitate more consistent writing of tests across projects. ▸ It allows tests to be written in the same way regardless of choice of test runner.
  5. HUGS.JS/WHAT-IS-HUGS.JS WHAT IS HUGS.JS? ▸ It is a wrapper (geddit?)

    around common test runners, mocking, and assertion libraries to facilitate more consistent writing of tests across projects. ▸ It allows tests to be written in the same way regardless of choice of test runner. ▸ Tongue-in-cheek project (if you haven’t guessed it already) that has the potential to be quite useful.
  6. HUGS.JS/WHAT-IS-HUGS.JS/SUPPORTS/TEST-RUNNERS WHAT DOES IT SUPPORT? ▸ Mocha ▸ Tap ▸

    Ava (currently, serial mode only) TEST RUNNERS: IN NODE-LAND:
  7. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to
  8. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner:
  9. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner: ▸ “Wait, which project is this? The one with Jasmine or Mocha?”
  10. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner: ▸ “Wait, which project is this? The one with Jasmine or Mocha?” for a second
  11. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner: ▸ “Wait, which project is this? The one with Jasmine or Mocha?” for a second ▸ “Mocha”
  12. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner: ▸ “Wait, which project is this? The one with Jasmine or Mocha?” for a second ▸ “Mocha” for three seconds
  13. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner: ▸ “Wait, which project is this? The one with Jasmine or Mocha?” for a second ▸ “Mocha” for three seconds ▸ “Ok, so that’s the should / better-assert / expect / unexpected / chai expect / chai assert / chai should / chai-sinon/ must assert syntax, right?
  14. HUGS.JS/WHY? BUT WHY?! ▸ 8 times out of 10 every

    project a developer might contribute to will have tests written in a slightly different manner to the last project they contributed to ▸ Which leads to the following conversation with yourself or pair partner: ▸ “Wait, which project is this? The one with Jasmine or Mocha?” for a second ▸ “Mocha” for three seconds ▸ “Ok, so that’s the should / better-assert / expect / unexpected / chai expect / chai assert / chai should / chai-sinon/ must assert syntax, right? ▸ “Erm, I can’t remember. Just copy and paste from the existing tests”
  15. HUGS.JS/WHY? BUT WHY?! ▸ Updating an older personal project that

    uses an assertion syntax I’ve long forgotten is a PITA
  16. HUGS.JS/WHY? BUT WHY?! ▸ Updating an older personal project that

    uses an assertion syntax I’ve long forgotten is a PITA ▸ It’s an exercise in reading documentation and not writing tests.
  17. HUGS.JS/WHY? BUT WHY?! ▸ Updating an older personal project that

    uses an assertion syntax I’ve long forgotten is a PITA ▸ It’s an exercise in reading documentation and not writing tests. ▸ The tests I write now, using Hugs, will very likely be the same syntax in tests I write in 2 years time.
  18. HUGS.JS/WHY? WITHOUT HUGS.JS, A TEST WOULD LOOK LIKE ▸ Tap

    test: var test = require(‘tape'); var lib = require(‘..'); test(‘Array.indexOf()’,function (assert) { assert.plan(1); assert.equal([1,2,3].indexOf(4), -1, ’should return -1 when the value is not present’); }); var assert = require(‘assert'); describe('Array', function() { describe('#indexOf()', function() { it('should return -1 when the value is not present', function() { assert.equal(-1, [1,2,3].indexOf(4)); }); }); }); ▸ Mocha or Jasmine test:
  19. HUGS.JS/WHY? WITHOUT HUGS.JS, SPYING WOULD LOOK LIKE ▸ Jasmine Spies

    Jasmine.spyOn(object, ‘method’); const sandbox = sinon.sandbox(); const spy = sandbox.spy(object, 'method'); ▸ Sinon spies
  20. HUGS.JS/WHY? WITHOUT HUGS.JS, ASSERTIONS WOULD LOOK LIKE ▸ 37 slides

    later… ▸ There’s just too many variants available.
  21. HUGS.JS/WHY? HUGS.JS IS OPINIONATED ▸ One mocking library (Sinon). ▸

    One assert syntax. (Sinon and Chai asserts) ▸ A choice of test runners to allow for user preference. For example to choose on the basis of better tests reports or test speed.
  22. HUGS.JS/WHY? HUGS.JS IS OPINIONATED ▸ One mocking library (Sinon). ▸

    One assert syntax. (Sinon and Chai asserts) ▸ A choice of test runners to allow for user preference. For example to choose on the basis of better tests reports or test speed. ▸ TDD not BDD i.e test() functions not describe() functions
  23. HUGS.JS/WHY? HUGS.JS IS OPINIONATED ▸ One mocking library (Sinon). ▸

    One assert syntax. (Sinon and Chai asserts) ▸ A choice of test runners to allow for user preference. For example to choose on the basis of better tests reports or test speed. ▸ TDD not BDD i.e test() functions not describe() functions ▸ No nested test() functions. Nested test() functions are fashionable versions of callback pyramids. Flipflops vs sandals.
  24. HUGS.JS/HOW? HOW TO USE IT? // tests/test.js const hugs =

    require('hugs'); // This example uses Mocha but the require call can be changed to 'tap' // and without any other changes, the tests will work const test = hugs(require('mocha')); const spy = test.spy; const assert = test.assert; let oUT = { method: () => { } }; test( 'a test with a spy', function (done) { // Note: no need to explicitly create a sandbox for spying. Sandbox creation is automatically taken care of. spy(oUT, 'method'); oUT.method(1, 2); assert.callCount(oUT.method, 1); assert.calledWith(oUT.method, 1, 2); done(); } ); > npm install hugs > mocha --recursive --ui tdd test/**/*.js
  25. HUGS.JS/API HUGS.JS API ▸ afterEach - Lifecycle method to register

    functions to execute after each test function ▸ assert - assertions object. Exposes Sinon and Chai assertions e.g test.assert.callCount(foo, 1)) ▸ beforeEach - Lifecycle method to register functions to execute before each test function ▸ chai - Chai object. ▸ createStubInstance - Sinon method for stubbing out constructors. ▸ done - ends the test for non Promise tests. Can be passed arguments of the test callback or a done argument if specified in the test signature. ▸ match - Sinon’s matcher api ▸ mock - Sinon mock (sandboxed for each test) ▸ only (Mocha) - Runs a single test test.only('a test', function(t){}); ▸ spy - Sinon spy (sandboxed for each test) ▸ stub - Sinon stub (sandboxed for each test) ▸ test - test method The test object returned from a hugs() call has the following API:
  26. HUGS.JS/DEMO OK, DEMO TIME ▸ Taken from Hugs.js examples: >

    git clone [email protected]:lawrencec/hugs.git > cd hugs > npm install > cd hugs/examples > npm install > npm run help NPM - ls scripts --- example:ava - Run ava example example:browser - Run browser examples using jasmine and mocha example:browser:debug - Run browser example in karma debug mode. use HUGS_TEST_LIB=mocha| jasmine to specify test lib to use example:browser:dev - Run browser example in karma dev mode. use HUGS_TEST_LIB=mocha| jasmine to specify test lib to use example:browser:jasmine - Run browser examples using jasmine example:browser:mocha - Run browser examples using mocha example:mocha - Run mocha example example:tap - Run tap example examples - npm run example:ava; npm run example:mocha; npm run example:tap; help - List available npm run commands --- > npm run <select from above list>