Share
Embed
Transcript
38 slides
-
HUGS.JS/WHAT-IS-HUGS.JS WHAT IS HUGS.JS? ▸ It is not a conference
or charity for frustrated javascript developers. -
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!) -
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. -
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. -
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. -
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: -
HUGS.JS/WHAT-IS-HUGS.JS/SUPPORTS/TEST-RUNNERS WHAT DOES IT SUPPORT? ▸ Mocha ▸ Jasmine TEST
RUNNERS: IN BROWSER-LAND: -
HUGS.JS/WHAT-IS-HUGS.JS/SUPPORTS/JS-LOADERS WHAT DOES IT SUPPORT? ▸ CommonJS/Browserify ▸ RequireJS ▸
Standard in-page browser scripts JS-LOADERS: -
HUGS.JS/WHAT-IS-HUGS.JS/SUPPORTS/ASSERTION-AND-MOCKING LIBRARIES WHAT DOES IT SUPPORT? ▸ Chai Assert ▸
Sinon ASSERTION & MOCKING LIBRARIES: -
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 -
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: -
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?” -
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 -
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” -
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 -
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? -
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” -
HUGS.JS/WHY? BUT WHY?! ▸ Updating an older personal project that
uses an assertion syntax I’ve long forgotten is a PITA -
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. -
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. -
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: -
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 -
HUGS.JS/WHY? WITHOUT HUGS.JS, ASSERTIONS WOULD LOOK LIKE ▸ 37 slides
later… ▸ There’s just too many variants available. -
HUGS.JS/WHY? HUGS.JS IS OPINIONATED ▸ One mocking library (Sinon). ▸
One assert syntax. (Sinon and Chai asserts) -
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. -
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 -
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. -
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 -
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: -
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>