From: Stephan Müller Date: Thu, 7 May 2020 15:13:57 +0000 (+0200) Subject: mgr/dashboard: Always use fast angular unit tests X-Git-Tag: v17.0.0~2381^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F34965%2Fhead;p=ceph.git mgr/dashboard: Always use fast angular unit tests This will replace the current approach having a fast testing workaround that didn't fully work on every test. Now using ng-bullet's optimization works with every test, therefore there is no need to keep the old configuration files and use the slow testing on Jenkins. Fixes: https://tracker.ceph.com/issues/45433 Signed-off-by: Stephan Müller --- diff --git a/src/pybind/mgr/dashboard/HACKING.rst b/src/pybind/mgr/dashboard/HACKING.rst index 6d0f47fab1379..4c6dc706467fd 100644 --- a/src/pybind/mgr/dashboard/HACKING.rst +++ b/src/pybind/mgr/dashboard/HACKING.rst @@ -315,10 +315,6 @@ to be visible in the rendered template. Running Unit Tests ~~~~~~~~~~~~~~~~~~ -Create ``unit-test-configuration.ts`` file based on -``unit-test-configuration.ts.sample`` in directory -``src/pybind/mgr/dashboard/frontend/src``. - Run ``npm run test`` to execute the unit tests via `Jest `_. diff --git a/src/pybind/mgr/dashboard/frontend/.gitignore b/src/pybind/mgr/dashboard/frontend/.gitignore index 5bb225251f09f..e7dc03521bf63 100644 --- a/src/pybind/mgr/dashboard/frontend/.gitignore +++ b/src/pybind/mgr/dashboard/frontend/.gitignore @@ -31,7 +31,6 @@ npm-debug.log testem.log /typings -/src/unit-test-configuration.ts # e2e /cypress/screenshots diff --git a/src/pybind/mgr/dashboard/frontend/package.json b/src/pybind/mgr/dashboard/frontend/package.json index 3250965721aef..1c4fe015a5593 100644 --- a/src/pybind/mgr/dashboard/frontend/package.json +++ b/src/pybind/mgr/dashboard/frontend/package.json @@ -30,15 +30,14 @@ "i18n:pull": "npx i18ntool pull -c i18n.config.json", "i18n:merge": "npx i18ntool merge -c i18n.config.json", "i18n:token": "npx i18ntool config token", - "test": "npm run test:config && jest --watch", - "test:ci": "npm run test:config && JEST_SILENT_REPORTER_DOTS=true jest --coverage --reporters jest-silent-reporter", - "test:config": "if [ ! -e 'src/unit-test-configuration.ts' ]; then cp 'src/unit-test-configuration.ts.sample' 'src/unit-test-configuration.ts'; fi", + "test": "jest --watch", + "test:ci": "JEST_SILENT_REPORTER_DOTS=true jest --coverage --reporters jest-silent-reporter", "e2e": "start-test 4200 'cypress open'", "e2e:ci": "start-test 4200 'cypress run -b chrome --headless'", "lint:tslint": "ng lint", "lint:prettier": "prettier --list-different \"{src,cypress}/**/*.{ts,scss}\"", "lint:html": "htmllint src/app/**/*.html && html-linter --config html-linter.config.json", - "lint:tsc": "npm run test:config && tsc -p src/tsconfig.app.json --noEmit && tsc -p tsconfig.spec.json --noEmit && tsc -p cypress/tsconfig.json --noEmit", + "lint:tsc": "tsc -p src/tsconfig.app.json --noEmit && tsc -p tsconfig.spec.json --noEmit && tsc -p cypress/tsconfig.json --noEmit", "lint": "npm run lint:tsc && npm run lint:tslint && npm run lint:prettier && npm run lint:html", "fix:prettier": "prettier --write \"{src,cypress}/**/*.{ts,scss}\"", "fix:tslint": "npm run lint:tslint -- --fix", diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-form/iscsi-target-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-form/iscsi-target-form.component.spec.ts index 3779144ffbef0..6fa48af6ec4a2 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-form/iscsi-target-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-form/iscsi-target-form.component.spec.ts @@ -141,26 +141,23 @@ describe('IscsiTargetFormComponent', () => { } ]; - configureTestBed( - { - declarations: [IscsiTargetFormComponent], - imports: [ - SharedModule, - ReactiveFormsModule, - HttpClientTestingModule, - RouterTestingModule, - ToastrModule.forRoot() - ], - providers: [ - i18nProviders, - { - provide: ActivatedRoute, - useValue: new ActivatedRouteStub({ target_iqn: undefined }) - } - ] - }, - true - ); + configureTestBed({ + declarations: [IscsiTargetFormComponent], + imports: [ + SharedModule, + ReactiveFormsModule, + HttpClientTestingModule, + RouterTestingModule, + ToastrModule.forRoot() + ], + providers: [ + i18nProviders, + { + provide: ActivatedRoute, + useValue: new ActivatedRouteStub({ target_iqn: undefined }) + } + ] + }); beforeEach(() => { fixture = TestBed.createComponent(IscsiTargetFormComponent); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/telemetry/telemetry.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/telemetry/telemetry.component.spec.ts index 783eeed810d0d..cc56046df62e8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/telemetry/telemetry.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/telemetry/telemetry.component.spec.ts @@ -47,20 +47,17 @@ describe('TelemetryComponent', () => { 'url' ]; - configureTestBed( - { - declarations: [TelemetryComponent], - imports: [ - HttpClientTestingModule, - ReactiveFormsModule, - RouterTestingModule, - SharedModule, - ToastrModule.forRoot() - ], - providers: i18nProviders - }, - true - ); + configureTestBed({ + declarations: [TelemetryComponent], + imports: [ + HttpClientTestingModule, + ReactiveFormsModule, + RouterTestingModule, + SharedModule, + ToastrModule.forRoot() + ], + providers: i18nProviders + }); describe('configForm', () => { beforeEach(() => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-form/nfs-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-form/nfs-form.component.spec.ts index 4b5ec0c909981..bb20702800b17 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-form/nfs-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-form/nfs-form.component.spec.ts @@ -21,29 +21,26 @@ describe('NfsFormComponent', () => { let httpTesting: HttpTestingController; let activatedRoute: ActivatedRouteStub; - configureTestBed( - { - declarations: [NfsFormComponent, NfsFormClientComponent], - imports: [ - HttpClientTestingModule, - ReactiveFormsModule, - RouterTestingModule, - SharedModule, - ToastrModule.forRoot(), - TypeaheadModule.forRoot() - ], - providers: [ - { - provide: ActivatedRoute, - useValue: new ActivatedRouteStub({ cluster_id: undefined, export_id: undefined }) - }, - i18nProviders, - SummaryService, - CephReleaseNamePipe - ] - }, - true - ); + configureTestBed({ + declarations: [NfsFormComponent, NfsFormClientComponent], + imports: [ + HttpClientTestingModule, + ReactiveFormsModule, + RouterTestingModule, + SharedModule, + ToastrModule.forRoot(), + TypeaheadModule.forRoot() + ], + providers: [ + { + provide: ActivatedRoute, + useValue: new ActivatedRouteStub({ cluster_id: undefined, export_id: undefined }) + }, + i18nProviders, + SummaryService, + CephReleaseNamePipe + ] + }); beforeEach(() => { const summaryService = TestBed.get(SummaryService); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts index 36039aa4d97e8..d8cbf7c6eac82 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/nfs/nfs-list/nfs-list.component.spec.ts @@ -33,21 +33,18 @@ describe('NfsListComponent', () => { summaryService['summaryDataSource'].next(data); }; - configureTestBed( - { - declarations: [NfsListComponent, NfsDetailsComponent], - imports: [ - BrowserAnimationsModule, - HttpClientTestingModule, - RouterTestingModule, - SharedModule, - ToastrModule.forRoot(), - TabsModule.forRoot() - ], - providers: [TaskListService, i18nProviders] - }, - true - ); + configureTestBed({ + declarations: [NfsListComponent, NfsDetailsComponent], + imports: [ + BrowserAnimationsModule, + HttpClientTestingModule, + RouterTestingModule, + SharedModule, + ToastrModule.forRoot(), + TabsModule.forRoot() + ], + providers: [TaskListService, i18nProviders] + }); beforeEach(() => { fixture = TestBed.createComponent(NfsListComponent); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login-password-form/login-password-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login-password-form/login-password-form.component.spec.ts index cf21e749ad70f..c336d26657e6f 100755 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login-password-form/login-password-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/login-password-form/login-password-form.component.spec.ts @@ -24,21 +24,18 @@ describe('LoginPasswordFormComponent', () => { let authStorageService: AuthStorageService; let authService: AuthService; - configureTestBed( - { - imports: [ - HttpClientTestingModule, - RouterTestingModule, - ReactiveFormsModule, - ComponentsModule, - ToastrModule.forRoot(), - SharedModule - ], - declarations: [LoginPasswordFormComponent], - providers: i18nProviders - }, - true - ); + configureTestBed({ + imports: [ + HttpClientTestingModule, + RouterTestingModule, + ReactiveFormsModule, + ComponentsModule, + ToastrModule.forRoot(), + SharedModule + ], + declarations: [LoginPasswordFormComponent], + providers: i18nProviders + }); beforeEach(() => { fixture = TestBed.createComponent(LoginPasswordFormComponent); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts index 10b5e3b776d72..3cff98667da42 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts @@ -31,20 +31,17 @@ describe('RoleFormComponent', () => { const routes: Routes = [{ path: 'roles', component: FakeComponent }]; - configureTestBed( - { - imports: [ - RouterTestingModule.withRoutes(routes), - HttpClientTestingModule, - ReactiveFormsModule, - ToastrModule.forRoot(), - SharedModule - ], - declarations: [RoleFormComponent, FakeComponent], - providers: i18nProviders - }, - true - ); + configureTestBed({ + imports: [ + RouterTestingModule.withRoutes(routes), + HttpClientTestingModule, + ReactiveFormsModule, + ToastrModule.forRoot(), + SharedModule + ], + declarations: [RoleFormComponent, FakeComponent], + providers: i18nProviders + }); beforeEach(() => { fixture = TestBed.createComponent(RoleFormComponent); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.spec.ts index 9574504f0fd2c..bd06b8e7dd2c1 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.spec.ts @@ -44,23 +44,20 @@ describe('UserFormComponent', () => { { path: 'users', component: FakeComponent } ]; - configureTestBed( - { - imports: [ - RouterTestingModule.withRoutes(routes), - HttpClientTestingModule, - ReactiveFormsModule, - ComponentsModule, - ToastrModule.forRoot(), - SharedModule, - ButtonsModule.forRoot(), - BsDatepickerModule.forRoot() - ], - declarations: [UserFormComponent, FakeComponent], - providers: i18nProviders - }, - true - ); + configureTestBed({ + imports: [ + RouterTestingModule.withRoutes(routes), + HttpClientTestingModule, + ReactiveFormsModule, + ComponentsModule, + ToastrModule.forRoot(), + SharedModule, + ButtonsModule.forRoot(), + BsDatepickerModule.forRoot() + ], + declarations: [UserFormComponent, FakeComponent], + providers: i18nProviders + }); beforeEach(() => { spyOn(TestBed.get(PasswordPolicyService), 'getHelpText').and.callFake(() => of('')); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-password-form/user-password-form.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-password-form/user-password-form.component.spec.ts index 3ac5f040fb094..a53f4451cb3bf 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-password-form/user-password-form.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-password-form/user-password-form.component.spec.ts @@ -22,21 +22,18 @@ describe('UserPasswordFormComponent', () => { let router: Router; let authStorageService: AuthStorageService; - configureTestBed( - { - imports: [ - HttpClientTestingModule, - RouterTestingModule, - ReactiveFormsModule, - ComponentsModule, - ToastrModule.forRoot(), - SharedModule - ], - declarations: [UserPasswordFormComponent], - providers: i18nProviders - }, - true - ); + configureTestBed({ + imports: [ + HttpClientTestingModule, + RouterTestingModule, + ReactiveFormsModule, + ComponentsModule, + ToastrModule.forRoot(), + SharedModule + ], + declarations: [UserPasswordFormComponent], + providers: i18nProviders + }); beforeEach(() => { fixture = TestBed.createComponent(UserPasswordFormComponent); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts index 8fa84bf65ae7d..1302f7a3abee6 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/navigation/navigation.component.spec.ts @@ -1,10 +1,10 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; -import { configureTestSuite } from 'ng-bullet'; import { MockModule } from 'ng-mocks'; import { of } from 'rxjs'; +import { configureTestBed } from '../../../../testing/unit-test-helper'; import { Permission, Permissions } from '../../../shared/models/permissions'; import { AuthStorageService } from '../../../shared/services/auth-storage.service'; import { @@ -49,23 +49,21 @@ describe('NavigationComponent', () => { let component: NavigationComponent; let fixture: ComponentFixture; - configureTestSuite(() => { - TestBed.configureTestingModule({ - declarations: [NavigationComponent], - imports: [MockModule(NavigationModule)], - providers: [ - { - provide: AuthStorageService, - useValue: { - getPermissions: jest.fn(), - isPwdDisplayed$: { subscribe: jest.fn() } - } - }, - { provide: SummaryService, useValue: { subscribe: jest.fn() } }, - { provide: FeatureTogglesService, useValue: { get: jest.fn() } }, - { provide: PrometheusAlertService, useValue: { alerts: [] } } - ] - }); + configureTestBed({ + declarations: [NavigationComponent], + imports: [MockModule(NavigationModule)], + providers: [ + { + provide: AuthStorageService, + useValue: { + getPermissions: jest.fn(), + isPwdDisplayed$: { subscribe: jest.fn() } + } + }, + { provide: SummaryService, useValue: { subscribe: jest.fn() } }, + { provide: FeatureTogglesService, useValue: { get: jest.fn() } }, + { provide: PrometheusAlertService, useValue: { alerts: [] } } + ] }); beforeEach(() => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts index 37be86346dd0c..5aacb19d9cf70 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/rbd-mirroring.service.spec.ts @@ -27,13 +27,10 @@ describe('RbdMirroringService', () => { executing_tasks: [{}] }; - configureTestBed( - { - providers: [RbdMirroringService], - imports: [HttpClientTestingModule] - }, - true - ); + configureTestBed({ + providers: [RbdMirroringService], + imports: [HttpClientTestingModule] + }); beforeEach(() => { service = TestBed.get(RbdMirroringService); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts index 86b8c06e503a2..dd73fe9cd0e26 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/api/settings.service.spec.ts @@ -11,13 +11,10 @@ describe('SettingsService', () => { const exampleUrl = 'api/settings/something'; const exampleValue = 'http://localhost:3000'; - configureTestBed( - { - providers: [SettingsService], - imports: [HttpClientTestingModule] - }, - true - ); + configureTestBed({ + providers: [SettingsService], + imports: [HttpClientTestingModule] + }); beforeEach(() => { service = TestBed.get(SettingsService); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts index 9912d67bea5e4..f1fbcd38baba9 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts @@ -39,14 +39,11 @@ describe('ModuleStatusGuardService', () => { expect(router.url).toBe(urlResult); }; - configureTestBed( - { - imports: [RouterTestingModule.withRoutes(routes)], - providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }], - declarations: [FooComponent] - }, - true - ); + configureTestBed({ + imports: [RouterTestingModule.withRoutes(routes)], + providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }], + declarations: [FooComponent] + }); beforeEach(() => { service = TestBed.get(ModuleStatusGuardService); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-manager.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-manager.service.spec.ts index 6c797d50633fb..be83c323c704b 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-manager.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/task-manager.service.spec.ts @@ -35,12 +35,9 @@ describe('TaskManagerService', () => { let summaryService: any; let called: boolean; - configureTestBed( - { - providers: [TaskManagerService, { provide: SummaryService, useClass: SummaryServiceMock }] - }, - true - ); + configureTestBed({ + providers: [TaskManagerService, { provide: SummaryService, useClass: SummaryServiceMock }] + }); beforeEach(() => { taskManagerService = TestBed.get(TaskManagerService); diff --git a/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts b/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts index e7d8b6fdd5c10..fa8a326bbbe7d 100644 --- a/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts +++ b/src/pybind/mgr/dashboard/frontend/src/testing/unit-test-helper.ts @@ -1,9 +1,10 @@ import { LOCALE_ID, TRANSLATIONS, TRANSLATIONS_FORMAT, Type } from '@angular/core'; -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; import { AbstractControl } from '@angular/forms'; import { By } from '@angular/platform-browser'; import { I18n } from '@ngx-translate/i18n-polyfill'; +import { configureTestSuite } from 'ng-bullet'; import { BsModalRef } from 'ngx-bootstrap/modal'; import { TableActionsComponent } from '../app/shared/datatable/table-actions/table-actions.component'; @@ -18,29 +19,9 @@ import { AlertmanagerNotificationAlert, PrometheusRule } from '../app/shared/models/prometheus-alerts'; -import { _DEV_ } from '../unit-test-configuration'; - -export function configureTestBed(configuration: any, useOldMethod?: boolean) { - if (_DEV_ && !useOldMethod) { - const resetTestingModule = TestBed.resetTestingModule; - beforeAll((done) => - (async () => { - TestBed.resetTestingModule(); - TestBed.configureTestingModule(configuration); - // prevent Angular from resetting testing module - TestBed.resetTestingModule = () => TestBed; - })() - .then(done) - .catch(done.fail) - ); - afterAll(() => { - TestBed.resetTestingModule = resetTestingModule; - }); - } else { - beforeEach(async(() => { - TestBed.configureTestingModule(configuration); - })); - } + +export function configureTestBed(configuration: any) { + configureTestSuite(() => TestBed.configureTestingModule(configuration)); } export class PermissionHelper { diff --git a/src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample b/src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample deleted file mode 100644 index 74dbf2c0792a7..0000000000000 --- a/src/pybind/mgr/dashboard/frontend/src/unit-test-configuration.ts.sample +++ /dev/null @@ -1 +0,0 @@ -export const _DEV_ = false; diff --git a/src/pybind/mgr/dashboard/run-frontend-unittests.sh b/src/pybind/mgr/dashboard/run-frontend-unittests.sh index 38c47f8e4d779..6195750147f19 100755 --- a/src/pybind/mgr/dashboard/run-frontend-unittests.sh +++ b/src/pybind/mgr/dashboard/run-frontend-unittests.sh @@ -14,19 +14,8 @@ fi npm run build -- --prod --progress=false || failed=true # Unit Tests -config='src/unit-test-configuration.ts' -if [ -e $config ]; then - mv $config ${config}_old -fi -cp ${config}.sample $config - npm run test:ci || failed=true -rm $config -if [ -e ${config}_old ]; then - mv ${config}_old $config -fi - # Linting npm run lint --silent if [ $? -gt 0 ]; then