Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma + Jasmine (Angular Testing): Where should I define mock classes and test variables?

Let's take the following example:

const listDefinition: any = {
    module: "module",
    service: "service",
    listname: "listname"
};

@Component(...)
class MockTreeExpanderComponent extends TreeExpanderComponent {...}

class MockListConfigurationsService extends ListConfigurationsService {...}

describe('ColumnsListConfigurationsComponent Test cases', () => {
    let fixture: ComponentFixture<ColumnsListConfigurationsComponent>;
    let component: ColumnsListConfigurationsComponent;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [
                ComponentToTest,
                MockTreeExpanderComponent
            ],
            providers: [
                 { provide: TreeListConfigurationService, useClass: MockTreeListConfigurationService }
            ]
        });
        fixture = TestBed.createComponent(ComponentToTest);
        component = fixture.componentInstance;
        component.listDefinition = listDefinition;
        fixture.detectChanges();
    });
});

As you can see, I have a Mock component (MockListViewerGridComponent) and service (ListConfigurationsService), a configuration variable (listDefinition) and the fixture and component that I want to test.

My question is about performance and test memory management:

  1. The variables instantiated inside of describe method will be destroy as soon as the all tests inside of describe finishes?
  2. Should I declare all variables and mock classes/services inside of describe?
  3. Should I create a fixture in beforeEach or beforeAll? By doing that, I will have performance improvement?

Thank you!

like image 774
Ricardo Rocha Avatar asked Dec 13 '18 11:12

Ricardo Rocha


People also ask

What is Jasmine testing framework and how do you use it for Angular Unit testing?

Jasmine is a behavior development testing framework. Unit tests are written using Jasmine and are run to see if individual parts of an application are working correctly. As a result, unit tests will either pass or fail depending on if the code is working correctly or has a bug.

What is the role of Jasmine and karma in Angular testing?

Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.

What is difference between karma and Jasmine?

Jasmine can be classified as a tool in the "Javascript Testing Framework" category, while Karma is grouped under "Browser Testing". "Can also be used for tdd " is the primary reason why developers consider Jasmine over the competitors, whereas "Test Runner" was stated as the key factor in picking Karma.


2 Answers

I have an answer for point #3

let me share my own experience with the beforeAll.

We were using the beforeEach fixture creation in one of our application which were taking almost 12 min to build entire app. For each unit of work.

we have to build the app

1st time client side

2nd time on review branch and

3rd time release branch

which were taking almost 30 min (combined) to commit unit of work.

Now multiple this time with the head count of resources, bingo as a team we were wasting lot of time in only the app build process.

at some point we replaced the beforeEach with beforeAll with the help of this article, and it worked. We were able to reduce the build time by around 80%.

Short answers for point #1 and #2

1) Yes

2) Its better to create separate mock service. you can provide the object of it with the help of stub inside your before all block, and keep all the mocks in same folder.

providers: [
      { provide: XService, useClass: XServiceStub }
]
like image 114
Kedar9444 Avatar answered Sep 25 '22 05:09

Kedar9444


In my projects, I always create mock classes and test variable in the separate file. And after importing them to my spec file I declare them to the beforeEach block:

  1. The main benefit is that the before each IT block it resets its previous reference of value.
  2. Unused variables are removed from memory which results in a performance improve.

I hope this helps

like image 37
siddharth shah Avatar answered Sep 22 '22 05:09

siddharth shah