Successfully reported this slideshow.
Your SlideShare is downloading. ×

power-assert, mechanism and philosophy

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 29 Ad
Advertisement

More Related Content

Slideshows for you (20)

Advertisement

More from Takuto Wada (20)

Recently uploaded (20)

Advertisement

power-assert, mechanism and philosophy

  1. 1. power-assert mechanism and philosophy Takuto Wada (a.k.a @t_wada) Nov 15, 2014 @ Nodefest Tokyo 2014
  2. 2. Takuto Wada @t_wada github: twada
  3. 3. agenda •what is power-assert? •mechanism •design philosophy
  4. 4. state of JavaScript testing testing framework remote test runner environment assertion library mocking library plenty of alternatives...
  5. 5. 33 Methods!? too much to learn...
  6. 6. Something is wrong with arms race among assertion libraries... What if the `assert` is informative enough?
  7. 7. power-assert https://github.com/twada/power-assert
  8. 8. 1. npm install --save-dev power-assert intelli-espower-loader 2. add "directories" to package.json "directories": { "test": "test" }, 3. change var assert = require('assert'); to var assert = require('power-assert'); 4. mocha --require intelli-espower-loader Just 4 steps
  9. 9. assert(typeof item.id === 'strong') | | | | | | | false | | "foo" | Item{id:"foo",name:"bar"} "string" --- [string] 'strong' +++ [string] typeof item.id @@ -1,6 +1,6 @@ str -o +i ng You get this work unobtrusively, fail noisily
  10. 10. assert(actual) assert.deepEqual(actual,expected) assert.notDeepEqual(actual,expected) You don t need to memorize assertion APIs any more. Just write simple assert
  11. 11. arms race among assertion APIs enrich test vocabulary...resulted in overkill power-assert is a Renaissance of code No more assertion war
  12. 12. So, why don t you run with tests?
  13. 13. agenda •what is power-assert? •mechanism •design philosophy
  14. 14. var assert = require('power-assert'); function Item(id, name) { this.id = id; this.name = name; } describe('Item', function () { it('has string id', function () { var item = new Item(1000, 'bar'); assert(assert._expr(assert._capt(assert._capt(typeof assert._capt(assert._capt(item, 'arguments/0/left/argument/object').id, 'arguments/0/left/argument'), 'arguments/0/left') === 'string', 'arguments/0'), { content: 'assert(typeof item.id === 'string')', filepath: 'test/item_test.js', line: 11 })); }); }); var assert = require('power-assert'); function Item (id, name) { this.id = id; this.name = name; } describe('Item', function () { it('has string id', function () { var item = new Item(1000, 'bar'); assert(typeof item.id === 'string'); }); }); But How? test code transpiled to
  15. 15. https://speakerdeck.com/michaelficarra/spidermonkey-parser-api-a-standard-for-structured-js-representations new C(1 + a)
  16. 16. https://speakerdeck.com/constellation/escodegen-and-esmangle-using-mozilla-javascript-ast-as-an-ir
  17. 17. https://speakerdeck.com/constellation/escodegen-and-esmangle-using-mozilla-javascript-ast-as-an-ir
  18. 18. 3 essential AST tools •esprima •escodegen •estraverse
  19. 19. espoweresprima code AST escodegen AST AST AST code code code espower-source grunt-espower / gulp-espower / espowerify / espower-loader / espower-coffee / espower-cli
  20. 20. agenda •what is power-assert? •mechanism •design philosophy
  21. 21. The UNIX Philosophy https://github.com/substack/nodefest-2012
  22. 22. Make each program do one thing well https://github.com/substack/nodefest-2012
  23. 23. Rule of Composition: Design programs to be connected with other programs. Rule of Modularity: Write simple parts connected by clean interfaces.
  24. 24. substack pattern module.exports = function(arg) { // do one thing well };
  25. 25. First version (4) •power-assert •empower •espower •grunt-espower Currently (15) • power-assert • empower • power-assert-formatter • espower • espower-source • type-name • stringifier • espurify • escallmatch • grunt-espower • gulp-espower • espowerify • espower-loader • espower-coffee • espower-cli modularize
  26. 26. Your sensibilities about simplicity being equal to ease of use are wrong. Easy is not simple Simple: objective Easy: relative
  27. 27. Easy •power-assert •grunt-espower •gulp-espower •espowerify •espower-loader •espower-cli •espower-coffee Simple •espower •espower-source •empower separate `simple` from `easy`
  28. 28. wrap-up •No more assertion war •Use JS to modify JS •Separate simple from easy •Make things small and composable
  29. 29. power-assert https://github.com/twada/power-assert Thank you!

×