forked from help-me-mom/ng-mocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-builder.spec.ts
162 lines (137 loc) · 4.11 KB
/
mock-builder.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* eslint-disable no-console */
import {
Component,
Directive,
Injectable,
NgModule,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { MockBuilder, ngMocks } from 'ng-mocks';
@Injectable()
class TargetService {
public readonly name = 'target';
}
@Directive({
selector: 'target',
})
class TargetDirective {}
@Component({
selector: 'target',
template: '{{ service.name }}',
})
class TargetComponent {
public constructor(public readonly service: TargetService) {}
}
@NgModule({
declarations: [TargetComponent, TargetDirective],
exports: [TargetComponent],
providers: [TargetService],
})
class TargetModule {}
describe('performance:MockBuilder', () => {
let timeStandard = 0;
let timeMockBuilder = 0;
let timeFasterBeforeEach = 0;
let timeFasterBeforeAll = 0;
jasmine.getEnv().addReporter({
jasmineDone: () => {
console.log('performance:MockBuilder');
console.log(`Time standard: ${timeStandard}`);
console.log(`Time MockBuilder: ${timeMockBuilder}`);
console.log(
`Ration standard / MockBuilder: ${
timeStandard / timeMockBuilder
}`,
);
console.log(`Time beforeEach: ${timeFasterBeforeEach}`);
console.log(
`Ratio standard / beforeEach: ${
timeStandard / timeFasterBeforeEach
}`,
);
console.log(`Time beforeAll: ${timeFasterBeforeAll}`);
console.log(
`Ratio beforeEach / beforeAll: ${
timeFasterBeforeEach / timeFasterBeforeAll
}`,
);
},
});
describe('standard', () => {
beforeAll(() => (timeStandard = Date.now()));
afterAll(() => (timeStandard = Date.now() - timeStandard));
beforeEach(() =>
TestBed.configureTestingModule({
imports: [TargetModule],
}).compileComponents(),
);
for (let i = 0; i < 100; i += 1) {
it(`#${i}`, () => {
const fixture = TestBed.createComponent(TargetComponent);
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('target');
});
}
});
describe('faster:MockBuilder', () => {
beforeAll(() => (timeMockBuilder = Date.now()));
afterAll(() => (timeMockBuilder = Date.now() - timeMockBuilder));
beforeEach(() =>
MockBuilder([TargetComponent, TargetService], TargetModule),
);
for (let i = 0; i < 100; i += 1) {
it(`#${i}`, () => {
const fixture = TestBed.createComponent(TargetComponent);
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('target');
});
}
});
describe('faster:beforeEach', () => {
ngMocks.faster();
beforeAll(() => (timeFasterBeforeEach = Date.now()));
afterAll(
() =>
(timeFasterBeforeEach = Date.now() - timeFasterBeforeEach),
);
beforeEach(() =>
MockBuilder([TargetComponent, TargetService], TargetModule),
);
for (let i = 0; i < 100; i += 1) {
it(`#${i}`, () => {
const fixture = TestBed.createComponent(TargetComponent);
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('target');
});
}
});
describe('faster:beforeAll', () => {
ngMocks.faster();
beforeAll(() => (timeFasterBeforeAll = Date.now()));
afterAll(
() => (timeFasterBeforeAll = Date.now() - timeFasterBeforeAll),
);
beforeAll(() =>
MockBuilder([TargetComponent, TargetService], TargetModule),
);
for (let i = 0; i < 100; i += 1) {
it(`#${i}`, () => {
const fixture = TestBed.createComponent(TargetComponent);
fixture.detectChanges();
expect(ngMocks.formatText(fixture)).toEqual('target');
});
}
});
it('ensures that faster is faster', () => {
// Usually, it is faster, but it is fine if we are down for 25%
expect(timeStandard / timeFasterBeforeEach).toBeGreaterThan(0.75);
// beforeEach should be definitely slower than beforeAll
expect(
timeFasterBeforeEach / timeFasterBeforeAll,
).toBeGreaterThan(0.75);
// without faster should be always slower
expect(timeMockBuilder / timeFasterBeforeEach).toBeGreaterThan(
0.75,
);
});
});