0

Trying to write simple test in Angular 2 but getting error for environment.ts as below

ERROR in ./web/environments/environment.ts Module build failed: Error: web\environments\environment.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

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();
  });
});

Any suggestion ?

Dipak Telangre
  • 1,792
  • 4
  • 19
  • 46

1 Answers1

0

create your **.spec.ts like this:

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();
    }));
});
Shashikant Devani
  • 2,298
  • 1
  • 12
  • 25