Giter Club home page Giter Club logo

testing-javascript's Introduction

Introduction to Unit Testing in JavaScript

Installation

First, clone this repository. For this lesson we will be using a repository set up to run Node.js, which means you'll be seeing some commands we haven't discussed that start with npm. We aren't going to dig into what this means today, but ultimately it allows us to pull in some powerful tools outside of our basic html/css/js files.

git clone [email protected]:turingschool-examples/testing-javascript.git  

Once, you have it on your computer, cd into the directory in the terminal and run the following command:

npm install

This command installs all of the dependencies we need to run tests.

Getting Started

We'll be modifying two files as we complete these exercises:

  • implentation.js
  • test.js

Just to verify that everything works, there should be a first example in place.

// implementation.js
function square(num) {
  return num * num;
}
// test.js
describe('square', function () {

  it('returns the square of a number', function () {
    var result = square(2);

    assert.equal(result, 4);
  });

});

Running a Test

From your command line, make sure you are navigated into this repository's root. Run the following:

npm test test.js

What is that doing? npm test is the command, and test.js is the single argument we are passing it. We are telling the computer to run tests, specifically, the tests is test.js file.

Together

Let's write a few cases together.

  • addTwo();
  • multiplyTwo();
  • largerNumber();

Your Turn

Pro Tip: Feel free to google to figure out how to implement some of this logic.

Subtract

Create a function that subtracts the second argument from the first argument passed in. For instance:

subtract(5, 3);

=> 2

subtract(12, 2);

=> 10

Longer Word

Create a function that finds which argument contains the longer word. For instance:

longerWord("hi", "hello");

=> "hello"

longerWord("hey", "hi");

=> "hey"

Reverse String

Create a function that reverses a string that is passed in as a parameter. For instance:

reverseString('reverse');

=> 'esrever'

Try to do this without using the .reverse() function in JavaScript.

Longest Word

Create a function that can find the longest word in a sentence. For instance:

findLongestWord('We had a great time!');

=> 'great'

Time Convert

Create a function that converts a number of minutes passed in as a parameter to the hours and minutes separated by a colon. For instance:

convertTime(126);
'2:06'

convertTime(45);
=> '0:45'

FizzBuzz

FizzBuzz is a classic programming program that a lot of programmers apparently can't do.

We're going to write a function called fizzBuzz() that will behave as follows with the each rule taking precedence over all previous rules:

  • If we pass it a number, it will return that number.
  • If we pass a number that is a multiple of 3, it will return 'Fizz'.
  • If we pass it a number that is a multiple of 5, it will return 'Buzz'.
  • If we pass it a number that is a multiple of both 3 and 5, it will return 'FizzBuzz'.

Resources

Dive Deeper

testing-javascript's People

Contributors

allpurposename avatar ameseee avatar breethomas avatar damwhit avatar martensonbj avatar nfosterky avatar rdavid1099 avatar robbiejaeger avatar rrgayhart avatar stevekinney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.