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
:
beforeEach
or beforeAll
? By doing that, I will have performance improvement?Thank you!
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.
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.
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.
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 }
]
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:
IT
block it resets its previous reference of value.I hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With