Unit testing

in JavaScript

Theory

What and how to test

Theory > What and how to test

What to test

Unit tests :

testing components, functions and/or classes in isolation.

 

Integration tests :

testing several parts in relation to each other.

 

End-to-end / Functional / Browser tests :

testing the application as a whole.

Theory > What and how to test

How to test

Unit tests :

e.g. Mocha, Jasmine, Jest, Chai, Sinon, Karma, Qunit

 

Integration tests :

Same tools as unit tests.

 

End-to-end / Functional / Browser tests :

Selenium, Protractor

Theory > white box or black box

White box or Black box?

Is a testing method in which the internal structure, the actual code is known bij the tester

 

Used for mainly unit testing and partly by integration tests

 

Done by Developers

 

Programming knowledge and implementation knowledge needed

Is a testing method in which the internal structure, the actual code is NOT known bij the tester

 

Used for mainly end-to-end tests and integration tests

 

Done by (external) Testers

 

Programming knowledge and implementation knowledge is NOT needed

Theory > Unit tests

Unit tests

- Feedback for the developer while coding

- Focus on small units: e.g. class, method, function

- Tested in isolation, dependencies are mocked

- Should be a good bug report

- Should be simple

- Should be fast

Theory > Unit tests

Unit tests

Structure

 

 

describe('Unit to test', function() {
 
    it('should do something', function() {
      //arrange...
      var dummyData = { foo: 'bar' };
      var expected = 'the result we want';
    
      //act...
      var result = functionUnderTest(dummyData);
    
      //assert...
      expect(result).to.equal(expected);
    });
 
});

Theory > Unit tests

Unit tests

Structure

 

 

Mock :

Code that replaces the original dependency  e.g. http call, service, helper and returns 0, "", null, undefined

 

Stub

As mock but it returns actual code, like a function, object and/or methods

 

Spy :

Intercepts the call to the dependency and returns information about how often and with what properties it was called.

Example

What and how to test

Example > What and how to test

What to test

Example > What and how to test

What to test

Setup with:

Yeoman

generator foundation webapp

 

https://www.npmjs.com/package/generator-fountain-webapp

 

Reading material

https://sites.google.com/site/unclebobconsultingllc/

 

Robert Martin (Uncle Bob)

- Clean code

- Ideal Programmer

Unit testing in JavaScript

By tietyk

Unit testing in JavaScript

Theory and practical examples about unit testing in Javascript

  • 1,235