Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CLI tests and enums

I am struggeling with Angular CLI's test framework and enum types. I am creating an enum like this (in someenum.ts):

const enum SomeEnum {
  Val0,
  Val1
}

And using it like this (in app.component.ts):

private someEnum = SomeEnum.Val0;

If I run this code using ng serve it runs ok, and the js generated defines it as:

this.someEnum = 0 /* Val0 */;

which is how it should be.

However, when I try to execute tests using ng test it fails with the compiler error:

ERROR in src/app/app.component.ts(10,14): error TS2304: Cannot find name 'SomeEnum'.

I think that the issue is something with the typescript configuration for test environment, but I can't figure it out, does someone have a good explanation of this?

like image 487
Rune G Avatar asked Oct 17 '25 16:10

Rune G


1 Answers

I just had the same problem. You can fix it by exporting your enums. For example:

languageEnums.ts

export const enum Languages {
  ENGLISH = "en-US",
  SWEDISH = "sv-SE"
}

app.component.ts

import { Languages } from '../assets/constants';

...

private language = Languages.ENGLISH; 

I created a new constants folder in the assets folder which contains all the enums. One enum per file! You don't need to import them into the app.module.ts or the .spec files.

After that the errors where fixed.

like image 191
theoretisch Avatar answered Oct 20 '25 08:10

theoretisch