Pablo
Pablo

Reputation: 534

Karma can't find my tests in my angular-cli app

I have an Angular 4 project generated with CLI and I'm trying to make test work. I have Karma.conf in the root of my project:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', 'angular-cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-remap-istanbul'),
      require('angular-cli/plugins/karma')
    ],
    files: [
      { pattern: './src/test.ts', watched: false }
    ],
    preprocessors: {
      './src/test.ts': ['angular-cli']
    },
    mime: {
      'text/x-typescript': ['ts','tsx']
    },
    remapIstanbulReporter: {
      reports: {
        html: 'coverage',
        lcovonly: './coverage/coverage.lcov'
      }
    },
    angularCli: {
      config: './angular-cli.json',
      environment: 'dev'
    },
    reporters: config.angularCli && config.angularCli.codeCoverage
              ? ['progress', 'karma-remap-istanbul']
              : ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

And here is my test.ts located in my src folder:

declare var __karma__: any;
declare var require: any;

__karma__.loaded = function () {};

getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
const context = require.context('./', true, /\.spec\.ts$/);
context.keys().map(context);
__karma__.start();

My folder structure is:

    +---app
|   +---class
|   +---components
|   +---services
|   \---templates
+---assets
|   \---images
\---environments

So, inside my components, I have created a test called feeds.component.spec.ts that is associated to a component called feeds.component.ts.

The output of my windows console is the following:

Unable to find "@angular/cli" in devDependencies.

The package "angular-cli" has been deprecated and renamed to "@angular/cli".

Please take the following steps to avoid issues:
"npm uninstall --save-dev angular-cli"
"npm install --save-dev @angular/cli@latest"

08 05 2017 08:33:12.674:WARN [karma]: No captured browser, open http://localhost
:9876/
08 05 2017 08:33:12.687:INFO [karma]: Karma v1.2.0 server started at http://loca
lhost:9876/
08 05 2017 08:33:12.688:INFO [launcher]: Launching browser Chrome with unlimited
 concurrency
08 05 2017 08:33:12.897:INFO [launcher]: Starting browser Chrome
08 05 2017 08:33:17.033:INFO [Chrome 58.0.3029 (Windows 10 0.0.0)]: Connected on
 socket /#tif1xw_35N5kuco6ACBA with id 3123123

And karma debug console just outputs Skipped 0 tests so no test is being launched.

The strange thing is that I can launch default tests that are located in my template folder: app.component.spec.ts:

Chrome 58.0.3029 (Windows 10 0.0.0): Executed 3 of 3 (3 FAILED) (0 secs / 0.159
Chrome 58.0.3029 (Windows 10 0.0.0): Executed 3 of 3 (3 FAILED) ERROR (0.536 sec
s / 0.159 secs)

I've also tried "forcing" the test in my karma.conf.js by commenting the test.ts pattern and hardcoding my component:

files: [
  //{ pattern: './src/test.ts', watched: false },
  { pattern: 'app/components/feeds.component.spec.ts', included: true, watched:false }
],

But this outputs the following:

Uncaught SyntaxError: Unexpected token import
at src/app/components/feeds.component.spec.ts:1

Why aren't my components test not being launched?

Upvotes: 3

Views: 1691

Answers (2)

Kesarion
Kesarion

Reputation: 2848

For npm 5, I resolved this issue by:

  • deleting package-lock.json;
  • running npm cache clean --force;
  • deleting any unnecessary dependencies from package.json (I had @ngtools);
  • updating the rest of the dependencies using ncu and running npm i again.

I did have some issues with typescript 2.4.x afterwards and reverted to 2.3.4, but that will likely be resolved in a few days; it's worth mentioning for now.

Upvotes: 0

Phil Huhn
Phil Huhn

Reputation: 4101

Please take the following steps to avoid issues:

  npm uninstall --save-dev angular-cli
  npm clean
  npm install --save-dev @angular/cli@latest

you should also do it globally.

Upvotes: 1

Related Questions