node.js development workflow automation with grunt.js

50
Node.Js Development Workow Automation With Grunt.Js Nadeem Shabir Hydrahack Birmingham 18th March 2014

Upload: kiyanwang

Post on 11-May-2015

1.521 views

Category:

Technology


1 download

DESCRIPTION

Slides for presentation I have at Hydrahack in Birmingham on 18th March 2014. http://hydrahack.co.uk/ http://talis.com

TRANSCRIPT

Page 1: Node.js Development Workflow Automation with Grunt.js

Node.Js Development Workflow Automation With Grunt.Js

Nadeem ShabirHydrahack Birmingham18th March 2014

Page 2: Node.js Development Workflow Automation with Grunt.js

~ Paul Irish

“Your job as a developer isn’t to just develop, it’s to continually learn how to develop

better.”

Page 3: Node.js Development Workflow Automation with Grunt.js

Automating common tasks• code quality (jshint)

• beautifying (jsbeautifier)

• live reload (watch)

• Automated Testing

Page 4: Node.js Development Workflow Automation with Grunt.js

Grunt.jshttp://gruntjs.com

Page 5: Node.js Development Workflow Automation with Grunt.js

http://gulpjs.com

Page 6: Node.js Development Workflow Automation with Grunt.js

– Shaun Dunne ( https://medium.com/fear-and-coding )

“If you’re doing automation, you’re already doing something right. It’s not about how you

do it”

Page 7: Node.js Development Workflow Automation with Grunt.js

Grunt.js• Javascript based task runner

• automate repetitive tasks

• large ecosystem of plugins (2500+)

• easy to maintain

• simple to learn and use

Page 8: Node.js Development Workflow Automation with Grunt.js

ANT

GRUNT

Page 9: Node.js Development Workflow Automation with Grunt.js

Install

npm install -g grunt-cli cd <your_project_dir> npm install grunt --save-dev npm install grunt-contrib-jshint grunt-contrib-watch \ grunt-mocha-test grunt-jsbeautifier --save-dev

Page 10: Node.js Development Workflow Automation with Grunt.js

gruntfile.js

Page 11: Node.js Development Workflow Automation with Grunt.js

watch• runs predefined tasks when files change

• use patterns to indicate which files to watch

Page 12: Node.js Development Workflow Automation with Grunt.js

jshint• static code analysis tool

• detect errors & potential problems

• enforce team coding conventions

• every time we change a .js file

Page 13: Node.js Development Workflow Automation with Grunt.js

jsbeautify• reformats Javascript source code

• enforces code formatting conventions

• easier to read and understand

• every time we change a .js file

Page 14: Node.js Development Workflow Automation with Grunt.js
Page 15: Node.js Development Workflow Automation with Grunt.js
Page 16: Node.js Development Workflow Automation with Grunt.js
Page 17: Node.js Development Workflow Automation with Grunt.js

live reload• monitors changes in files

• preprocesses files as needed

• browser automatically reloads and shows your changes

• built into grunt-contrib-watch

Page 18: Node.js Development Workflow Automation with Grunt.js
Page 19: Node.js Development Workflow Automation with Grunt.js
Page 20: Node.js Development Workflow Automation with Grunt.js

Automated Testing

Page 21: Node.js Development Workflow Automation with Grunt.js

!~ Gerald Weinberg

Weinberg’s Second Law: !

“If builders built buildings the way programmers wrote programs, the first wood-

pecker that came along would destroy civilization”

Page 22: Node.js Development Workflow Automation with Grunt.js
Page 23: Node.js Development Workflow Automation with Grunt.js

Test Driven Development

Page 24: Node.js Development Workflow Automation with Grunt.js

BDD vs TDD• The gotcha with TDD is that too many developers focused on

the "How" when writing their unit tests, so they ended up with very brittle tests that did nothing more than confirm that the system does what it does.

• BDD provides a new vocabulary and thus focus for writing a unit test. Basically it is a feature driven approach to TDD.

Page 25: Node.js Development Workflow Automation with Grunt.js

~ Matt Wynne http://blog.mattwynne.net/2012/11/20/tdd-vs-bdd/

“BDD builds upon TDD by formalising the good habits of the best TDD practitioners”

Page 26: Node.js Development Workflow Automation with Grunt.js

Tools• mocha - (http://visionmedia.github.io/mocha/) BDD, TDD, QUnit

styles via interfaces

• should.js - (https://github.com/visionmedia/should.js/) expressive, readable, test framework agnostic, assertion library

• supertest - (https://github.com/visionmedia/supertest) Super-agent driven library for testing HTTP servers. E2E for apis.

• Protractor - (https://github.com/angular/protractor) E2E test framework for Angular apps

Page 27: Node.js Development Workflow Automation with Grunt.js

… caveat

• This is based on my personal experience and opinion - YMMV

• I’ve been known to change my mind ;-)

• … BUT these tools and techniques are being used on a lot of projects successfully.

Page 28: Node.js Development Workflow Automation with Grunt.js

Mocha• vs Jasmine - Jasmine is easier to get started –

great all-in-one test framework.

• Mocha is more flexible: you have to piece it together yourself. i.e. add an assertion framework, add a spy/mocking framework. Supports different styles of testing, lots of reporters (spec, xunit, nyan ;-) ) & disable tests

Page 29: Node.js Development Workflow Automation with Grunt.js

describe(“my test suite”, function(){!!! it(“should test something”, function(done){!! ! // do some assertions here!! ! // when your test completes call !! ! done();!! !! });!!! xit(“this test is skipped”, function(done){!! ! done();!! !! });!!});

Mocha - BDD

Page 30: Node.js Development Workflow Automation with Grunt.js

should.js• nice assertion library. There are lots of others

for example chai which is also very good.

• chainable language to construct assertions

• its api provides lots of assertion methods

• very easy to read

Page 31: Node.js Development Workflow Automation with Grunt.js

it(“should test something useful”, function(done){!! !! var colors = [‘red’, ‘white’, ‘blue’];!!! colors.should.include(‘red’);!! colors.should.not.include(‘yellow’);!! colors.length.should.equal(3);!! colors.should.be.an.instanceof(Array);!! !! done();!! !});!

Page 32: Node.js Development Workflow Automation with Grunt.js

calculator_test.js

models/calc.js

Page 33: Node.js Development Workflow Automation with Grunt.js
Page 34: Node.js Development Workflow Automation with Grunt.js
Page 35: Node.js Development Workflow Automation with Grunt.js

supertest• great way to test running http apis,

• provides assertions and expectations for http

• when combined with async, makes it easy to test a series of api calls

• easy to use and makes tests easy to read!

Page 36: Node.js Development Workflow Automation with Grunt.js

var request = require(“supertest”);!!it(“should return some json”, function(done){!! request(“http://localhost:3000”)! ! .get('/isbns/978000000000X/lists')! !.set('Accept', 'application/json')! !.expect('Content-Type', /json/)! !.expect(200)! !.end(function(err, res) {! !! if(err) return done(err);! !! ! ! res.body.should.xxxxx!! !! done();!! ! });! !});!

Page 37: Node.js Development Workflow Automation with Grunt.js
Page 38: Node.js Development Workflow Automation with Grunt.js

Protractor• Browser testing for Angular.js apps, built on

Webdriver.js

• uses selenium-webdriver, tests written in WebDriver API, communicates with Selenium to control browsers

• you can run tests locally using Selenium-Standalone

• you can run tests against any service that exposes Selenium Hub endpoint ( BrowserStack, SauceLabs)

Page 39: Node.js Development Workflow Automation with Grunt.js

Protractor

Page 40: Node.js Development Workflow Automation with Grunt.js

it('should greet the named user', function() {! browser.get('http://www.angularjs.org');! ! // find the element with ng-model matching 'yourName'! element(by.model('yourName')).sendKeys('Nadeem');! ! // find the <h1>Hello {{yourName}}!</h1> element.! var greeting = element(by.binding('yourName'));!! expect(greeting.getText()).toEqual('Hello Nadeem!');!});

Page 41: Node.js Development Workflow Automation with Grunt.js

chromeConf.js

Page 42: Node.js Development Workflow Automation with Grunt.js

Demo

Page 43: Node.js Development Workflow Automation with Grunt.js

E2E testing pitfalls• Use Selenium Standalone on local dev machines

• Use Continuous Integration to test range of browsers on different OS. Nightly builds etc.

• Use BrowserStack / SauceLabs / Other for CI, unless you have time/£ to set up you’re own infrastructure.

• Tunnelling to test local / internal deployments from those services can be slow / unreliable; avoid if possible …

Page 44: Node.js Development Workflow Automation with Grunt.js
Page 45: Node.js Development Workflow Automation with Grunt.js
Page 46: Node.js Development Workflow Automation with Grunt.js

github.com/kiyanwang/js-workflow-automation

Page 47: Node.js Development Workflow Automation with Grunt.js

engineering.talis.com

Page 48: Node.js Development Workflow Automation with Grunt.js

[email protected] @kiyanwang

github/kiyanwang

Page 49: Node.js Development Workflow Automation with Grunt.js

We are hiring!

http://www.talis.com/jobs

Page 50: Node.js Development Workflow Automation with Grunt.js

@talis facebook.com/talisgroup@talis facebook.com/talisgroup

+44 (0) 121 374 2740 !talis.com [email protected] !48 Frederick Street Birmingham B1 3HN