Angular-cli: Unable to exclude spec files from the test

Created on 29 Dec 2017  路  5Comments  路  Source: angular/angular-cli

Hi,
I am trying to exclude some spec files from the test, but can't.
so that,
I have created a fresh 'ng new myapp' and

I tried all the ways by the options provided.

  1. In karma.conf.js
    I have added exclude option in karma.conf.js like following,
    exclude:['app.component.spec.']

but this one, included the app.component.spec.ts and showed some test results.

  1. In tsconfig.spec.json
    I have tried exclude option but its also ends in vain.

Now, I would like to exclude bunch of specs but I am unable to do in all the ways even in the fresh project.

Can anybody help in this regard?

I am using Windows OS machine, and with the following versions:
Angular CLI: 1.6.3
Node: 6.10.0
OS: win32 x64
Angular: 4.4.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, tsc-wrapped

@angular/cli: 1.6.3
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.3
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.3.4
webpack: 3.10.0

Can you please identify the root cause of the problem?

Most helpful comment

Changing karma.conf.js doesn't do anything really, and changing tsconfig.spec.json just says those files shouldn't be compiled which isn't what you want I think.

What really loads the specs files is src/test.ts are these lines:

// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

You can change the regex to exclude some tests, but this might be hard on complicated setups.

A simpler but more verbose way is to replace those lines to import only the tests you want:

// Run only this test
import './app/app.component.spec';

You can also exclude some tests by replacing it(...) with xit(...).

All 5 comments

Changing karma.conf.js doesn't do anything really, and changing tsconfig.spec.json just says those files shouldn't be compiled which isn't what you want I think.

What really loads the specs files is src/test.ts are these lines:

// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

You can change the regex to exclude some tests, but this might be hard on complicated setups.

A simpler but more verbose way is to replace those lines to import only the tests you want:

// Run only this test
import './app/app.component.spec';

You can also exclude some tests by replacing it(...) with xit(...).

Definitely, we need something like:
"codeCoverageExclude" from angular.json
but not only for coverage but for test run exclude as well.

For now not to include every separate file I just used folder-wise approach:

const context = require.context('./lib/lib-components', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

const context2 = require.context('./lib/shared', true, /\.spec\.ts$/);
context2.keys().map(context2);

Changing karma.conf.js doesn't do anything really, and changing tsconfig.spec.json just says those files shouldn't be compiled which isn't what you want I think.

What really loads the specs files is src/test.ts are these lines:

// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

You can change the regex to exclude some tests, but this might be hard on complicated setups.

A simpler but more verbose way is to replace those lines to import only the tests you want:

// Run only this test
import './app/app.component.spec';

You can also exclude some tests by replacing it(...) with xit(...).

this is not working. My test file is empty but it is still loading the spec files. using karma webpack and there only is the test.ts as entry file. I don't understand from where it is loading the spec files

You able to change regex in src/test.ts file for exclude files with name
"*name*.*type*.exclude.spec.ts".

Change this line:
const context = require.context('./', true, /^((?!exclude).)*\.spec.ts$/);

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings