Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
React Components

You're reading from   React Components Explore the power of React components for cutting-edge web development

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781785889288
Length 182 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Christopher Pitt Christopher Pitt
Author Profile Icon Christopher Pitt
Christopher Pitt
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Testing with assertions


Assertions are the spoken/written language constructs made in the code. They look and function similar to how I've been speaking about them. In fact, most tests are structured in the same way we've been describing tests:

  • Given some pre-conditions

  • When something happens

  • We see some post-conditions

The first two points happen as we create objects and components and call their various methods. Assertions happen in the third point. Node.js ships with a few basic assertion methods, which we can use to write our first tests:

import assert from "assert";

assert(
    rendered.match(/<h1 data-reactid=".*">Home<\/h1>/g)
);

There are quite a few assertion methods we can use:

  • assert(condition), assert.ok(condition)

  • assert.equal(actual, expected)

  • assert.notEqual(actual, expected)

  • assert.strictEqual(actual, expected)

  • assert.notStrictEqual(actual, expected)

  • assert.deepEqual(actual, expected)

  • assert.notDeepStrictEqual(actual, expected)

  • assert.throws(function, type)

You can add an...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime