Skip to content

Commit c09da0b

Browse files
devversionjelbourn
authored andcommitted
fix(schematics): generated spec files not working (#12842)
If someone generates components with Angular Material by using our schematics and runs `ng test` afterwards, the tests will fail because: * The needed Material modules are not being imported * No animation module is specified (either `Noop` or `BrowserAnimationsModule`) * `fakeAsync` is used to run `compileComponents`. `async()` should be used to wait for `compileComponents` (see the default CLI component schematic) Fixes #12778
1 parent f6cded8 commit c09da0b

File tree

5 files changed

+103
-36
lines changed

5 files changed

+103
-36
lines changed

src/lib/schematics/address-form/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
2-
import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { ReactiveFormsModule } from '@angular/forms';
3+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
4+
import {
5+
MatButtonModule,
6+
MatCardModule,
7+
MatInputModule,
8+
MatRadioModule,
9+
MatSelectModule,
10+
} from '@angular/material';
311

412
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
513

614
describe('<%= classify(name) %>Component', () => {
715
let component: <%= classify(name) %>Component;
816
let fixture: ComponentFixture<<%= classify(name) %>Component>;
917

10-
beforeEach(fakeAsync(() => {
18+
beforeEach(async(() => {
1119
TestBed.configureTestingModule({
12-
declarations: [ <%= classify(name) %>Component ]
13-
})
14-
.compileComponents();
20+
declarations: [ <%= classify(name) %>Component ],
21+
imports: [
22+
NoopAnimationsModule,
23+
ReactiveFormsModule,
24+
MatButtonModule,
25+
MatCardModule,
26+
MatInputModule,
27+
MatRadioModule,
28+
MatSelectModule,
29+
]
30+
}).compileComponents();
31+
}));
1532

33+
beforeEach(() => {
1634
fixture = TestBed.createComponent(<%= classify(name) %>Component);
1735
component = fixture.componentInstance;
1836
fixture.detectChanges();
19-
}));
37+
});
2038

2139
it('should compile', () => {
2240
expect(component).toBeTruthy();

src/lib/schematics/dashboard/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
1-
2-
import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { LayoutModule } from '@angular/cdk/layout';
2+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
3+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
4+
import {
5+
MatButtonModule,
6+
MatCardModule,
7+
MatGridListModule,
8+
MatIconModule,
9+
MatMenuModule,
10+
} from '@angular/material';
311

412
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
513

614
describe('<%= classify(name) %>Component', () => {
715
let component: <%= classify(name) %>Component;
816
let fixture: ComponentFixture<<%= classify(name) %>Component>;
917

10-
beforeEach(fakeAsync(() => {
18+
beforeEach(async(() => {
1119
TestBed.configureTestingModule({
12-
declarations: [ <%= classify(name) %>Component ]
13-
})
14-
.compileComponents();
20+
declarations: [< %= classify(name) %> Component],
21+
imports: [
22+
NoopAnimationsModule,
23+
LayoutModule,
24+
MatButtonModule,
25+
MatCardModule,
26+
MatGridListModule,
27+
MatIconModule,
28+
MatMenuModule,
29+
]
30+
}).compileComponents();
31+
}));
1532

33+
beforeEach(() => {
1634
fixture = TestBed.createComponent(<%= classify(name) %>Component);
1735
component = fixture.componentInstance;
1836
fixture.detectChanges();
19-
}));
37+
});
2038

2139
it('should compile', () => {
2240
expect(component).toBeTruthy();

src/lib/schematics/nav/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1+
import { LayoutModule } from '@angular/cdk/layout';
2+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
4+
import {
5+
MatButtonModule,
6+
MatIconModule,
7+
MatListModule,
8+
MatSidenavModule,
9+
MatToolbarModule,
10+
} from '@angular/material';
111

2-
import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { MatSidenavModule } from '@angular/material/sidenav';
412
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
513

614
describe('<%= classify(name) %>Component', () => {
715
let component: <%= classify(name) %>Component;
816
let fixture: ComponentFixture<<%= classify(name) %>Component>;
917

10-
beforeEach(fakeAsync(() => {
18+
beforeEach(async(() => {
1119
TestBed.configureTestingModule({
12-
imports: [MatSidenavModule],
13-
declarations: [<%= classify(name) %>Component]
14-
})
15-
.compileComponents();
20+
declarations: [<%= classify(name) %>Component],
21+
imports: [
22+
NoopAnimationsModule,
23+
LayoutModule,
24+
MatButtonModule,
25+
MatIconModule,
26+
MatListModule,
27+
MatSidenavModule,
28+
MatToolbarModule,
29+
]
30+
}).compileComponents();
31+
}));
1632

33+
beforeEach(() => {
1734
fixture = TestBed.createComponent(<%= classify(name) %>Component);
1835
component = fixture.componentInstance;
1936
fixture.detectChanges();
20-
}));
37+
});
2138

2239
it('should compile', () => {
2340
expect(component).toBeTruthy();

src/lib/schematics/table/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
2-
import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
3+
import { MatPaginatorModule, MatSortModule, MatTableModule } from '@angular/material';
34

45
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
56

67
describe('<%= classify(name) %>Component', () => {
78
let component: <%= classify(name) %>Component;
89
let fixture: ComponentFixture<<%= classify(name) %>Component>;
910

10-
beforeEach(fakeAsync(() => {
11+
beforeEach(async(() => {
1112
TestBed.configureTestingModule({
12-
declarations: [ <%= classify(name) %>Component ]
13-
})
14-
.compileComponents();
13+
declarations: [ <%= classify(name) %>Component ],
14+
imports: [
15+
NoopAnimationsModule,
16+
MatPaginatorModule,
17+
MatSortModule,
18+
MatTableModule,
19+
]
20+
}).compileComponents();
21+
}));
1522

23+
beforeEach(() => {
1624
fixture = TestBed.createComponent(<%= classify(name) %>Component);
1725
component = fixture.componentInstance;
1826
fixture.detectChanges();
19-
}));
27+
});
2028

2129
it('should compile', () => {
2230
expect(component).toBeTruthy();

src/lib/schematics/tree/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
2-
import { fakeAsync, ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { MatButtonModule, MatIconModule, MatTreeModule } from '@angular/material';
33

44
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
55

66
describe('<%= classify(name) %>Component', () => {
77
let component: <%= classify(name) %>Component;
88
let fixture: ComponentFixture<<%= classify(name) %>Component>;
99

10-
beforeEach(fakeAsync(() => {
10+
beforeEach(async(() => {
1111
TestBed.configureTestingModule({
12-
declarations: [ <%= classify(name) %>Component ]
13-
})
14-
.compileComponents();
12+
declarations: [ <%= classify(name) %>Component ],
13+
imports: [
14+
MatButtonModule,
15+
MatIconModule,
16+
MatTreeModule,
17+
]
18+
}).compileComponents();
19+
}));
1520

21+
beforeEach(() => {
1622
fixture = TestBed.createComponent(<%= classify(name) %>Component);
1723
component = fixture.componentInstance;
1824
fixture.detectChanges();
19-
}));
25+
});
2026

2127
it('should compile', () => {
2228
expect(component).toBeTruthy();

0 commit comments

Comments
 (0)