Giter Site home page Giter Site logo

diego-ec / angular-jasmine-karma-demo Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 2.0 261 KB

Basic demo with just a couple of tests in Angular, using Jasmine and Karma.

JavaScript 5.86% TypeScript 83.79% HTML 9.11% SCSS 1.24%
testing bemit hexagonal-architecture angular typescript jasmine karma

angular-jasmine-karma-demo's Introduction

AngularJasmineKarmaDemo

This project was generated with Angular CLI version 12.1.3.


Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.


Running unit tests

Run ng test to execute the unit tests via Karma. You can run the tests even without the server running 馃槻


About this project

It is intended to be an introduction to unit testing with jasmine, providing information on the most basic concepts and sample tests. This is a work in progress, so, content will be added whenever possible.


About Jasmine tests

Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests. It's developed by Pivotal Labs and is open-source.

Karma: is a test runner, it allows us to run the test suite. It's developed by Google.

Both, Jasmine and Karma, come preinstalled and preconfigured in an Angular project created with Angular CLI.

The Unit Tests must be coded in component-name.spec.ts files, so that Karma can find them.

There are a couple of example tests with comments on the item and add-item components.


Test doubles

Son un t茅rmino gen茅rico que hace referencia a cualquier caso en el que se reemplaza un objeto de producci贸n con otro con el 煤nico objetivo de probar el c贸digo.

According with Gerard Meszaros there are at least 5 kinds of doubles https://en.wikipedia.org/wiki/Test_double

  • Test stub: used for providing the tested code with "indirect input".
  • Mock object: used for verifying "indirect output" of the tested code, by first defining the expectations before the tested code is executed.
  • Test spy: used for verifying "indirect output" of the tested code, by asserting the expectations afterwards, without having defined the expectations before the tested code is executed. It helps in recording information about the indirect object created.
  • Fake object: used as a simpler implementation, e.g. using an in-memory database in the tests instead of doing real database access.
  • Dummy object: used when a parameter is needed for the tested method but without actually needing to use the parameter.

AAA Pattern: sections of a Unit Test

  1. Arrange: code required to setup a specific test. Here objects would be created, mocks setup, ...
    fixture = TestBed.createComponent(AddItemComponent);
    component = fixture.componentInstance;
  2. Act: which should be the invocation of the method being tested
    component.form.controls['name'].setValue('foo');
    component.form.controls['description'].setValue('bar');
    component.form.controls['price'].setValue('33');
  3. Assert: check whether the expectations were met.
    expect(component.form.valid).toBeTruthy();

Jasmine assertion functions

  • expect(array).toContain(member);
  • expect(fn).toThrow(string);
  • expect(fn).toThrowError(string);
  • expect(instance).toBe(instance);
  • expect(mixed).toBeDefined();
  • expect(mixed).toBeFalsy();
  • expect(mixed).toBeNull();
  • expect(mixed).toBeTruthy();
  • expect(mixed).toBeUndefined();
  • expect(mixed).toEqual(mixed);
  • expect(mixed).toMatch(pattern);
  • expect(number).toBeCloseTo(number, decimalPlaces);
  • expect(number).toBeGreaterThan(number);
  • expect(number).toBeLessThan(number);
  • expect(number).toBeNaN();
  • expect(spy).toHaveBeenCalled();
  • expect(spy).toHaveBeenCalledTimes(number);
  • expect(spy).toHaveBeenCalledWith(鈥rguments);

Jasmine functions that can be run before or after tests

To help a test suite DRY up any duplicated setup and teardown code, Jasmine provides the global beforeEach, afterEach, beforeAll, and afterAll functions:

  • beforeAll: is called only once before all the specs in describe are run
    • e.g.: to createt the TestBed
  • afterAll: is called only once after all the specs in describe are run
    • e.g.: to run some shared teardown after each of the specs in the describe in which it is called.
  • beforeEach: is called once before each spec in the describe in which it is called
    • This functionality is very useful for running the common code in the application, lie data initialization.
  • afterEach: is called once after each spec in the describe in which it is called
    • Generally used to reset/clean up purposes at the end of specs

Jasmine methods

  • TestBed: modulo de angular que nos permite manipular las pruebas y configurarlas.
  • SpyOn is a Jasmine feature that allows dynamically intercepting the calls to a function nd change its result.

Testing Cases / How to test...

Testing component creation

  • You can see an example at item.component.spec.ts
  • this is a very basic test, but it's well documented.

Testing form validation

You can see an example at add-item.component.spec.ts

Testing sharing data from parent to child (using @Input)

You can see an example at item-detail.component.spec.ts

Testing calling a service from a component

You can see an example at users.component.spec.ts

RouterLink

// TODO: pending to code

Testing service against rest api

// TODO: pending to code

  • no tengo claro si es un test muy util, porque se mockea la base de datos y da la impresi贸n de que siempre funciona.
  • imports: [HttpClientModule]

Testing service against real rest api

// TODO: pending to code

angular-jasmine-karma-demo's People

Contributors

diego-ec avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.