Giter Club home page Giter Club logo

js-tdd-writing-unit-tests-lab's Introduction

Writing Unit Tests Lab

Learning Goals

  • Follow a test-driven development process for writing code
  • Write a unit test using Jest

Introduction

Time for some practice! In this lab, you'll write your own unit test and get it to pass by following a test-driven development workflow.

As before, we'll use Jest as our test framework. We'll be writing the tests in the src/__tests__/utils.test.js file and the application code in the src/utils.js file.

Fork and clone this lesson, then run npm install to install the dependencies.

Instructions

Our word game needs another feature. We'll give users double the amount of points if their word is a palindrome: a word that reads the same forwards and backwards, like "mom" or "racecar".

You should implement a function isPalindrome by following test-driven development that accepts a string as an argument and returns true if the string is a palindrome, and false if it isn't.

Try testing some known use-cases first: isPalindrome("racecar") should return true and isPalindrome("car") should return false.

Follow the same workflow we did in the previous lesson:

  1. Understand the feature we're building
  2. Translate the feature into a test specification
  3. Write and implement code that passes the test
  4. Clean up and refactor
  5. Repeat!

Then, consider adding tests for some of these edge cases:

  • Should return true for words that are a combination of uppercase and lowercase letters
  • Should return false for an empty string
  • Bonus Should throw an error if input has any non-alphabetic characters (hint: look into the .toThrow Jest matcher). You can check if a word has only alphabetic characters with this regular expression code: /^[A-Za-z]+$/.test(word)
  • Bonus Should throw an error if input that isn't a string (hint: look into the .toThrow Jest matcher)

Since you'll be writing the tests, this lesson doesn't have any tests for you to run to check your implementation. You can see both the completed tests and the working solution in the solution branch of this repository.

Resources

js-tdd-writing-unit-tests-lab's People

Contributors

alveem avatar lizbur10 avatar tracyk10 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.