SlideShare a Scribd company logo
Node.Js Development Workflow Automation
With Grunt.Js
Nadeem Shabir
Hydrahack Birmingham
18th March 2014
~ Paul Irish
“Your job as a developer isn’t to just develop,
it’s to continually learn how to develop
better.”
Automating common tasks
• code quality (jshint)
• beautifying (jsbeautifier)
• live reload (watch)
• Automated Testing
Grunt.js
http://gruntjs.com
http://gulpjs.com
– 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”
Grunt.js
• Javascript based task runner
• automate repetitive tasks
• large ecosystem of plugins (2500+)
• easy to maintain
• simple to learn and use
ANT
GRUNT
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
gruntfile.js
watch
• runs predefined tasks when files change
• use patterns to indicate which files to watch
jshint
• static code analysis tool
• detect errors & potential problems
• enforce team coding conventions
• every time we change a .js file
jsbeautify
• reformats Javascript source code
• enforces code formatting conventions
• easier to read and understand
• every time we change a .js file
Node.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.js
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
Node.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.js
Automated Testing
!
~ 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”
Node.js Development Workflow Automation with Grunt.js
Test Driven Development
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.
~ 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”
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
… 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.
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
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
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
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();!! !
});!
calculator_test.js
models/calc.js
Node.js Development Workflow Automation with Grunt.js
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!
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();!
! ! });! !
});!
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)
Protractor
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!');!
});
chromeConf.js
Demo
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 …
Node.js Development Workflow Automation with Grunt.js
Node.js Development Workflow Automation with Grunt.js
github.com/kiyanwang/
js-workflow-automation
engineering.talis.com
ns@talis.com
@kiyanwang
github/kiyanwang
We are hiring!
http://www.talis.com/jobs
@talis
facebook.com/talisgroup
@talis
facebook.com/talisgroup
+44 (0) 121 374 2740
!
talis.com
info@talis.com
!
48 Frederick Street
Birmingham
B1 3HN

More Related Content

What's hot (20)

PDF
Avoiding Common Pitfalls in Ember.js
Alex Speller
 
PPTX
Automated Testing with Cucumber, PhantomJS and Selenium
Dev9Com
 
KEY
Agile JavaScript Testing
Scott Becker
 
PPTX
Better End-to-End Testing with Page Objects Model using Protractor
Kasun Kodagoda
 
PPTX
Cypress Automation
Susantha Pathirana
 
PDF
APIs: A Better Alternative to Page Objects
Sauce Labs
 
PPTX
An Introduction to AngularJS End to End Testing using Protractor
Cubet Techno Labs
 
PPTX
CSS Regression Tests
Kaloyan Kosev
 
PPTX
Angular UI Testing with Protractor
Andrew Eisenberg
 
PDF
Cypress vs Selenium WebDriver: Better, Or Just Different? -- by Gil Tayar
Applitools
 
PPTX
Using protractor to build automated ui tests
🌱 Dale Spoonemore
 
PPTX
Protractor training
Sergiy Stotskiy
 
PDF
Selenium Tips & Tricks - StarWest 2015
Andrew Krug
 
PDF
Cypress - Best Practices
Brian Mann
 
PPTX
Java script unit testing
Mats Bryntse
 
PDF
Automated interactive testing for i os
Mobile March
 
PDF
JavaScript + Jenkins = Winning!
Eric Wendelin
 
PDF
[123] quality without qa
NAVER D2
 
PDF
How to Use Selenium, Successfully
Sauce Labs
 
PPTX
Introduction to Integration Testing With Cypress
Erez Cohen
 
Avoiding Common Pitfalls in Ember.js
Alex Speller
 
Automated Testing with Cucumber, PhantomJS and Selenium
Dev9Com
 
Agile JavaScript Testing
Scott Becker
 
Better End-to-End Testing with Page Objects Model using Protractor
Kasun Kodagoda
 
Cypress Automation
Susantha Pathirana
 
APIs: A Better Alternative to Page Objects
Sauce Labs
 
An Introduction to AngularJS End to End Testing using Protractor
Cubet Techno Labs
 
CSS Regression Tests
Kaloyan Kosev
 
Angular UI Testing with Protractor
Andrew Eisenberg
 
Cypress vs Selenium WebDriver: Better, Or Just Different? -- by Gil Tayar
Applitools
 
Using protractor to build automated ui tests
🌱 Dale Spoonemore
 
Protractor training
Sergiy Stotskiy
 
Selenium Tips & Tricks - StarWest 2015
Andrew Krug
 
Cypress - Best Practices
Brian Mann
 
Java script unit testing
Mats Bryntse
 
Automated interactive testing for i os
Mobile March
 
JavaScript + Jenkins = Winning!
Eric Wendelin
 
[123] quality without qa
NAVER D2
 
How to Use Selenium, Successfully
Sauce Labs
 
Introduction to Integration Testing With Cypress
Erez Cohen
 

Similar to Node.js Development Workflow Automation with Grunt.js (20)

PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
PDF
How do I Write Testable Javascript so I can Test my CF API on Server and Client
ColdFusionConference
 
PPTX
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
PDF
Automated Testing in Angular Slides
Jim Lynch
 
PPTX
Testing nodejs apps
felipefsilva
 
PDF
Frontend automation and stability
Máté Nádasdi
 
PDF
How to write Testable Javascript
ColdFusionConference
 
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
PPTX
Full Stack Unit Testing
GlobalLogic Ukraine
 
PPTX
How to write test in node.js
Jason Lin
 
PPTX
Introduction to bdd
antannatna
 
PDF
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
PDF
Quick tour to front end unit testing using jasmine
Gil Fink
 
PDF
軟體測試是在測試什麼?
Yao Nien Chung
 
PPT
Jasmine presentation Selenium Camp 2013
dimakovalenko
 
PDF
BDD Testing and Automating from the trenches - Presented at Into The Box June...
Gavin Pickin
 
PDF
ITB2016 -BDD testing and automation from the trenches
Ortus Solutions, Corp
 
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Ortus Solutions, Corp
 
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
3 WAYS TO TEST YOUR COLDFUSION API -
Ortus Solutions, Corp
 
How do I Write Testable Javascript so I can Test my CF API on Server and Client
ColdFusionConference
 
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
Automated Testing in Angular Slides
Jim Lynch
 
Testing nodejs apps
felipefsilva
 
Frontend automation and stability
Máté Nádasdi
 
How to write Testable Javascript
ColdFusionConference
 
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
Gavin Pickin
 
Full Stack Unit Testing
GlobalLogic Ukraine
 
How to write test in node.js
Jason Lin
 
Introduction to bdd
antannatna
 
Intro To JavaScript Unit Testing - Ran Mizrahi
Ran Mizrahi
 
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
Quick tour to front end unit testing using jasmine
Gil Fink
 
軟體測試是在測試什麼?
Yao Nien Chung
 
Jasmine presentation Selenium Camp 2013
dimakovalenko
 
BDD Testing and Automating from the trenches - Presented at Into The Box June...
Gavin Pickin
 
ITB2016 -BDD testing and automation from the trenches
Ortus Solutions, Corp
 
Ad

Recently uploaded (20)

PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
introduction to computer hardware and sofeware
chauhanshraddha2007
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
introduction to computer hardware and sofeware
chauhanshraddha2007
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
Ad

Node.js Development Workflow Automation with Grunt.js

  • 1. Node.Js Development Workflow Automation With Grunt.Js Nadeem Shabir Hydrahack Birmingham 18th March 2014
  • 2. ~ Paul Irish “Your job as a developer isn’t to just develop, it’s to continually learn how to develop better.”
  • 3. Automating common tasks • code quality (jshint) • beautifying (jsbeautifier) • live reload (watch) • Automated Testing
  • 6. – 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”
  • 7. Grunt.js • Javascript based task runner • automate repetitive tasks • large ecosystem of plugins (2500+) • easy to maintain • simple to learn and use
  • 9. 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
  • 11. watch • runs predefined tasks when files change • use patterns to indicate which files to watch
  • 12. jshint • static code analysis tool • detect errors & potential problems • enforce team coding conventions • every time we change a .js file
  • 13. jsbeautify • reformats Javascript source code • enforces code formatting conventions • easier to read and understand • every time we change a .js file
  • 17. live reload • monitors changes in files • preprocesses files as needed • browser automatically reloads and shows your changes • built into grunt-contrib-watch
  • 21. ! ~ 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”
  • 24. 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.
  • 25. ~ 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”
  • 26. 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
  • 27. … 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.
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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();!! ! });!
  • 35. 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!
  • 36. 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();! ! ! });! ! });!
  • 38. 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)
  • 40. 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!');! });
  • 42. Demo
  • 43. 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 …
  • 50. @talis facebook.com/talisgroup @talis facebook.com/talisgroup +44 (0) 121 374 2740 ! talis.com [email protected] ! 48 Frederick Street Birmingham B1 3HN