Title Text

@webdave_de

Testing

"That seperates the wannabe from a pro"

BUT!!!!

No tests in JavaScript natively!

Testframeworks

How?

think in user stories

Let's start

 

As a (user role)

I want to (do this)

so that (goal)

suite

describe(title, function)

describe('A suite', () => {
    ...
});

spec

it(title, function)

describe('A suite', () => {
  it('contains n specs', () => {
     ...
  });
});

expectations

expect(actual).matcher()

describe('A suite', () => {
  it('contains n specs with n expectations', () => {
    expect(true).toBe(true);
  });
});

matcher

describe('A suite', () => {
  it('contains n specs with n expectations', () => {

    expect(true).toEqual(true);
    expect(true).toBe(true);
    
    let foo: string = 'bar';
    expect(foo).toBeDefined();
    expect(bar).not.toBeDefined();

    expect(true).toBeTruthy();
    expect(false).toBeFalsy();

    expect('Hi Hamburg').toMatch('Hamburg');

    expect(array).toContain(value);

    expect(func()).toThrow(error);
  });
});

setup / teardown

  • beforeAll

  • beforeEach

  • afterEach

  • afterAll

//  is called only once before all the specs in describe are run
beforeAll(() => { ... })

//  is called once before each spec in the describe in which it is called
beforeEach(() => { ... })

//  is called once after each spec
afterEach(() => { ... })

//  is called after all specs finish
afterAll(() => { ... })

Doku

https://jasmine.github.io/2.5/introduction

Got it?

Testrunner

KARMA

https://karma-runner.github.io/1.0/index.html

extensions

//  npm install jasmine-expect karma-jasmine-matchers --save-dev


//  karma.conf.js
module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: [
        'jasmine', 
        'angular-cli', 
        'jasmine-matchers' //<=
    ],
    plugins: [
      require('karma-jasmine'),
      require('karma-jasmine-matchers'), //<=
      require('karma-chrome-launcher'),
      require('karma-remap-istanbul'),
      require('angular-cli/plugins/karma')
    ],
...
  });
};

https://github.com/JamieMason/Jasmine-Matchers

Code(?)

https://github.com/web-dave/testing_meetup_hh

recomention

Angular 2 — Testing Guide

Gerard Sans

https://medium.com/google-developer-experts/angular-2-testing-guide-a485b6cb1ef0

Testen deiner AngularJS Anwendung mit Jasmine

Robin Böhm

https://angularjs.de/artikel/angularjs-test

THX

@webdave_de

unit testing

By David Muellerchen

unit testing

  • 798