Search code examples
angularangular-test

How to exclude a file when compiling angular tests


I have an Angular 7 app that was created using the angular cli.

When I run ng-test, I want to exclude a file from the compilation process.

The file I want to exclude is "app/config/context.prod.ts"

I added the following section into my tsconfig.spec.ts file which is located in the src folder:

"exclude": [
    "app/config/context.prod.ts"
  ],

But it doesn't work. I tried adding this entry to the parent ts.config, located in the above folder:

"exclude": [
    "src/app/config/context.prod.ts"
  ]

Still no joy. With this file included, the build fails and I would expect it to fail when included but I don't want it to be.

ng build works. I got ng build to work by updating my tsconfig.app.ts file in exactly the same was as the tsconfig.spec.ts.

What am I doing wrong?

My tsconfig.spec.ts file:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "exclude": [
    "app/config/context.prod.ts"
  ],
  "files": [
    "test.ts",
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

Solution

  • The problem was a couple of our tests were importing the class contained within the file in question i.e:

    import { Context } from "../config/context.prod";
    

    Removed the import and it worked.