JavaScript testing.

Test driven development (TDD)

Dmytro Golysh

Find Software Bugs Early

Simplifies Integration

Provides Documentation

Okay, we have tests.

What next?

Continuous Integration

Frameworks for unit testing in JavaScript

Enzyme

Jest

E2E or Accepting testing

(Gherkin)

Jasmine's syntax

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

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

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

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;
  });
});

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

TDD

Add a test

Run test and see how it fails

Fail First

Write the code

Run and check works

Refactor code and repeat

Benefits

  • It makes coding more fun
  • Coverage
  • Simpler design
  • Simple decomposition of complex tasks
  • Code as documentation
  • No extra code
  • More testable code - More readable code
  • Rare debugging
  • More concentrated

Think Different

How start ? 

Good knowledge of test framework

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:

TDD is good for tasks with many conditions and bad for spikes

KISS

Keep It Simple, Stupid

YAGNI

You Ain't Gonna Need It

Do it

Angular Unit testing. TDD

By Dmytro Golysh

Angular Unit testing. TDD

For Junior MP

  • 874