0
votes

i create some TS error in my test (spec file) such: let num: number = "dsds".

  • i run the ng test and the test is successful... why?
  • i expected to get some TS error in the terminal
  • What i need to do to enable this behavior (the TS catch error) when running tests?
  • Details:
    • @angular/cli: 1.0.0-beta.31
    • node: 6.9.2
    • os: win32 x64
    • @angular/common: 2.4.7
    • @angular/compiler: 2.4.7
    • @angular/core: 2.4.7
    • @angular/forms: 2.4.7
    • @angular/http: 2.4.7
    • @angular/platform-browser: 2.4.7
    • @angular/platform-browser-dynamic: 2.4.7
    • @angular/router: 3.4.7
    • @angular/cli: 1.0.0-beta.31
    • @angular/compiler-cli: 2.4.7
1
Can you show us your test code? - MChaker
sure but is really obviuos one because it related to the TS comipilition error. it('should create the app', async(() => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; let num: number = "dsds"; expect(app).toBeTruthy(); })); - Chen Reuven

1 Answers

1
votes

If you want the ng test to catch it while compiling, you have to write a separate test case for this element. You can use, e.g. RegExp object to check if it is or it's not a number.

it('should be a number', () => {
  expect(component.num).toMatch(/\d+/);
});


Edit:

If you declare a following variable:

someNumberVar: number = "thisIsNotANumber";

the TypeScript will catch it as an error, but while compiling the app, not while testing! You can check it out by yourself, set this variable and start the compilation, by typing ng serve in the console. You will receive following error:

Type 'string' is not assignable to type 'number'.

In case if you would like to test your application, you have to write a separate test cases for each element you want to test, as I mentioned above.