Git Product home page Git Product logo

js-unit-testing's Introduction

#Javascript Unit Testing

##Directory Setup

Build out a directory that is set up for testing:

$ mkdir js-unit-testing
$ cd js-unit-testing
$ git init
$ npm init -y
$ echo 'node_modules'>>.gitignore
$ npm install -g mocha
$ npm install --save-dev chai

Your package.json should include the following modification:

"scripts": {
  "test": "./node_modules/.bin/mocha -w"
}
$ touch main.js
$ mkdir test
$ cd test
$ touch test.js

Run the command $ npm test to make sure the Mocha and Chai npm modules are imported correctly. You should see 0 passing (2ms) in your terminal.

Note: Running the command mocha will also run your tests, with one minor difference. Can you see what the difference is?

##main.js: Set Up Export Module Prepare to add code to the module.exports object by adding the following to main.js:

module.exports = {
  //code goes here
};

##test/test.js: Import Modules & Write Tests At the top of test.js, import the following:

  • the Chai expect library
  • your main.js file

Write tests for the functions greeting and reduce. Expected inputs and outputs are defined, along with the specific conditions to be tested.

The Chai expect documentation will be crucial in understanding the syntax to use when writing these tests.

###Write tests for the function "greeting":

Input: name, string (ex: 'Jennie')

Output: greeting that incorporates string input (ex: 'Hello, Jennie')

Test for:

  • accepts only one argument
  • input is a string
  • output is a string
  • output is expected string

###Write tests for the function "reduce":

Input: array of integers (ex: [3, 8, 12, 2])

Output: integer representing sum of all integers in array (ex: 25)

Test for:

  • input is an array
  • input array includes only numbers
  • output is a number
  • output is a sum of all elements

###Once You've Finished Once you've written code that passes your tests, fork & clone the javascript-test-coverage repo and begin working through those exercises.

Be aware that test/test.js imports and uses the assert rather expect Chai library. Feel free to dig around the Chai docs to learn and use the assert style, or simply delete all code in index.js and start from scratch using the expect library used for the above exercise.

##Example Test/Function Pair for Reference

From test/test.js:

'use strict';

const code = require('../main.js');
const expect = require('chai').expect;

describe("Hello World", () => {
  it("should return 'Hello, World!'", () => {
    expect(code.helloWorld()).to.equal('Hello, World!');
  });

  it("should return a string", () => {
    expect(code.helloWorld()).to.be.a('string');
  });
});

From main.js:

module.exports = {
  helloWorld: () => {
    return 'Hello, World!';
  }
}

js-unit-testing's People

Watchers

James Cloos 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.