environment.ts не работает при тестировании Angular 2

Пытаюсь написать простой тест в Angular 2, но получаю ошибку для environment.ts, как показано ниже

ОШИБКА в ./web/environments/environment.ts Сборка модуля завершилась неудачно: Ошибка: web \ environment \ environment.ts отсутствует в компиляции TypeScript. Убедитесь, что он находится в вашем tsconfig через свойство 'files' или 'include'.

app.component.ts

import { Component} from '@angular/core';
import { environment } from '../environments/environment';

@Component({
  selector: 'web-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'Test App'; 

  constructor() {
    console.log(environment);
  }  
}

app.component.spec.ts

import { AppComponent } from './app.component';

describe('AppComponent', () => {
  it(`should 1+1`, () => {
    expect(1 + 1).toEqual(2);  //Success
  });
  it('should have component ', () => {
    const component = new AppComponent();  //Throws error
    // expect(component).toBeTruthy();
  });
});

Любое предложение ?


person Dipak Telangre    schedule 12.09.2018    source источник
comment
это потому, что ng serve и ng test используют разные tsconfig.json. Можете ли вы опубликовать свой tsconfig.spec.json?   -  person Poul Kruijt    schedule 12.09.2018
comment
У меня просто tsconfig.json, но не tsconfig.spec.json. Я создал проект с использованием Angular CLI.   -  person Dipak Telangre    schedule 12.09.2018
comment
посмотрите в папку src   -  person Poul Kruijt    schedule 12.09.2018


Ответы (1)


создайте свой **.spec.ts следующим образом:

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { AppModule } from './app/app.module';
import { APP_BASE_HREF } from '@angular/common';

describe('App', () => {
    beforeEach(() => {
        jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
        TestBed.configureTestingModule({
            declarations: [
                AppComponent
            ],
            imports: [
                AppModule
            ],
            providers: [
                { provide: APP_BASE_HREF, useValue: '/' }
            ]
        });
    });

    it('should have component', async(() => {
        let fixture = TestBed.createComponent(AppComponent);
        let app = fixture.debugElement.componentInstance;
        expect(app).toBeTruthy();
    }));
});
person Shashikant Devani    schedule 12.09.2018
comment
Получение Error: Illegal state: Could not load the summary for directive AppComponent. - person Dipak Telangre; 12.09.2018
comment
Внося изменения в ответ, надеюсь, это поможет вам - person Shashikant Devani; 12.09.2018