Skip to content

[Suggestion] - export declarations, imports, providers for reuse #3532

Closed
@DmitryEfimenko

Description

@DmitryEfimenko

This is not a bug, which is why I'm not providing any information required by "new issue template".

Often I run into this issue (and I see others run into that as well) when the code I write works in the browser but fails tests due to the fact that my imports/declarations/providers get out of sync in the module and in the tests. So I started writing something like this:

app.module.ts

...
export const declarations: any[] = [
  AppComponent
];

export const imports: any[] = [
  AppRoutes,
  BrowserModule,
  FormsModule,
  HttpModule
];

export const providers: any[] = [
  { provide: APP_BASE_HREF, useValue: '/' }
];

@NgModule({
  declarations: declarations,
  imports: imports,
  providers: providers,
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.spec.ts

...
import { declarations, imports, providers } from './app.module';

describe('AppComponent', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations, imports, providers
    });
    TestBed.compileComponents();
  });
...

So when I add another declaration, my tests won't be broken. Also, tests look much cleaner.

I'm wondering if there is any downside to doing things this way, and if not, why not default module generation template and tests template to this syntax?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions