Jasmine JavaScript Testing - Second Edition
()
About this ebook
- Leverage the power of unit testing React.js to develop full-fledged JavaScript applications
- Learn the best practices of modularization and code organization while scaling your application
- Enhance your practical skills required to develop applications using the Jasmine framework in a step-by-step manner
This book is for web developers and designers who work with React.js and JavaScript and who are new to unit testing and automation. It's assumed that you have a basic knowledge of JavaScript and HTML.
Related to Jasmine JavaScript Testing - Second Edition
Related ebooks
Mastering MeteorJS Application Development Rating: 4 out of 5 stars4/5Learning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsJavaScript Security Rating: 4 out of 5 stars4/5Learning Single-page Web Application Development Rating: 0 out of 5 stars0 ratingsNW.js Essentials Rating: 0 out of 5 stars0 ratingsLearning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5React Components Rating: 0 out of 5 stars0 ratingsNode.js High Performance Rating: 0 out of 5 stars0 ratingsLearning Bootstrap Rating: 1 out of 5 stars1/5AngularJS Deployment Essentials Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsMastering Java EE Development with WildFly Rating: 0 out of 5 stars0 ratingsJavaScript Regular Expressions Rating: 3 out of 5 stars3/5Responsive Design High Performance Rating: 0 out of 5 stars0 ratingsExpress Web Application Development Rating: 3 out of 5 stars3/5Mastering JavaScript Object-Oriented Programming Rating: 0 out of 5 stars0 ratingsLearning Underscore.js Rating: 0 out of 5 stars0 ratingsDistributed Computing in Java 9 Rating: 0 out of 5 stars0 ratingsJavaScript Unlocked Rating: 5 out of 5 stars5/5Deploying Node.js Rating: 5 out of 5 stars5/5Getting Started with React Native Rating: 4 out of 5 stars4/5Mastering JavaScript Design Patterns - Second Edition Rating: 5 out of 5 stars5/5JavaScript Projects for Kids Rating: 0 out of 5 stars0 ratingsReactJS for Jobseekers: The Only Guide You Need to Learn React and Crack Interviews (English Edition) Rating: 0 out of 5 stars0 ratingsLearning AngularJS Animations Rating: 4 out of 5 stars4/5Mockito Essentials Rating: 3 out of 5 stars3/5ASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsRESTful Web API Design with Node.js - Second Edition Rating: 1 out of 5 stars1/5Getting Started with Meteor.js JavaScript Framework - Second Edition Rating: 0 out of 5 stars0 ratings
Internet & Web For You
More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Introduction to Internet Scams and Fraud: Credit Card Theft, Work-At-Home Scams and Lottery Scams Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5Cybersecurity For Dummies Rating: 4 out of 5 stars4/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsHow To Make Money Blogging: How I Replaced My Day-Job With My Blog and How You Can Start A Blog Today Rating: 4 out of 5 stars4/5The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5Wireless Hacking 101 Rating: 4 out of 5 stars4/5Tor and the Dark Art of Anonymity Rating: 5 out of 5 stars5/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5200+ Ways to Protect Your Privacy: Simple Ways to Prevent Hacks and Protect Your Privacy--On and Offline Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SEO For Dummies Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5How To Start A Podcast Rating: 4 out of 5 stars4/5Remote/WebCam Notarization : Basic Understanding Rating: 3 out of 5 stars3/5The Internet Is Not What You Think It Is: A History, a Philosophy, a Warning Rating: 4 out of 5 stars4/5Podcasting For Dummies Rating: 4 out of 5 stars4/5The Cyber Attack Survival Manual: Tools for Surviving Everything from Identity Theft to the Digital Apocalypse Rating: 0 out of 5 stars0 ratingsHow to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5No Place to Hide: Edward Snowden, the NSA, and the U.S. Surveillance State Rating: 4 out of 5 stars4/5
Reviews for Jasmine JavaScript Testing - Second Edition
0 ratings0 reviews
Book preview
Jasmine JavaScript Testing - Second Edition - Paulo Ragonha
Table of Contents
Jasmine JavaScript Testing Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started with Jasmine
JavaScript – the bad parts
Jasmine and behavior-driven development
Downloading Jasmine
Summary
2. Your First Spec
The Investment Tracker application
Jasmine basics and thinking in BDD
Setup and teardown
Nested describes
Setup and teardown
Coding a spec with shared behavior
Understanding matchers
Custom matchers
Built-in matchers
The toEqual built-in matcher
The toBe built-in matcher
The toBeTruthy and toBeFalsy matchers
The toBeUndefined, toBeNull, and toBeNaN built-in matchers
The toBeDefined built-in matcher
The toContain built-in matcher
The toMatch built-in matcher
The toBeLessThan and toBeGreaterThan built-in matchers
The toBeCloseTo built-in matcher
The toThrow built-in matcher
Summary
3. Testing Frontend Code
Thinking in terms of components (Views)
The module pattern
Using HTML fixtures
Basic View coding rules
The View should encapsulate a DOM element
Integrating Views with observers
Testing Views with jQuery matchers
The toBeMatchedBy jQuery matcher
The toContainHtml jQuery matcher
The toContainElement jQuery matcher
The toHaveValue jQuery matcher
The toHaveAttr jQuery matcher
The toBeFocused jQuery matcher
The toBeDisabled jQuery matcher
More matchers
Summary
4. Asynchronous Testing – AJAX
Acceptance criterion
Setting up the scenario
Installing Node.js
Coding the server
Running the server
Writing the spec
Asynchronous setups and teardowns
Asynchronous specs
Timeout
Summary
5. Jasmine Spies
The bare
spy
Spying on an object's functions
Testing DOM events
Summary
6. Light Speed Unit Testing
Jasmine stubs
Jasmine Ajax
Installing the plugin
A fake XMLHttpRequest
Summary
7. Testing React Applications
Project setup
Our first React component
The Virtual DOM
JSX
Using JSX with Jasmine
Component attributes (props)
Component events
Component state
Component life cycle
Composing components
Summary
8. Build Automation
Module bundler – webpack
Module definition
Webpack project setup
Managing dependencies with NPM
Webpack configuration
The spec runner
Testing a module
Test runner: Karma
Quick feedback loop
Watch and run the tests
Watch and update the browser
Optimizing for production
Static code analysis: JSHint
Continuous integration – Travis-CI
Adding a project to Travis-CI
Project setup
Summary
Index
Jasmine JavaScript Testing Second Edition
Jasmine JavaScript Testing Second Edition
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: August 2013
Second edition: April 2015
Production reference: 1210415
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-204-1
www.packtpub.com
Credits
Author
Paulo Ragonha
Reviewers
Hany A. Elemary
Ryzhikov Maksim
Veer Shubhranshu Shrivastav
Sergey Simonchik
Commissioning Editor
Amarabha Banerjee
Acquisition Editor
Larissa Pinto
Content Development Editor
Manasi Pandire
Technical Editor
Anushree Arun Tendulkar
Copy Editor
Sarang Chari
Project Coordinator
Suzanne Coutinho
Proofreaders
Paul Hindle
Linda Morris
Indexer
Tejal Soni
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
About the Author
Paulo Ragonha is a software engineer with over 7 years of professional experience. An advocate of the open Web, he is inspired and driven to build compelling experiences on top of this ubiquitous platform.
He loves to hack, so you will often see him wandering around in conferences or attending hackathons. His most recent professional experiences ranged from DevOps (with Chef and Docker) to moving up the stack with Node.js, Ruby, and Python and all the way toward building single-page applications (mostly with Backbone.js and ad hoc
solutions).
Passionate about automation, he sees testing as a liberating tool to enjoy the craft of writing code even more. Back in 2013, he wrote the first edition of the book Jasmine JavaScript Testing, Packt Publishing.
Paulo has an amazing wife, who he loves very much. He lives in beautiful Florianópolis, a coastal city in the south of Brazil. He is a casual speaker, a biker, a runner, and a hobbyist photographer.
About the Reviewers
Hany A. Elemary is a software engineer / technical team lead at OCLC in Columbus, Ohio, currently working on the next generation of mobile/web apps (http://www.worldcat.org and WorldCat for local institutions). He has been blessed with diverse experience while working for multiple companies (from small software shops to large corporations) and seeing different releasable software strategies. He has a clear focus and passion for mobile/web UI design, interactions, usability, and accessibility. When there is time, he enjoys playing his acoustic guitar, AnnaMaria.
Special thanks to my close friends and family for always pushing me to be better in every aspect of life.
Ryzhikov Maksim is a 27-year-old software developer from Saint Petersburg, Russia.
He develops complex web applications. He graduated from the physics faculty at Saint Petersburg State University. His journey into the world of software development started not so long ago—5 years ago.
His brother invited him to join the team that developed programs for American hospitals, as an HTML developer.
Ryzhikov started with developing a simple, static site for hospitals and then studied JavaScript, Ruby, and SQL and worked as a full-stack developer. In 5 years of work in the area of IT, he has worked in various projects and teams. He developed medical systems, dating sites, web mail (Yandex.Mail), and now he helps develop tools for developers at JetBrains.
Veer Shubhranshu Shrivastav is an Indian software developer working with Tata Consultancy Services since 2013 and is a former research intern at IIIT-Delhi. He has worked on different technologies, such as PHP, Moodle, jQuery, AngularJS, RequireJS, Android, Jasmine, Ionic, and so on, and also takes an interest in cryptography, network security, and database technologies. He has worked with various Indian IT start-ups, helping them as a software architect.
With his interest in the open source community, he developed a Pro*C library named CODBC, which is available at http://codbc.com. The library enables an object-oriented approach to connect C++ and Oracle Database.
Sergey Simonchik is a software developer living and working in Saint Petersburg, Russia. He is lucky because he has a wonderful wife and a kind cat. Sergey develops the WebStorm IDE at JetBrains. He is working on improving JavaScript unit testing support and other IDE features.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Preface
This book is about being a better JavaScript developer. So, throughout the chapters, you will not only learn about writing tests in the Jasmine 'idiom', but also about the best practices in writing software in the JavaScript language. It is about acknowledging JavaScript as a real platform for application development and leveraging