Reputation: 7215
I am using angular-cli with Karma, and I need to exclude a particular file from being compiled when running unit tests.
The file in question is referenced in the error below, where it cannot be compiled due to a module not being present it relies on.
This file is used by other projects where the module will be present and so compiles fine, its just in this particular project it does not need compilation but still needs to be exported.
My directory structure is as follows:
index.ts
- /src
- /testing
In my index.ts file I have a single export:
export * from './src/core';
TS source files under my testing
folder are getting picked up by the compiler, and I am unsure why as I am not importing any files under this folder in files under my src
folder or anywhere else from what I can tell.
I want to prevent the compiler from picking up my testing folder altogether, but I cannot get it to ignore it.
I am using angular-cli, and in my .angular-cli.json file I have these lines:
"tsconfig": "tsconfig.json",
"testTsconfig": "tsconfig.spec.json"
Both of these files are located under my src
folder, I tried adding some exclude patterns to my tsconfig.spec.json like so:
"exclude": [
"../testing",
"testing"
]
But this made no difference.
I then wondered if karma was definitely using my tsconfig.spec.json file and so I added the following:
karmaTypescriptConfig : {
tsconfig: '.src/tsconfig.spec.json',
}
But again, still no difference is made, my testing folder gets picked up by the compiler when preparing to run my tests.
I am unsure what else to try, I have updated my versions of TypeScript(3.1.6) and Karma(3.1.1) to the latest.
Has anyone got any ideas on what may be going on here?
Thanks
Upvotes: 8
Views: 12934
Reputation: 1468
For reference as the question is quite old (tested with Angular 14): it doesn't help anything to exclude files from karma.conf.js
, tsconfig.js
, etc. because tests are defined and loaded in test.ts
via Webpack.
tsconfig.spec.ts
Karma will complain that they are not compiled and won't run any other tests.Now look at test.js:
There is a line like this:
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
The regex needs to be adjusted to exclude the files, to make them effectively disappear. BUT: you cannot make the regex dynamic, you cannot filter the result afterward. The reason is Webpack, which seems to extract the regex and interpret it itself, so no JavaScript magic is allowed here.
The regex parameter needs to be passed directly without any dynamic code, no JavaScript, nothing but a regex.
// Then we find all the tests.
const context = require.context(
'./',
true,
// Webpack interprets this parameter by itself, meaning it must be passed
// directly and cannot contain any dynamic parts. Any attempts to handle it
// with exclude arrays, dynamic patterns, or post-filtering will fail because
// Webpack ignores all of that, instead extracting and parsing this expression
// on its own. It will fail with javascript parts.
/^\.\/(?!.*(filename1\.spec\.ts|filename2\.spec\.ts|filename3\.spec\.ts)).*\.spec\.ts$/
);
Upvotes: 0
Reputation: 81
Try to add the exclude file route into angular.json file in test configuration.
Example:
test: {...
"codeCoverageExclude": ["src/your_rute_file/name_file.ts"],
}
This works for me.
Upvotes: 1
Reputation: 306
Karma runs its tests on dist directory
. So, If you exclude your tesing directory
, it won't go into your compiled dist directory
.
In the file
tsconfig.json
Just add following code
"exclude": [
"./testing"
]
Writing this code should solve your problem. Let me know if this works.
Upvotes: -1
Reputation: 34
In the file,
tsconfig.spec.json
You can add the exclude array as follows,
"exclude": [
"node_modules",
"dist",
"**/tests/*.test.ts",
"src/tests"
]
so your final output will be as follow
Upvotes: 0
Reputation: 91
you need to add the file that need to remove in the tsconfig.spec.json file
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts",
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
],
"exclude": [
"yourdirectory to file/**/file that does not need to compile in testing"
]
}
so it will look like that,after run the unit tests this will exclude the file in the exclude section.
Upvotes: 0
Reputation: 2128
Maybe your path is not enough precise. Could we have your project pseudo tree view ? And especially the path of the file you want to exclude ?
The config file may expect something like this :
"exclude": [
"someFolder/**/o3-test-harness.class.ts"
]
Upvotes: 4