Angular Unit Test – Get started

スポンサーリンク

Angular Test

  • Unit Test – Single unit of code, Component, Service
  • Integration Test – More than Unit Test, less than end to end test
  • End to End Test – Frontend, WebServer, DB

Basic Tool

NameDescription
KarmaTest Runner with Browser (Karma)
JasmineJavaScript Unit Test (Jasmine)

How to run test

ng test

This commands runs all spec.ts files (project test files)

If you see package.json, following command also defined

"scripts": {
   "test": "ng test"
}

test as ng test, so we can use this

npm test

If you want to run only one file (test file)

ng test --include xxxx.spec.ts

First Test

Let’s write simple test.

app/first-test.spec.ts


describe('Hello Htest', () => {

    let hello;

    beforeEach(() => {
        hello = {};
    })

    it('it should be true', () => {
        hello.a = true;

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

This is jasmine test sytle.

describe is test root. beforeEach is pre-preparation for test (call everytime to start test)

it indicates test function.

To run this, you can see result in browser (karma)

TypeScript
スポンサーリンク
Professional Programmer2

コメント