Coder Social home page Coder Social logo

calculator_unit-tests's Introduction

calculator_unit-tests (Javascript ( Node.js))

The application show how testers have to write unit tests.
There is calculator class in [index.js] than we need to coverage unit tests.
It has two methods [add] - for summarising and [multiply] - for multiplication.

The application needs to have the following dependencies:

npm --version 6.4.1
node --version 8.11.4

Deploy

$ npm install

Before start the program is supposed to run the command [npm install].
It downloads needed modules and prepars the programm for start.
There is a list of modules and their versions:

chai: ^4.1.2
chai-spies: ^1.0.0
mocha: ^5.2.0
mochawesome: ^3.0.3
eslint: ^5.6.0
nyc: ^13.1.0

Structure

Folders:

  • test - there are all files and folders connected with tests;
  • [node_modules] - will be created after [npm install] command. There will be stored all additional modules;
  • [reports] - will be created after the first run of [npm test] command. There will be stored all data that are need for report;
  • [.nyc-output] - will be created after the first run of [npm test] command. There will be stored all data for [nyc] module.

Files in root:

  • index.js - main file in which you can find our Calculator class;
  • .eslintignore - all folders and files that should not be indexed by [eslint] are listed here;
  • .eslintrc.js - all rulles for [eslint] are placed there;
  • .gitignore - all folders and files that should not be indexed by [git] are listed here;
  • package.json - includes all data for [npm]
  • README.md - readme file with special information about the application and git-syntacs.

Usage

There are some scripts in package.json that helps us.

  • 1.[npm run linter] - check style and fix some marks in code. Afret have finished it write errors in console.
  • 2.[npm test] - run tests and at the end print results of tests coverage the Calculator class.

Author

Dzmitry_Karneyenka, Republic of Belarus, Minsk

N.B.

beforeEach

Don't put [beforeEach()] in [forEach], because [beforeEach()] multiply on array elements length
that causes running [beforeEach()] block before every it equals array.length times.
It is wrong way. It is better to place [beforeEach()] outside [forEach] but inside [describe] block.

If you use two and more [beforeEach()] - -blocks, they are run in the order of one by one before every it block.

chai-spies

How to make checks with spy?

  1. make spy:
    const someSpy = chai.spies.on(object, 'nameOfMethod');

  2. use chai.expect for making assertion:
    chai.expect(someSpy).to.have.been.called.with(data);
    see documentation

chai method throw

How to make checks for Errors?

  1. Create new function that will include tested method that have to throw an Error with data.
    e.g. const actualResult = () => calculator.add(data);

  2. Send it into check's method. further steps it does itself.It makes listener, runs method and catches an Error.
    e.g. chai.expect(actualResult).to.throw(TypeError);
    see documentation

Use spread operator - (...) for:

  1. Sending arguments into method from array;
    e.g. obj.functionName(...[a, b, c, d]) spread converts into obj.functionName(a, b, c, d)

  2. making array from arguments in functions;
    e.g. function Name(...arg). If we call the function as obj.Name(a, b, c, d), so arg will be an array [a, b, c, d]
    see documentation

calculator_unit-tests's People

Contributors

dkarneyenka avatar karneyenkadzmitry avatar

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.