Skip to Content
Mastering JavaScript Functional Programming - Second Edition
book

Mastering JavaScript Functional Programming - Second Edition

by Federico Kereki
January 2020
Intermediate to advanced
470 pages
11h 13m
English
Packt Publishing
Content preview from Mastering JavaScript Functional Programming - Second Edition

Testing pure functions

Given the characteristics of pure functions that we have already described, most of your unit tests could simply be the following:

  • Call the function with a given set of arguments.
  • Verify that the results match what you expected.

Let's start with a couple of simple examples. Testing the isOldEnough() function would have been more complex than we needed for the version that required access to a global variable. On the other hand, the last version, isOldEnough3(), which didn't require anything because it received two parameters, is simple to test: 

describe("isOldEnough", function() {  it("is false for people younger than 18", () => {    expect(isOldEnough3(1978, 1963)).toBe(false);  });    it("is true for people older than ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering JavaScript Functional Programming

Mastering JavaScript Functional Programming

Federico Kereki

Publisher Resources

ISBN: 9781839213069Supplemental Content