Неожиданная директива 'InformationComponent', импортированная модулем 'DynamicTestModule'. Пожалуйста, добавьте аннотацию @NgModule - PullRequest
0 голосов
/ 21 января 2020

Я знаю, что есть аналогичная ссылка на ответ по Stackoverflow, но это не решило мою проблему, поэтому я объясняю свой случай, я проверил ng, но не смог с приведенной ниже ошибкой:

Failed: Unexpected directive 'InformationComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
error properties: Object({ ngSyntaxError: true })
Error: Unexpected directive 'InformationComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
    at syntaxError (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
    at http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:20068:1
    at <Jasmine>
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:20043:1)
    at JitCompiler._loadModules (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25824:1)
    at JitCompiler._compileModuleAndAllComponents (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25813:1)
    at JitCompiler.compileModuleAndAllComponentsAsync (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25775:1)
    at CompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9877/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:244:1)
    at TestingCompilerImpl.compileModuleAndAllComponentsAsync (http://localhost:9877/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:159:1)
    at TestBedViewEngine.compileComponents (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3623:1)
Error: Unexpected directive 'InformationComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
error properties: Object({ ngSyntaxError: true })
    at syntaxError (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:2175:1)
    at http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:20068:1
    at <Jasmine>
    at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:20043:1)
    at JitCompiler._loadModules (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25824:1)
    at JitCompiler._compileModuleAndAllComponents (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25813:1)
    at JitCompiler.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/node_modules/@angular/compiler/fesm2015/compiler.js:25772:1)
    at CompilerImpl.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/platform-browser-dynamic.js:232:1)
    at TestingCompilerImpl.compileModuleAndAllComponentsSync (http://localhost:9877/_karma_webpack_/node_modules/@angular/platform-browser-dynamic/fesm2015/testing.js:151:1)
    at TestBedViewEngine._initIfNeeded (http://localhost:9877/_karma_webpack_/node_modules/@angular/core/fesm2015/testing.js:3645:1)
Error: Expected undefined to be truthy.
    at <Jasmine>
    at UserContext.<anonymous> (http://localhost:9877/_karma_webpack_/src/app/modules/verification/components/results/result/result.component.spec.ts:27:23)
    at ZoneDelegate.invoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
    at ProxyZoneSpec.onInvoke (http://localhost:9877/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)

Я много чего перепробовал, но ничего не работает, у меня все еще есть эта ошибка.

uploadcomponent.spe c .ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

    import { UploadComponent } from './upload.component';
    import { UploaderComponent } from '../uploader/uploader.component';
    import { AnalyseComponent } from '../analyse/analyse.component';

    describe('UploadComponent', () => {
      let component: UploadComponent;
      let fixture: ComponentFixture<UploadComponent>;

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ UploadComponent, UploaderComponent, AnalyseComponent ]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(UploadComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });

information-component.ts

import { Component, OnInit, Input, OnChanges } from '@angular/core';
import * as InformationMessages from '../../../json/InformationMessages.json';

@Component({
  selector: 'app-information',
  templateUrl: './information.component.html',
  styleUrls: ['./information.component.css']
})
export class InformationComponent implements OnInit {

  constant: any = (InformationMessages as any).default;

  constructor() { }

  @Input() fileName: string;
  @Input() result: string;

  ngOnInit() {
  }

}

Надеюсь, вы мне поможете

app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {ErrorHandler, NgModule} from '@angular/core';

import {AppComponent} from './app.component';
import {AppRoutingModule} from './app-routing.module';
import {BreadcrumbModule} from 'primeng/breadcrumb';
import {HttpClientModule} from '@angular/common/http';
import {ErrorComponent} from './components/error/error.component';
import {HomeComponent} from './components/home/home.component';
import {MenuComponent} from './components/menu/menu.component';
import {HeadComponent} from './components/head/head.component';
import {ArianeComponent} from './components/ariane/ariane.component';
import {PlanComponent} from './components/plan/plan.component';
import {SeparatorComponent} from './components/separator/separator.component';
import {FooterComponent} from './components/footer/footer.component';
import {EsignatureErrorHandler} from './handlers/esignature-error-handler';
import {ToastrModule} from 'ngx-toastr';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {FaIconLibrary, FontAwesomeModule} from '@fortawesome/angular-fontawesome';
import {faArrowRight} from '@fortawesome/free-solid-svg-icons';


@NgModule({
  declarations: [
    AppComponent, HomeComponent, MenuComponent, HeadComponent, ArianeComponent,
    PlanComponent, SeparatorComponent, FooterComponent, ErrorComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BreadcrumbModule,
    HttpClientModule,
    BrowserModule,
    BrowserAnimationsModule,
    FontAwesomeModule,
    ToastrModule.forRoot(),
  ],
  providers: [
    {
      provide: ErrorHandler,
      useClass: EsignatureErrorHandler,
    },
  ],
  exports: [
  ],

  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(library: FaIconLibrary) {
    library.addIcons(faArrowRight);
  }
}

1 Ответ

0 голосов
/ 21 января 2020

Обратите внимание, что вы можете импортировать только модули, а не компоненты. В файле *.module.ts в разделе @NgModule в разделе imports убедитесь, что вы не пытаетесь импортировать InformationComponent.

...