logo_ironhack_blue 7

LAB | Intro to testing with Jasmine

img

Introduction

The scope of this exercise is to get familiar with the Jasmine testing framework and learn how to write basic tests.

Up to this point, you have worked with tests in almost every lab. Tests were written prior and you would develop solution based on tests. Today you will take a bit different turn, and develop tests for already "working" solution. The goal is to get basic understanding of how testing frameworks work in general and to get familiar with some of the most used testing approaches.

A friendly reminder - you will be developing tests for just one small piece of code, just one function better saying. In this case, we are talking about you developing unit tests, since you will be testing just one "unit" or a small component of your (imaginary) whole application. To learn more about unit (and integration) tests, check the extra resources section.

Requirements

expect(Function_To_Test(parameter)).toEqual(Expected_Result);
  • Fork this repo
  • Then clone this repo.

Submission

  • Upon completion, run the following commands:
$ git add .
$ git commit -m "done"
$ git push origin master
  • Create Pull Request so your TAs can check up your work.

Test, test, test!

In this exercise, your goal is to rewrite the simple example tests that were written just with the console.log() method, and instead, implement them using the Jasmine framework.

Here is the example of a single test, which was written using the console.log() and then re-written using the proper Jasmine syntax and specific Jasmine methods:

function centsToDecimals(centValue) {
  if (typeof centValue !== 'number' || isNaN(centValue)) {
    return undefined;
  }

  let result = centValue / 100;

  return result.toFixed(2) + '$';
}

// Test Specs:

// Description:
// function centsToDecimals()

// Tests

// 1: Should return undefined when the parameter passed is a string
console.log('-->  should return undefined when parameter passed is a string');

// you  are  given  the following:
console.log(centsToDecimals('abcdef') === undefined);

// this is the Jasmine expression that needs to  be updated accordingly:
expect(Function_To_Test(parameter)).toEqual(Expected_Result);

// solution: replace placeholders with corresponding function/value
expect(centsToDecimals('abcdef')).toEqual(undefined);

Following the same logic, go through the rest of tests, making sure they are passing in the end.

Getting Started

After cloning the repository, open the file spec/cents-to-decimals.spec.js. You will be working in this file.

The initial skeleton of the test suites is already set in place; however, you will notice that each test assertion it(...) has a placeholder saying Function_To_Test, instead of calling the function to test. As well, you will notice that each test assertion is missing the expected result value in the toEqual() (or toBe()) block and is instead having a placeholder Expected_Result.


Use the first example as the starting point and a reference for writing the rest of the tests.

// Use this test suite as a starting point/reference.
it('Returns undefined when parameter passed is a string', () => {
  expect(centsToDecimals('abcdef')).toEqual(undefined);
});

😉 In case you need a bit more help, check the test specs file spec/hello-ironhacker.spec.js as an additional reference.

Run The Tests

To run the tests, open SpecRunner.html in the browser.

Suites

describe() Your Tests

The describe function is used for grouping related specs (tests), typically each test file has one at the top level.

The string parameter is for naming the collection of specs.

Write A Test - it()

The it() matcher defines a single spec (test). It should contain one or more expectations that test the state of the code.

Additionally, depending on the scenario that you are testing for you may decide to use different matchers, such as: toBe, toEqual, etc. You can see the full list of Jasmine matchers here.

Extra Resources

Happy coding! ❤️