Git Product home page Git Product logo

node-tests's Introduction

node-tests

Mocha Test 101

  1. Install mocha: npm i mocha --save-dev (-dev: this lib use only on dev mood)
  2. Create basic function for test (utils.js)
  3. Create test class for that (utils.test.js)
  4. Edit test line in package.json: "test": "mocha */.test.js"
  5. Run npm test
  6. To auto restart tests use: nodemon --exec "npm test"
  7. In commit 5: "test-watch":"nodemon --exec "npm test"" Run npm run test-watch
  8. Adding Assertion library:(mjackson/expect) to install this use: npm install [email protected] --save-dev

Using commonJs

it('should add two numbers', ()=>{
  var res = utils.add(55,5);

  if(res !== 60){
		throw new Error(`Expect 60 but got ${res}`);
	}

});

Using expect

it('sholud add one number',()=>{
    var res = utils.squre(3);

    expect(res).toBe(9).toBeA('number');

});

Using expect with objects

** when we working with object we need to use toEqual method .for objects toBe not working**

expect({name:"Shehan"}).toEqual({name:"Shehan"});
expect({name:"Shehan"}).toNotEqual({name:"Gamage"});
  1. Asynchronous code testing with mocha. when we using async function we need to tell mocha that function is a async function. for that we can use done()

    module.exports.asynAdd = (a, b, callback)=>{
      setTimeout(()=>{
        callback(a+b);
      },1000);
    };
    
    it('should async add two numbers',(done)=>{
        utils.asynAdd(3, 4, (sum)=>{
          expect(sum).toBe(7).toBeA('number');
          done();
        });
    });
    
  2. Testing Express applications with supertest and mocha.

    1. install Express and create server.js.
    2. install (supertest) npm install supertest --save-dev
    3. Create server.test.js file.
  3. Organizing test with describe(). using describe() we can organize out testing code

    describe('sometext', ()=>{
      //add test functions
      //add test functions
      //add test functions
      });
    
  4. Test spies

  5. Using rewire with spies npm install rewire --save-dev

node-tests's People

Contributors

shehangamage avatar

Watchers

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