0

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
  • Can you show us your test code? – MChaker Feb 12 '17 at 13:52
  • 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 Feb 12 '17 at 17:27

1 Answers1

1

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.

kind user
  • 40,029
  • 7
  • 67
  • 77
  • mm but why? This is an TS compilation error not a logic error or runtime exception.. – Chen Reuven Feb 12 '17 at 17:27
  • @user2415300 Edited my post. – kind user Feb 12 '17 at 18:06
  • so what u say is that the type script compile error will NOT catch when u run testing, just on Build/running the app? – Chen Reuven Feb 13 '17 at 05:25
  • 1
    @user2415300 If you don't make a test case for it - nope. – kind user Feb 13 '17 at 09:17
  • It all depends on what you're expecting it looks like num could be a string containing only numeric values. If that's the case you should use `string` as the type. If it could be a string or a number use the the type `string | number` and so on. In other words the type should reflect the domain of values you expect it to take at runtime. – Aluan Haddad Feb 17 '17 at 08:39
  • @user2415300, Thanks For Answer, – Chen Reuven Feb 18 '17 at 07:02
  • @AluanHaddad u didnt understand my question was: why ng test ignore TS compilation error? – Chen Reuven Feb 18 '17 at 07:03
  • I did understand it. I just thought it was already explained by others. Unless what you're testing is a declaration file, tests always test runtime behavior they run after compilation has occurred. You can prevent typescript from emitting code if there is a compilation error with the `--noEmitOnError` flag. Otherwise you're testing the compiler, not your code. This is just a generally how statically typed languages work and it makes a lot of sense. For example if you use ts-node to run your tests, you actually will get a failure when you run your test command but it will come from compilation – Aluan Haddad Feb 18 '17 at 07:15