Unit testing

in JavaScript world​

 

Dmytro Golysh

Agenda:

  • Why?
  • Approaches to write Unit tests
  • Frameworks:
  • Mocha, Chai, Sinon
  • Jasmine
  • Jest
  • Enzyme
  • Advanced: Protractor. Cucumber
  • Live coding: Jasmine, write angular functionality in TDD from scratch
  • Best practices

Find Software Bugs Early

Simplifies Integration

Provides Documentation

Approaches to write unit tests

Stub everything

describe('CompanyModule:: CompanyHome Component', () => {
    let sut;

    let store;
    let route;

    beforeEach(() => {
        store = jasmine.createSpyObj('store', [
            'dispatch'
        ]);

        route = {
            snapshot: {
                data: {
                    company: {}
                }
            }
        };

        sut = new CompanyHomeComponent(store, route);
    });

Use real dependencies

import { ActivatedRoute } from '@angular/router';
import { Store } from '@ngrx/store';

describe('CompanyModule:: CompanyHome Component', () => {
    let sut;

    beforeEach(() => {
        spyOn(store, 'dispatch');

        sut = new CompanyHomeComponent(Store, ActivatedRoute);
    });

Approaches to write unit tests

Use compiled component Dom as entry point

Enzyme (React)

TestBed (Angular2)

Approaches to write unit tests

Frameworks for unit testing in JavaScript

Enzyme

Jest

Suitable for any UI framework

Chai sinon `should` syntax

describe("A suite is just a function", function() {
  var a;

  it("and so is a spec", function() {
    a = true;

    a.should.be.true;
  });
});

Let's jump to real example

describe("A suite is just a function", function() {
  var a;

  it("and so is a spec", function() {
    a = true;

    expect(a).toBe(true);
  });
});

Let's jump to real example

Best friend of Angular

Let's jump to real example

Best friend of React

Let's jump to real example

Best friend of React

Enzyme

For testing DOM manipulation in component

E2E or Accepting testing

(Gherkin)

Best Practices 

Pair programming

Driver

Navigator

Knowledge sharing

Navigation Lists

Self time management.

Pomodoro

Tricks:

Ping-pong technique

Change driver after each 25 mins 

If the driver is silent, the navigator should intervene

Silent coding

Keep in mind:

KISS

Keep It Simple, Stupid

YAGNI

You Ain't Gonna Need It

DRY

Don't repeat yourself​

SOC

Separation of Concerns​

Live coding example

Write angular functionality in TDD from scratch

Q/A

Thanks

Unit testing in JavaScript world

By Dmytro Golysh

Unit testing in JavaScript world

For not JS devs

  • 603