SlideShare a Scribd company logo
DANIEL JIMENEZ GARCIA
UNIT
TESTING
TYPESCRIPT
Daniel Jimenez | 2015-11-01 | Page 2
uSING

â€ș Qunit (Test Framework)
– Alternatives: Jasmine (BDD) Mocha (Node.js)
â€ș Sinon (Mocking Framework)
– Alternative: Jasmine spies
â€ș Grunt (JS Task Runner)
– Alternative: Gulp
Daniel Jimenez | 2015-11-01 | Page 3
1. Introduction
2. Writing Typescript tests using QUnit
and Sinon.js
â€ș Writing Qunit tests
â€ș Basic mocking
â€ș Mocking with Sinon.js
â€ș Advanced tips
3. Unit testing workflows
â€ș Manual workflow
â€ș Automated workflow using grunt
â€ș Scaffold html runner files using grunt
â€ș External modules
Agenda
1. INTRODUCTION
Daniel Jimenez | 2015-11-01 | Page 5
Qunit basics (1)
Visit: http://qunitjs.com/
Qunit requires the following pieces
Transform
The production code
One or more JavaScript files
Code to be tested
Defines the tests
The tests to be run
Usually one JavaScript file
Runs the tests
A test runner
HTML file. References QUnit library, production code
and test code
Test runner
Prod code
Test code
Daniel Jimenez | 2015-11-01 | Page 6
â€ș Running a test requires a browser
– Open HTML file in browser that includes:
â€ș QUnit JS file
â€ș QUnit CSS file
â€ș Production JS files
â€ș Test JS files
– Results are displayed in the html page
â€ș Reload or click rerun after changes
â€ș A JS task runner like Grunt can run
the tests without a browser
Qunit basics (2)
Daniel Jimenez | 2015-11-01 | Page 7
â€ș When testing TypeScript
– Compile TypeScript code into JavaScript
– Reference compiled JavaScript in the
HTML runner file
– Use a declaration file to reference libraries
like QUnit in your test TypeScript code
â€ș Download .d.ts files from:
http://definitelytyped.org/
â€ș A JS task runner like Grunt can be
used to compile TS code.
â€ș Some editors and IDE (like VS) can
also compile the TS code.
Qunit basics (3)
Daniel Jimenez | 2015-11-01 | Page 8
DEMO: QUNIT BASICS
2. WRITING Typescript
tests using QUnit and
Sinon.js
Daniel Jimenez | 2015-11-01 | Page 10
Writing qunit tests (1)
‱ Reference declarations
for Qunit, Sinon, etc
‱ Reference source code
‱ Import libraries in html
‱ Create test module
‱ Allows common setup
and teardown
‱ Add tests to the module
using QUnit test function
‱ Test logic goes in the
inline function
Daniel Jimenez | 2015-11-01 | Page 11
Writing qunit tests (2)
‱ Use module setup and teardown
‱ Create common dependencies and data in setup.
‱ Perform any cleanup between tests in teardown.
‱ Use appropriated QUnit assertions to your test.
‱ Avoid overusing ok assertion
‱ Don’t use equal when you need deepEqual
Familiarize with the range of assertions:
http://api.qunitjs.com/category/assert/
Daniel Jimenez | 2015-11-01 | Page 12
â€ș Take advantage of structural typing
– Structural typing is a way of relating types
based solely on their members
â€ș Need a stub of a class or interface?
– Create empty object literal
– Cast it as the class/interface
â€ș Need a mock of a class or interface?
– Create an object literal with just the
members needed
– Cast it as the class/interface
â€ș You can cast as any but you will lose
compiler support.
BASIC MOCKING (1)
Daniel Jimenez | 2015-11-01 | Page 13
â€ș Any object or member can be mocked
– Mock method in existing object
– Mock getter in singleton without setter
â€ș Be careful if changing prototypes
– At least restore them
â€ș Try to avoid accessing private
members
– Anything accessible with the brackets
notation can be mocked
– This includes TS private members
â€ș Use this with hard to test code. Then
refactor your code and your tests
BASIC MOCKING (2)
Daniel Jimenez | 2015-11-01 | Page 14
â€ș Create spies to verify interactions
– Let you assert when a particular method
was called, how many times, with which
arguments, etc
– Can wrap an existing method, but the
method will still be called
– If you need to control what the method will
return, use a stub
â€ș http://sinonjs.org/docs/#spies
MOCKING WITH SINON (1)
Daniel Jimenez | 2015-11-01 | Page 15
â€ș Create stubs to replace method
implementations with mocked ones
– Existing methods are replaced, the
original implementation is not executed
– Let you specify the return values of a
particular method.
– Allows complex setup like:
â€ș a return value for the Nth call
â€ș a return value for specific arguments
â€ș Stubs are also spies
– When possible choose spies over stubs
â€ș http://sinonjs.org/docs/#stubs
MOCKING WITH SINON (2)
Daniel Jimenez | 2015-11-01 | Page 16
Advanced tips (1)
‱ Create a sinon sandbox in test setup
‱ Sinon stubs/spies in test setup are created using
the sandbox
‱ Restore the sandbox in test teardown
‱ Move common test references and type
definitions to its own file.
‱ Reference this file in your test files
Daniel Jimenez | 2015-11-01 | Page 17
Advanced tips (2)
‱ Try to avoid QUnit asynchronous tests!
‱ Use stubs to return resolved/rejected promises
‱ Use a spy to verify done/fail callbacks
‱ Avoid time-dependent tests!
‱ Use sinon FakeTimers to control Date, setInterval
and setTimeout
‱ Use a sandbox to restore the clock between tests
Daniel Jimenez | 2015-11-01 | Page 18
Advanced tips (3)
‱ Use object builder pattern with fluent syntax
‱ Useful not only when creating test data but also
creating classes under test
‱ Test will support refactorings better
‱ Use sinon matchers and assertions
‱ Test code is more expressive
‱ Avoid checking more than is needed
Daniel Jimenez | 2015-11-01 | Page 19
DEMO: writing tests using
QUNIT and sinon
3. Unit testing
workflows
Daniel Jimenez | 2015-11-01 | Page 21
MANUAL WORKFLOW
You can run unit tests just by compiling typescript and using a browser
Might be enough for small projects
Create tests
Write TypeScript tests
Create QUnit html test
runners
Manually manage
dependencies by adding
scripts in the test runner
html file
Manually manage paths to
the compiled files
Compile TS
Compile TypeScript code
and tests into JavaScript.
Some IDE and editors can
compile TypeScript
automatically
You can also
manually compile using
tsc.exe or Node.js
Run in browser
Manually open QUnit test
runner html files in the browser
Every html file has to be
opened independently
Requires someone
to run the tests and
interpret the results
Daniel Jimenez | 2015-11-01 | Page 22
AUTOMATing the WORKFLOW USING
Grunt is a JS Task Runner that can automate tasks like bundling or minification
There are hundreds of plugins, including TS compilation and running QUnit tests
STEP 4
Text
Create tests
Write TypeScript tests
Create QUnit html test
runners
Manually manage
dependencies by adding
scripts in the test runner
html file
Manually manage paths to
the compiled files in the
folder
Configure Grunt
Install grunt-ts for compiling
TypeScript
Install grunt-contrib-qunit
for running QUnit tests
Add tasks in your
gruntfile for:
1. Clean a temp folder
2. Compile into temp
folder
3. Copy html files into
temp folder
4. Run QUnit tests
Run Grunt
Add a “test” task in the
gruntfile . It will run the
tasks in the full process
Run grunt either from the
command line (installing
grunt-cli) or from an
IDE like Visual Studio
The full process can be run
with a single command, will
report test results and the
exit code will be non zero if
failed
http://gruntjs.com/
https://github.com/grunt
js/grunt-contrib-qunit
https://github.com/Type
Strong/grunt-ts
Daniel Jimenez | 2015-11-01 | Page 23
SCAFFOLD QUNIT HTML FILES USING
You can create your own grunt plugins and tasks
Create a task that searches for ///< reference tags and scaffolds the html file
STEP 4
Text
Create tests
Write TypeScript tests
Configure Grunt
Create task to scaffold
QUnit html files
Update the tasks in your
grunt file as :
1. Clean temp folder
2. Compile into temp
folder
3. Scaffold html files into
temp folder
4. Run QUnit tests
Run Grunt
Add a “test” task in the
gruntfile . It will run the
tasks in the full process
Run grunt either from the
command line (installing
grunt-cli) or from an
IDE like Visual Studio
The full process can be run
with a single command, will
report test results and the
exit code will be non zero if
failed
Check example in:
https://github.com/DaniJG
/TypeScriptUTWorkflows
Daniel Jimenez | 2015-11-01 | Page 24
USING EXTERNAL MODULES WITH
‱ Write TypeScript code using external modules via
export and import (including your tests)
‱ Specify the module option when compiling
TypeScript
‱ Dependencies are loaded by the module system
(require.js or common.js) instead of script tags.
‱ Using require.js needs to adjust test startup
Daniel Jimenez | 2015-11-01 | Page 25
DEMO: unit testing workflows
Unit Testing TypeScript

More Related Content

PDF
Test-Driven Development with TypeScript+Jasmine+AngularJS
PPT
TypeScript - Javascript done right
PPTX
Ready, set, go! An introduction to the Go programming language
PPTX
Golang
ODP
Test Driven Development (TDD) with Windows PowerShell
PPTX
C++ Unit testing - the good, the bad & the ugly
ODP
Behaviour Driven Development Hands-on
PDF
Let's Contribute
Test-Driven Development with TypeScript+Jasmine+AngularJS
TypeScript - Javascript done right
Ready, set, go! An introduction to the Go programming language
Golang
Test Driven Development (TDD) with Windows PowerShell
C++ Unit testing - the good, the bad & the ugly
Behaviour Driven Development Hands-on
Let's Contribute

What's hot (20)

PDF
Test Driven Development with OSGi - BalĂĄzs Zsoldos
PPTX
Working with c++ legacy code
PPTX
ATO 2014 - So You Think You Know 'Go'? The Go Programming Language
PPT
Flash Camp Chennai - Build automation of Flex and AIR applications
PDF
Develop Maintainable Apps - edUiConf
PDF
Go lang
PPTX
The New York Times: Sustainable Systems, Powered by Python
PDF
Developer Job in Practice
 
PPTX
Vagrant and Docker
PDF
TDD for joomla extensions
PDF
Monorepo at Pinterest
PPTX
XPDays-2018
PDF
DockerCon US 2016 - Scaling Open Source operations
PDF
Test Driven Development with PHP
PPTX
Advantages and disadvantages of a monorepo
PDF
Devops is (not ) a buzzword
PPTX
Mono Repo
PPTX
Intro to Continuous Integration
PDF
Testing cloud and kubernetes applications - ElasTest
PDF
The way Devs do Ops
Test Driven Development with OSGi - BalĂĄzs Zsoldos
Working with c++ legacy code
ATO 2014 - So You Think You Know 'Go'? The Go Programming Language
Flash Camp Chennai - Build automation of Flex and AIR applications
Develop Maintainable Apps - edUiConf
Go lang
The New York Times: Sustainable Systems, Powered by Python
Developer Job in Practice
 
Vagrant and Docker
TDD for joomla extensions
Monorepo at Pinterest
XPDays-2018
DockerCon US 2016 - Scaling Open Source operations
Test Driven Development with PHP
Advantages and disadvantages of a monorepo
Devops is (not ) a buzzword
Mono Repo
Intro to Continuous Integration
Testing cloud and kubernetes applications - ElasTest
The way Devs do Ops
Ad

Viewers also liked (20)

PDF
13 Signs You Might Be A Bad Boss
PPTX
Unit testing js
PPTX
Q unit
PPTX
#SUGDE Sitecore Gesundheit
PDF
Javascript unit testing with QUnit and Sinon
PDF
Testing with Express, Mocha & Chai
PPTX
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
PPTX
Testing Javascript Apps with Mocha and Chai
PPTX
Test automation introduction training at Polteq
PDF
Unit testing with mocha
PPTX
Finjs - Angular 2 better faster stronger
PDF
Efficient JavaScript Unit Testing, May 2012
PDF
Advanced Jasmine - Front-End JavaScript Unit Testing
PPTX
SoapUI one key to all doors
PDF
Test trend analysis: Towards robust reliable and timely tests
PDF
WixAutomation - Test State Pattern - Selenium Camp 2017
PPTX
How does Java 8 exert hidden power on Test Automation?
PPTX
The wild wild west of Selenium Capabilities
PPTX
Roman iovlev. Test UI with JDI - Selenium camp
PPTX
Colorful world-of-visual-automation-testing-latest
13 Signs You Might Be A Bad Boss
Unit testing js
Q unit
#SUGDE Sitecore Gesundheit
Javascript unit testing with QUnit and Sinon
Testing with Express, Mocha & Chai
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
Testing Javascript Apps with Mocha and Chai
Test automation introduction training at Polteq
Unit testing with mocha
Finjs - Angular 2 better faster stronger
Efficient JavaScript Unit Testing, May 2012
Advanced Jasmine - Front-End JavaScript Unit Testing
SoapUI one key to all doors
Test trend analysis: Towards robust reliable and timely tests
WixAutomation - Test State Pattern - Selenium Camp 2017
How does Java 8 exert hidden power on Test Automation?
The wild wild west of Selenium Capabilities
Roman iovlev. Test UI with JDI - Selenium camp
Colorful world-of-visual-automation-testing-latest
Ad

Similar to Unit Testing TypeScript (20)

PDF
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
PDF
Qunit tutorial
PPTX
JS Frameworks Day April,26 of 2014
 
PDF
Js fwdays unit tesing javascript(by Anna Khabibullina)
PDF
How to write Testable Javascript
PDF
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
PPTX
Testing nodejs apps
PPT
Js unit testing
PPTX
Qunit Java script Un
ODP
Js unit testingpresentation
PDF
Unit Testing in JavaScript
PPT
Test Driven Development using QUnit
ODP
Dot Net Notts Js Unit Testing at Microlise
PDF
Javascript Unit Testing Tools
PPT
JavaScript Unit Testing
PPTX
Mini training - Moving to xUnit.net
PDF
Quick tour to front end unit testing using jasmine
JAVASCRIPT TDD(Test driven Development) & Qunit Tutorial
Qunit tutorial
JS Frameworks Day April,26 of 2014
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
How to write Testable Javascript
How do I write Testable Javascript - Presented at dev.Objective() June 16, 2016
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API -
Testing nodejs apps
Js unit testing
Qunit Java script Un
Js unit testingpresentation
Unit Testing in JavaScript
Test Driven Development using QUnit
Dot Net Notts Js Unit Testing at Microlise
Javascript Unit Testing Tools
JavaScript Unit Testing
Mini training - Moving to xUnit.net
Quick tour to front end unit testing using jasmine

Recently uploaded (20)

PPT
Introduction Database Management System for Course Database
PDF
System and Network Administration Chapter 2
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
medical staffing services at VALiNTRY
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Introduction Database Management System for Course Database
System and Network Administration Chapter 2
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Wondershare Filmora 15 Crack With Activation Key [2025
Upgrade and Innovation Strategies for SAP ERP Customers
How to Choose the Right IT Partner for Your Business in Malaysia
medical staffing services at VALiNTRY
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
How to Migrate SBCGlobal Email to Yahoo Easily
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
VVF-Customer-Presentation2025-Ver1.9.pptx
Understanding Forklifts - TECH EHS Solution
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...

Unit Testing TypeScript

  • 2. Daniel Jimenez | 2015-11-01 | Page 2 uSING
 â€ș Qunit (Test Framework) – Alternatives: Jasmine (BDD) Mocha (Node.js) â€ș Sinon (Mocking Framework) – Alternative: Jasmine spies â€ș Grunt (JS Task Runner) – Alternative: Gulp
  • 3. Daniel Jimenez | 2015-11-01 | Page 3 1. Introduction 2. Writing Typescript tests using QUnit and Sinon.js â€ș Writing Qunit tests â€ș Basic mocking â€ș Mocking with Sinon.js â€ș Advanced tips 3. Unit testing workflows â€ș Manual workflow â€ș Automated workflow using grunt â€ș Scaffold html runner files using grunt â€ș External modules Agenda
  • 5. Daniel Jimenez | 2015-11-01 | Page 5 Qunit basics (1) Visit: http://qunitjs.com/ Qunit requires the following pieces Transform The production code One or more JavaScript files Code to be tested Defines the tests The tests to be run Usually one JavaScript file Runs the tests A test runner HTML file. References QUnit library, production code and test code Test runner Prod code Test code
  • 6. Daniel Jimenez | 2015-11-01 | Page 6 â€ș Running a test requires a browser – Open HTML file in browser that includes: â€ș QUnit JS file â€ș QUnit CSS file â€ș Production JS files â€ș Test JS files – Results are displayed in the html page â€ș Reload or click rerun after changes â€ș A JS task runner like Grunt can run the tests without a browser Qunit basics (2)
  • 7. Daniel Jimenez | 2015-11-01 | Page 7 â€ș When testing TypeScript – Compile TypeScript code into JavaScript – Reference compiled JavaScript in the HTML runner file – Use a declaration file to reference libraries like QUnit in your test TypeScript code â€ș Download .d.ts files from: http://definitelytyped.org/ â€ș A JS task runner like Grunt can be used to compile TS code. â€ș Some editors and IDE (like VS) can also compile the TS code. Qunit basics (3)
  • 8. Daniel Jimenez | 2015-11-01 | Page 8 DEMO: QUNIT BASICS
  • 9. 2. WRITING Typescript tests using QUnit and Sinon.js
  • 10. Daniel Jimenez | 2015-11-01 | Page 10 Writing qunit tests (1) ‱ Reference declarations for Qunit, Sinon, etc ‱ Reference source code ‱ Import libraries in html ‱ Create test module ‱ Allows common setup and teardown ‱ Add tests to the module using QUnit test function ‱ Test logic goes in the inline function
  • 11. Daniel Jimenez | 2015-11-01 | Page 11 Writing qunit tests (2) ‱ Use module setup and teardown ‱ Create common dependencies and data in setup. ‱ Perform any cleanup between tests in teardown. ‱ Use appropriated QUnit assertions to your test. ‱ Avoid overusing ok assertion ‱ Don’t use equal when you need deepEqual Familiarize with the range of assertions: http://api.qunitjs.com/category/assert/
  • 12. Daniel Jimenez | 2015-11-01 | Page 12 â€ș Take advantage of structural typing – Structural typing is a way of relating types based solely on their members â€ș Need a stub of a class or interface? – Create empty object literal – Cast it as the class/interface â€ș Need a mock of a class or interface? – Create an object literal with just the members needed – Cast it as the class/interface â€ș You can cast as any but you will lose compiler support. BASIC MOCKING (1)
  • 13. Daniel Jimenez | 2015-11-01 | Page 13 â€ș Any object or member can be mocked – Mock method in existing object – Mock getter in singleton without setter â€ș Be careful if changing prototypes – At least restore them â€ș Try to avoid accessing private members – Anything accessible with the brackets notation can be mocked – This includes TS private members â€ș Use this with hard to test code. Then refactor your code and your tests BASIC MOCKING (2)
  • 14. Daniel Jimenez | 2015-11-01 | Page 14 â€ș Create spies to verify interactions – Let you assert when a particular method was called, how many times, with which arguments, etc – Can wrap an existing method, but the method will still be called – If you need to control what the method will return, use a stub â€ș http://sinonjs.org/docs/#spies MOCKING WITH SINON (1)
  • 15. Daniel Jimenez | 2015-11-01 | Page 15 â€ș Create stubs to replace method implementations with mocked ones – Existing methods are replaced, the original implementation is not executed – Let you specify the return values of a particular method. – Allows complex setup like: â€ș a return value for the Nth call â€ș a return value for specific arguments â€ș Stubs are also spies – When possible choose spies over stubs â€ș http://sinonjs.org/docs/#stubs MOCKING WITH SINON (2)
  • 16. Daniel Jimenez | 2015-11-01 | Page 16 Advanced tips (1) ‱ Create a sinon sandbox in test setup ‱ Sinon stubs/spies in test setup are created using the sandbox ‱ Restore the sandbox in test teardown ‱ Move common test references and type definitions to its own file. ‱ Reference this file in your test files
  • 17. Daniel Jimenez | 2015-11-01 | Page 17 Advanced tips (2) ‱ Try to avoid QUnit asynchronous tests! ‱ Use stubs to return resolved/rejected promises ‱ Use a spy to verify done/fail callbacks ‱ Avoid time-dependent tests! ‱ Use sinon FakeTimers to control Date, setInterval and setTimeout ‱ Use a sandbox to restore the clock between tests
  • 18. Daniel Jimenez | 2015-11-01 | Page 18 Advanced tips (3) ‱ Use object builder pattern with fluent syntax ‱ Useful not only when creating test data but also creating classes under test ‱ Test will support refactorings better ‱ Use sinon matchers and assertions ‱ Test code is more expressive ‱ Avoid checking more than is needed
  • 19. Daniel Jimenez | 2015-11-01 | Page 19 DEMO: writing tests using QUNIT and sinon
  • 21. Daniel Jimenez | 2015-11-01 | Page 21 MANUAL WORKFLOW You can run unit tests just by compiling typescript and using a browser Might be enough for small projects Create tests Write TypeScript tests Create QUnit html test runners Manually manage dependencies by adding scripts in the test runner html file Manually manage paths to the compiled files Compile TS Compile TypeScript code and tests into JavaScript. Some IDE and editors can compile TypeScript automatically You can also manually compile using tsc.exe or Node.js Run in browser Manually open QUnit test runner html files in the browser Every html file has to be opened independently Requires someone to run the tests and interpret the results
  • 22. Daniel Jimenez | 2015-11-01 | Page 22 AUTOMATing the WORKFLOW USING Grunt is a JS Task Runner that can automate tasks like bundling or minification There are hundreds of plugins, including TS compilation and running QUnit tests STEP 4 Text Create tests Write TypeScript tests Create QUnit html test runners Manually manage dependencies by adding scripts in the test runner html file Manually manage paths to the compiled files in the folder Configure Grunt Install grunt-ts for compiling TypeScript Install grunt-contrib-qunit for running QUnit tests Add tasks in your gruntfile for: 1. Clean a temp folder 2. Compile into temp folder 3. Copy html files into temp folder 4. Run QUnit tests Run Grunt Add a “test” task in the gruntfile . It will run the tasks in the full process Run grunt either from the command line (installing grunt-cli) or from an IDE like Visual Studio The full process can be run with a single command, will report test results and the exit code will be non zero if failed http://gruntjs.com/ https://github.com/grunt js/grunt-contrib-qunit https://github.com/Type Strong/grunt-ts
  • 23. Daniel Jimenez | 2015-11-01 | Page 23 SCAFFOLD QUNIT HTML FILES USING You can create your own grunt plugins and tasks Create a task that searches for ///< reference tags and scaffolds the html file STEP 4 Text Create tests Write TypeScript tests Configure Grunt Create task to scaffold QUnit html files Update the tasks in your grunt file as : 1. Clean temp folder 2. Compile into temp folder 3. Scaffold html files into temp folder 4. Run QUnit tests Run Grunt Add a “test” task in the gruntfile . It will run the tasks in the full process Run grunt either from the command line (installing grunt-cli) or from an IDE like Visual Studio The full process can be run with a single command, will report test results and the exit code will be non zero if failed Check example in: https://github.com/DaniJG /TypeScriptUTWorkflows
  • 24. Daniel Jimenez | 2015-11-01 | Page 24 USING EXTERNAL MODULES WITH ‱ Write TypeScript code using external modules via export and import (including your tests) ‱ Specify the module option when compiling TypeScript ‱ Dependencies are loaded by the module system (require.js or common.js) instead of script tags. ‱ Using require.js needs to adjust test startup
  • 25. Daniel Jimenez | 2015-11-01 | Page 25 DEMO: unit testing workflows

Editor's Notes

  • #9: Demo TypeScriptUTIntro: Show stack source, test code and html test runner Open test in chrome. Add new test for push method and refresh test in chrome (The test will push an item, then calling pop should return the same item)
  • #20: Demo TypeScriptUT: Add second test in userFormProcessingTest to validate the user will be posted when the validation succeeds. Shared instances for stubs/mocks are created in the startup method. A sandbox is created. Stub for user name validation is configured using withArgs so the stub is only applied when that particular name is validated The validation stub returns a resolved promise, telling the user is valid. User is created with an object builder. (Adding a property or refactor into a class would be easier later) UserFormProcessor (class under test) is also created with an object builder. (Adding/modifying a dependency would be easier) There is a spy to verify the postUser method was called There is another spy to verify the done callback of the asynchronous method we were testing was called The code we are testing and its dependencies are asynchronous, but the whole test can be synchronously run.
  • #22: 11/17/2015
  • #23: 11/17/2015
  • #24: 11/17/2015
  • #26: Demo TypeScriptUTWorkflows: The Manual one was already modified AutomatedWorkflow You can just run “grunt test” from the command line or tools like VS Add an option to run particular tests like the “-p” argument we have used in Ericsson The code is compiled into a temp folder, you can still open html files from there in chrome and debug Show gruntfile ScaffoldingWorkflow Similar to previous one but there is a new custom task in the gruntfile to generate the html files This task searches for reference tags in the ts code and genereates the html file using a handlebars template Show how we don’t need to worry about writing html files anymore ExternalModulesWorkflow This idea can work with external modules too Code has to be written and compiled using external modules (Using a module loader like require.js) Show compiled JS code The html template has to be modified so we load and start Qunit using require.js