Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore spec files and mock files in production build in Angular 6

I m trying to compile in production my angular application and I m receiving this error:

Cannot determine the module for class TranslateMockPipe in /src/testing/translate.service.spec.ts! Add TranslateMockPipe to the NgModule to fix it.

this TranslateMockPipe class is a common class that I declared and I m using it in all my test files in order to avoid repeating the same line of code in all the test cases

the code of the class:

@Pipe({ name: "translate" })
class TranslateMockPipe {
    transform = (ss) => ss
}

I m using it in my test like this:

TestBed.configureTestingModule({
            declarations: [
                DemoComponent,
                TranslateMockPipe
            ]
})

If I run the project everything goes well, and my test pass without issues but fail when I try to compile for production

How I can ignore this kind of fake classes for production build?

like image 854
Ricardo Avatar asked Nov 29 '18 21:11

Ricardo


2 Answers

You can read it up on this ticket but the simplest ways should be:

  • add a path where you have your testing utilites e.g. "./app/shared/tests/" and in your tsconfig.json add:

    "exclude": [
      "./app/shared/tests/*.ts"
    ]
    

or

  • Create a module that is not imported to any other module except Testbed modules using it for testing purposes and include mock, stubs etc...
like image 133
Lucho Avatar answered Oct 26 '22 11:10

Lucho


If you are using Ionic Framework with Angular, you need to update the exclude section in tsconfig.app.json so it lists the files that appear in the error message (or most likely, all similarly named files).

Error:

Cannot determine the module for class TranslateMockPipe in /src/testing/translate.service.spec.ts! Add TranslateMockPipe to the NgModule to fix it.

tsconfig.app.json

...

 "exclude": [
   "src/testing/*.service.spec.ts"
 ]

...
like image 1
Johnathan J. Avatar answered Oct 26 '22 11:10

Johnathan J.