Skip to content

Commit 544257a

Browse files
committed
test(multiple): remove unnecessary compileComponents calls
Removes some calls to `compileComponents` that aren't necessary.
1 parent b9299cb commit 544257a

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

guides/using-component-harnesses.md

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This guide discusses the advantages of using component test harnesses and shows
1919
## Benefits of component test harnesses
2020

2121
There are two primary benefits to using the Angular Material component harnesses in your tests:
22-
22+
2323
1. Harnesses make tests easier to read and understand with straightforward APIs.
2424
2. Harnesses make tests more robust and less likely to break when updating Angular Material.
2525

@@ -48,8 +48,7 @@ let loader: HarnessLoader;
4848

4949
describe('my-component', () => {
5050
beforeEach(async () => {
51-
await TestBed.configureTestingModule({imports: [MyModule], declarations: [UserProfile]})
52-
.compileComponents();
51+
TestBed.configureTestingModule({imports: [MyModule], declarations: [UserProfile]});
5352
fixture = TestBed.createComponent(UserProfile);
5453
loader = TestbedHarnessEnvironment.loader(fixture);
5554
});
@@ -59,10 +58,10 @@ describe('my-component', () => {
5958
This code creates a fixture for `UserProfile` and then creates a `HarnessLoader` for that fixture.
6059
The `HarnessLoader` can then locate Angular Material components inside `UserProfile` and create
6160
harnesses for them. Note that `HarnessLoader` and `TestbedHarnessEnvironment` are loaded from
62-
different paths.
61+
different paths.
6362
6463
- `@angular/cdk/testing` contains symbols that are shared regardless of the environment your tests
65-
are in.
64+
are in.
6665
- `@angular/cdk/testing/testbed` contains symbols that are used only in Karma tests.
6766
- `@angular/cdk/testing/selenium-webdriver` (not shown above) contains symbols that are used only in
6867
Selenium WebDriver tests.
@@ -93,7 +92,7 @@ with your tests.
9392
9493
The example above retrieves all button harnesses and uses an array index to get the harness for a
9594
specific button. However, if the number or order of buttons changes, this test will break. You can
96-
write a less brittle test by instead asking for only a subset of harnesses inside `UserProfile`.
95+
write a less brittle test by instead asking for only a subset of harnesses inside `UserProfile`.
9796
9897
You can load harnesses for a sub-section of the DOM within `UserProfile` with the `getChildLoader`
9998
method on `HarnessLoader`. For example, say that we know `UserProfile` has a div,
@@ -111,17 +110,17 @@ You can also use the static `with` method implemented on all Angular Material co
111110
This method creates a `HarnessPredicate`, an object that filters loaded harnesses based on the
112111
provided constraints. The particular constraint options vary depending on the harness class, but all
113112
harnesses support at least:
114-
113+
115114
- `selector` - CSS selector that the component must match (in addition to its host selector, such
116115
as `[mat-button]`)
117116
- `ancestor` - CSS selector for a some ancestor element above the component in the DOM
118-
117+
119118
In addition to these standard options, `MatButtonHarness` also supports
120-
121-
- `text` - String text or regular expressions that matches the text content of the button
122-
119+
120+
- `text` - String text or regular expressions that matches the text content of the button
121+
123122
Using this method we could locate buttons as follows in our test:
124-
123+
125124
```ts
126125
it('should work', async () => {
127126
// Harness for mat-button whose id is 'more-info'.
@@ -164,7 +163,7 @@ which will cause the test to wait for `setTimeout`, `Promise`, etc.
164163
## Comparison with and without component harnesses
165164
166165
Consider an `<issue-report-selector>` component that you want to test. It allows a user to
167-
choose an issue type and display the necessary form create report for that issue type. You need a
166+
choose an issue type and display the necessary form create report for that issue type. You need a
168167
test to verify that when the user chooses an issue type the proper report displays. First consider
169168
what the test might look like without using component harnesses:
170169
@@ -173,10 +172,10 @@ describe('issue-report-selector', () => {
173172
let fixture: ComponentFixture<IssueReportSelector>;
174173

175174
beforeEach(async () => {
176-
await TestBed.configureTestingModule({
175+
TestBed.configureTestingModule({
177176
imports: [IssueReportSelectorModule],
178177
declarations: [IssueReportSelector],
179-
}).compileComponents();
178+
});
180179

181180
fixture = TestBed.createComponent(IssueReportSelector);
182181
fixture.detectChanges();
@@ -205,10 +204,10 @@ describe('issue-report-selector', () => {
205204
let loader: HarnessLoader;
206205

207206
beforeEach(async () => {
208-
await TestBed.configureTestingModule({
207+
TestBed.configureTestingModule({
209208
imports: [IssueReportSelectorModule],
210209
declarations: [IssueReportSelector],
211-
}).compileComponents();
210+
});
212211

213212
fixture = TestBed.createComponent(IssueReportSelector);
214213
fixture.detectChanges();
@@ -247,13 +246,13 @@ Notice that the test without harnesses directly uses CSS selectors to query elem
247246
`<mat-select>`, such as `.mat-select-trigger`. If the internal DOM of `<mat-select>` changes, these
248247
queries may stop working. While the Angular team tries to minimize this type of change, some
249248
features and bug fixes ultimately require restructuring the DOM. By using the Angular Material
250-
harnesses, you avoid depending on internal DOM structure directly.
249+
harnesses, you avoid depending on internal DOM structure directly.
251250
252251
In addition to DOM structure, component asynchronicity often offers a challenge when updating
253252
components. If a component changes between synchronous and asynchronous, downstream unit tests may
254253
break due to expectations around timing. Tests then require the addition or removal of some
255254
arcane combination of `whenStable`, `flushMicroTasks`, `tick`, or `detectChanges`. Component
256-
harnesses, however, avoid this problem by normalizing the asynchronicity of all component behaviors
255+
harnesses, however, avoid this problem by normalizing the asynchronicity of all component behaviors
257256
with all asynchronous APIs. When a test uses these harnesses, changes to asynchronicity become
258257
far more manageable.
259258

src/cdk-experimental/accordion/accordion.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ describe('CdkAccordionGroup', () => {
9494
await runAccessibilityChecks(fixture.nativeElement);
9595
});
9696

97-
beforeEach(async () => {
98-
await TestBed.configureTestingModule({
97+
beforeEach(() => {
98+
TestBed.configureTestingModule({
9999
providers: [provideFakeDirectionality('ltr'), _IdGenerator],
100100
imports: [BidiModule, AccordionGroupExample],
101-
}).compileComponents();
101+
});
102102

103103
fixture = TestBed.createComponent(AccordionGroupExample);
104104
});

src/cdk-experimental/listbox/listbox.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('CdkListbox', () => {
6666
TestBed.configureTestingModule({
6767
providers: [provideFakeDirectionality(opts?.textDirection ?? 'ltr')],
6868
imports: [BidiModule, ListboxExample],
69-
}).compileComponents();
69+
});
7070

7171
fixture = TestBed.createComponent(ListboxExample);
7272
const testComponent = fixture.componentInstance as ListboxExample;
@@ -99,7 +99,7 @@ describe('CdkListbox', () => {
9999
TestBed.configureTestingModule({
100100
providers: [provideFakeDirectionality('ltr')],
101101
imports: [BidiModule, DefaultListboxExample],
102-
}).compileComponents();
102+
});
103103

104104
const defaultFixture = TestBed.createComponent(DefaultListboxExample);
105105
defaultFixture.detectChanges();

src/cdk-experimental/radio/radio.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('CdkRadioGroup', () => {
4646
TestBed.configureTestingModule({
4747
providers: [provideFakeDirectionality(opts?.textDirection ?? 'ltr')],
4848
imports: [BidiModule, RadioGroupExample],
49-
}).compileComponents();
49+
});
5050

5151
fixture = TestBed.createComponent(RadioGroupExample);
5252
const testComponent = fixture.componentInstance;
@@ -86,7 +86,7 @@ describe('CdkRadioGroup', () => {
8686
TestBed.configureTestingModule({
8787
providers: [provideFakeDirectionality('ltr')],
8888
imports: [BidiModule, DefaultRadioGroupExample],
89-
}).compileComponents();
89+
});
9090

9191
const fixture = TestBed.createComponent(DefaultRadioGroupExample);
9292
fixture.detectChanges();

src/cdk/overlay/overlay.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,12 @@ describe('Overlay', () => {
361361

362362
overlayContainer.ngOnDestroy();
363363

364-
TestBed.resetTestingModule().configureTestingModule({
365-
imports: [OverlayModule],
366-
providers: [CustomErrorHandler, {provide: ErrorHandler, useExisting: CustomErrorHandler}],
367-
});
368-
369-
expect(() => TestBed.compileComponents()).not.toThrow();
364+
expect(() => {
365+
TestBed.resetTestingModule().configureTestingModule({
366+
imports: [OverlayModule],
367+
providers: [CustomErrorHandler, {provide: ErrorHandler, useExisting: CustomErrorHandler}],
368+
});
369+
}).not.toThrow();
370370
});
371371

372372
it('should keep the direction in sync with the passed in Directionality', () => {

0 commit comments

Comments
 (0)