juliasut / testing_TS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

testing_TS

Structure of a unit test: AAA principles:

  • arrange

  • act

  • assert

    Setup Teardown

Jest matchers are functions that assisst us in comparing actual and expected object.

.toBe - for primitiive types .toEqual - to compare objects

Each test should be independent from other test.

Parametrized tests can use it.each() for multiple input examples.

Fast

  • Unit tests should be fast

Isolated

  • Unit tests should be isolated from one another
  • No shared state with other tests
  • Order of running tests does not matter

(contradicts Fast principle - more time for individual test setup)

Repeatable

  • Unit tests should have same result for same input (if a test writes to a db, it should always clean up; random/Date values - mocked)

Self-validating

  • Unit tests should have clear results: pass/fail

Thourough

  • Unit tests should cover all the cases/scenarios/edge cases
  • 100 code coverage does not automatically imply thouroughness

Jest hooks help reduce code duplication (beforeEach, afterEach; beforeAll, afterAll in integration tests). Good practice to put our hooks inside describe blocks.

We can test for errors, by wrapping in expectError function, or using an arrow function inside expect, or using try catch block.

Under the hood jest uses a library called Istanbul for covarage.

About


Languages

Language:TypeScript 100.0%