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
Node Web Development, Second Edition Rating: 0 out of 5 stars0 ratingsLearning jQuery Rating: 4 out of 5 stars4/5Learning Single-page Web Application Development Rating: 0 out of 5 stars0 ratingsJavascript Mastery: In-Depth Techniques and Strategies for Advanced Development Rating: 0 out of 5 stars0 ratingsLearning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsJavaScript Mobile Application Development Rating: 0 out of 5 stars0 ratingsjQuery Design Patterns Rating: 0 out of 5 stars0 ratingsThe Javascript Adventure Rating: 0 out of 5 stars0 ratingsJump Start Web Performance Rating: 0 out of 5 stars0 ratingsLearning Behavior-driven Development with JavaScript Rating: 5 out of 5 stars5/5The Best Javascript Rating: 0 out of 5 stars0 ratingsModern JavaScript Applications Rating: 0 out of 5 stars0 ratingsJavaScript Security Rating: 4 out of 5 stars4/5Your First Week With Node.js Rating: 0 out of 5 stars0 ratingsJavaScript: Tips and Tricks to Programming Code with Javascript: JavaScript Computer Programming, #2 Rating: 0 out of 5 stars0 ratingsDecoding JavaScript: A Simple Guide for the Not-so-Simple JavaScript Concepts, Libraries, Tools, and Frameworks (English Edition) Rating: 0 out of 5 stars0 ratingsJavascript - 50 functions and tutorial Rating: 4 out of 5 stars4/5Object-Oriented JavaScript - Third Edition Rating: 4 out of 5 stars4/5Node.js Web Development - Third Edition Rating: 2 out of 5 stars2/5JavaScript Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsJavaScript Programming: Pushing the Limits Rating: 0 out of 5 stars0 ratingsMastering jQuery UI Rating: 0 out of 5 stars0 ratingsJavaScript Everywhere Second Edition Rating: 0 out of 5 stars0 ratingsMastering the Art of Node.js Programming: Unraveling the Secrets of Expert-Level Programming Rating: 0 out of 5 stars0 ratingsDeploying Node.js Rating: 5 out of 5 stars5/5JavaScript for .NET Developers Rating: 0 out of 5 stars0 ratings
Internet & Web For You
Coding For Dummies Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer 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/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5UX/UI Design Playbook Rating: 4 out of 5 stars4/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Canva Tips and Tricks Beyond The Limits Rating: 3 out of 5 stars3/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5CISM Certified Information Security Manager Study Guide Rating: 4 out of 5 stars4/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5SEO For Dummies Rating: 4 out of 5 stars4/548 Really Useful Web Sites Rating: 5 out of 5 stars5/5Stop Asking Questions: How to Lead High-Impact Interviews and Learn Anything from Anyone Rating: 5 out of 5 stars5/5Learn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/5Ready, Set, Brand!: The Canva for Work Quickstart Guide Rating: 5 out of 5 stars5/5Get Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5Principles of Web Design Rating: 0 out of 5 stars0 ratingsSurveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5A Great Online Dating Profile: 30 Tips to Get Noticed and Get More Responses 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.
Support files, eBooks, discount offers, and morehttps://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