About This Book
Learn the life cycle of TDD and its importance in real-world application
Gain knowledge about popular tools and analyze features, syntax, and how they help in JavaScript testing
Implement test-driven programming exercises using the practical code examples
Who This Book Is For
If you have an intermediate knowledge of HTML, CSS, and JavaScript and want to learn how and why the test-driven development approach is better for your assignments, then this book is for you.
What You Will Learn
Basic TDD fundamentals, life cycle, and benefits
Become acquainted with the concepts and elements of unit testing and writing basic unit tests for JavaScript
Understand the way JsUnit, Qunit, Karma and DalekJs work
Use the Jasmine framework
Interpret feature detection and devise tests specific to cross-browser compatibility
Integrate jsTestDriver with Eclipse and run tests with jsTestDriver
Explore re-factoring, adding and notifying observers
Understand test-driven development in case of server-side JS
In Detail
Initially, all processing used to happen on the server-side and simple output was the response to web browsers. Nowadays, there are so many JavaScript frameworks and libraries created that help readers to create charts, animations, simulations, and so on. By the time a project finishes or reaches a stable state, so much JavaScript code has already been written that changing and maintaining it further is tedious. Here comes the importance of automated testing and more specifically, developing all that code in a test-driven environment. Test-driven development is a methodology that makes testing the central part of the design process – before writing code developers decide upon the conditions that code must meet to pass a test. The end goal is to help the readers understand the importance and process of using TDD as a part of development.
This book starts with the details about test-driven development, its importance, need, and benefits. Later the book introduces popular tools and frameworks like YUI, Karma, QUnit, DalekJS, JsUnit and goes on to utilize Jasmine, Mocha, Karma for advanced concepts like feature detection, server-side testing, and patterns. We are going to understand, write, and run tests, and further debug our programs. The book concludes with best practices in JavaScript testing. By the end of the book, the readers will know why they should test, how to do it most efficiently, and will have a number of versatile tests (and methods for devising new tests) to get to work immediately.
Style and approach
Easy-to-follow guide with suitable examples for developing JavaScript code in the test-Driven environment, with popular tools and frameworks. User experience and statements are also included to help readers make a better choice of tool for real-world projects.
Chapter 1: Overview of TDD 1
Complexity of web pages 1
Understanding test-driven development 2
The need for testing 2
Types of testing 4
The life cycle of TDD 4
TDD microcycle 7
Agile and TDD 8
Benefits of TDD and common myths 8
Benefits 8
Myths 10
Chapter 2: Testing Concepts 13 Unit testing 13
Unit testing frameworks 14
YUI Tests 15
Following the process 15
Preparing the environment 16
Following the life cycle 19
Writing a test 19
Running the test and seeing if test fails 20
Writing a production code 21
Running all tests 23
Cleaning up the code 24
Repeat 24
Using the browser console 24
setUp() and tearDown() 25
Test suites 26
Actions and assertions 27
Actions 27
Assertions 30
Benefits and pitfalls 32
Benefits of unit testing 32
Pitfalls of unit testing 33
Chapter 3: Testing Tools 35
JsUnit 35
Getting started 36
Writing tests 37
Running tests 39
QUnit 42
Getting started 42
Writing tests 42
Running tests 44
Karma with Jasmine 45
Getting started 45
Writing tests 46
Running tests 49
DalekJS 50
Getting started 51
Writing tests 53
Actions 53
Assertions 54
Running tests 54
Chapter 4: Jasmine 59
Understanding behavior-driven development 59
Setting up Jasmine 60
describe and specs 61
Expectations 63
Matchers 64
Set up and tear down 67
Spies 69
Tracking spies using calls 76
Creating a custom spy 78
Jasmine clock 80
Creating a custom matcher 82
Creating a custom equality tester 85
Asynchronous calls 87
The Jasmine Ajax plugin 88
Nesting suites 89
Disabling suites and specs 92
Chapter 5: JsTestDriver 95
JsTestDriver 95
Overview 95
Getting started 96
Writing tests 96
Assertions 97
Capturing the browser 98
Restructuring your project 100
The configuration file 101
Running tests using the command prompt 102
Setting up JsTestDriver with IDE 105
Running the tests 109
Running Jasmine specs 111
Code coverage 112
Chapter 6: Feature Detection 117
Understanding feature detection 117
Available methods and libraries 118
has.js 119
Writing custom tests 120
Modernizr 120
Downloading and setting up Modernizr 120
How it works 122
Using Modernizr 124
Polyfills 124
Loading polyfills 125
Supported browsers 126
Browser detection 126
User agent sniffing 126
Object detection 128
Features testing with Modernizr 129
CSS features 129
HTML5 features 131
Miscellaneous features 133
Plugins for additional tests 134
Modernizr methods 134
Modernizr.prefixed() 134
Modernizr.prefixedCSS() 137
Modernizr.mq() 137
Modernizr.on() 138
Modernizr.atRule() 138
Modernizr.addTest() 139
Modernizr.testStyles() 140
Modernizr.testProp() 141
Modernizr.testAllProps() 141
Modernizr.hasEvent() 141
Modernizr._prefixes and Modernizr._domPrefixes 142
Undetectable features 143
Chapter 7: Observer Design Pattern 145
Overview 145
When to use it 147
Advantages 147
Disadvantages 148
Different implementations of the observer 148
Event dispatcher/listener 148
Publish/subscribe 150
Push versus pull 150
Sample code 150
Understanding patterns in API 151
Observer with topics 152
Observer pattern using jQuery.Callbacks 154
Implementing the observer pattern using TDD 157
Red step (code with error) 157
Green step (with running code) 158
Refactoring 160
Hot swapping components 162
Browser compatibility of the observer API 162
Chapter 8: Testing with Server-Side JS 165
Setting up the environment 165
Installing Node.js 166
Setting up the application 167
Choosing a test runner 167
Mocha and Chai 168
Server-side unit testing 172
Implementing the web server 172
Helpdesk – sample application 175
Setting up the MongoDB database 176
The Mocha test 180
Chapter 9: Best Practices 195
TDD best practices 195
Follow proper rules to define test cases 196
Make test case names more readable 196
Keep the same name for test files and source files 196
Keep the name descriptive 197
Applying proper processes 197
Making sure that tests are written before starting implementation 197
Modifying/writing new code only when the test is failing 198
Running all the tests when we modify anything in the existing code 198
Existing tests should pass before new tests are written 198
Cleaning up code once all test cases are passed 199
Follow right development practices 199
Simple code that can be easily understood 199
Writing assertions first 200
One assertion per test is enough 200
Keep your focus on findability 201
Reducing duplication 201
Tests should run fast 202
Using mocks 202
Using stubs 203
Using setUp and tearDown methods 203
Choosing the right tool 204
Feature detection tools 205
Server-side testing tools 205
Asynchronous testing 205
Running time of unit tests 205
Browser support 205
Other features 206
JsLint 207
No comments:
Post a Comment