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

Modern Javascript

Daniel Knell
September 04, 2013

Modern Javascript

A look at some of the processes and tools of the trade employed by modern day javascript developers, for performance, testing, and application development.

Daniel Knell

September 04, 2013
Tweet

More Decks by Daniel Knell

Other Decks in Technology

Transcript

  1. Stone Age var element ; if (document.all) { element =

    document.all[id]; else { element = document.getElementById(id); }
  2. Bronze Age function getElement(id) { if (document.all) { return document.all[id];

    } else { return document.getElementById(id); } } var element = getElement(id);
  3. BDD

  4. Gherkin Spec Feature: Homepage In order be the best site

    on the web As a webmaster I want to have a homepage Scenario: View Homepage Given I am on the Homepage When I click "Greet!" Then I should see "Hello World!"
  5. Step Definition Given /^I am on the home page$/ do

    visit "/" end When /^I click on "([^"]*)"$/ do |text| click_on text end Then /^I should see "([^"]*)"$/ do |text| page.should have_content text end
  6. Gherkin Spec Feature: Brainfuck Support In order to turn peoples

    brain to mush As a troll I want to write programs in brainfuck Scenario: Hello World Given I enter the code: """ >+++++++++[<++++++++>-]<. >+++++++[<++++>-]<+. +++++++..+++. >>>++++++++[<++++>-]<. >>>++++++++++[<+++++++++>-]<---. <<<<.+++.------.--------.>>+. """ When I run the program Then I should see "Hello World!" as the result
  7. Step Definition var brainfuck = require(‘brainfuck’); module.exports = function ()

    { this.Given('I enter the code:', function(code, callback) { this.code = code; callback(); }); this.When('I run the program', function(callback) { this.result = brainfuck(this.code); callback(); }); this.Then('I should see "$result" as the result', function(result, callback) { if (this.result !== result) { callback.fail(new Error('Expected "'+result+'" but received "'+this.result+'"')); } else { callback(); } }); };
  8. It Should... describe(“Parser”, function () { describe(“#parse()”, function () {

    it(“should compile simple paths”, function () { }); }); });
  9. In Full... var delegator = require(“../lib/index”) , assert = require(“assert”)

    ; describe(“Parser”, function () { describe(“#parse()”, function () { it(“should compile simple paths”, function () { var instance = new delegator.parser.SimpleParser() , result = instance.parse(“/hello/world”) , regexp = new RegExp(“^/hello/world$”) ; assert.deepEqual(result.expr, regexp); assert.deepEqual(result.keys, []); }); }); });
  10. Common Setup var delegator = require(“../lib/index”) , assert = require(“assert”)

    ; describe(“Parser”, function () { describe(“#parse()”, function () { var instance ; beforeEach(function () { instance = new delegator.parser.SimpleParser(); }); it(“should compile simple paths”, function () { var result = instance.parse(“/hello/world”) , regexp = new RegExp(“^/hello/world$”) ; assert.deepEqual(result.expr, regexp); assert.deepEqual(result.keys, []); }); });
  11. Spies var sinon = require(“sinon”) , callback = sinon.spy() ;

    doSomething(callback); sinon.assert.called(callback);
  12. Stubs var sinon = require(“sinon”) , callback = sinon.stub() ;

    callback.withArgs(“foo”).returns(“bar”); doSomething(callback); sinon.assert.called(callback);
  13. Mocks var sinon = require(“sinon”) , foo = new String(“bar”)

    , mock = sinon.mock(foo) ; mock.expects(“toUpperCase”).once().returns(“FOO”); doSomething(foo); mock.verify();
  14. Mockery var mockery = require(“mockery”) , mock = { readFileSync:

    function () { return “foobar”; } } , foo ; mockery.registerAllowables([“../lib/foo”], true); mockery.registerMock(“fs”, mock); foo = require(“../lib/foo”); foo.loadFromFile(“/var/foo”); mockery.disable(); mockery.deregisterAll();
  15. Browser Based describe(“Widget”, function () { var Widget ; beforeEach(function

    (done) { require(["widget"], function(_Widget) { Widget = _Widget done(); }); }); describe(“#errors()”, function () { it(“should return empty array for new widgets”, function () { var widget = new Widget() ; assert.deepEqual([], widget.errors()) }); }); });
  16. Make rocks REPORTER = dot MOCHA = ./node_modules/mocha/bin/mocha JSCOVER =

    ./node_modules/jscover/bin/jscover JSHINT = ./node_modules/jshint/bin/hint test: ! @ NODE_ENV=test $(MOCHA) --reporter $(REPORTER) test-watch: ! @ NODE_ENV=test $(MOCHA) --reporter min --growl --watch coverage: ! @ echo ' generating coverage.html' ! @ $(JSCOVER) lib lib-cov ! @ DELEGATOR_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html ! @ rm -rf lib-cov lint: ! @ $(JSHINT) $(shell find {lib,test} -name "*.js") .PHONY: test test-watch coverage lint
  17. Spinx ======= Parsers ======= This module implements the parsers used

    to convert string path expressions into regular expressions used in the matching stage of request delegation. Simple Parser ============= .. class:: delegator.parser.SimpleParser(patterns) Parser to turn path expressions into regular expressions with a list of group names. .. function:: parser.parse(pattern) Parse a string into a regular expression :param pattern string: the pattern string to parse :throws delegator.errors.ParserError: For invalid patterns. :returns: A dictionary containing the regular expression and an array of keys