Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular2 - KarmaJS - unit Tests not executed automatically on npm test #2833

Closed
zaminabbaskhan opened this issue Sep 27, 2017 · 3 comments
Closed

Comments

@zaminabbaskhan
Copy link

Expected behaviour

Should execute tests - there is 300 + test case in my pro.

Actual behaviour

Throwing an exception Can't execute tests - Executed 0 of 0 ERROR (0.005 secs / 0 secs)

Environment Details

  • Karma version (output of karma --version):
  • Relevant part of your karma.config.js file
const webpackConfig = require('./webpack.config.test');    
module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    plugins: [
    require('karma-jasmine'),
    require('karma-chrome-launcher'),
    require('karma-phantomjs-launcher'),
    require('karma-jasmine-html-reporter'),
    require('karma-coverage-istanbul-reporter'),
    require('karma-webpack')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    files: [
    {pattern: './src/test.ts', watched: false}
    ],
    preprocessors: {
      './src/test.ts': ['webpack']
    },
    webpack: webpackConfig,
    webpackServer: {
      noInfo: true // prevent console spamming when running in Karma!
    },
    mime: {
      'text/x-typescript': ['ts', 'tsx']
    },

    // configure the reporter
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly', 'text-summary'],
      dir: 'coverage/',
      // if using webpack and pre-loaders, work around webpack breaking the source path
      fixWebpackSourcePaths: true,
      skipFilesWithNoCoverage: true,
      // enforce percentage thresholds
      thresholds: {
        statements: 80,
        lines: 80,
        branches: 70,
        functions: 80
      }
    },

    angularCli: {
      environment: 'dev',
      codeCoverage: true
    },
    reporters: ['progress', 'coverage-istanbul'],

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: true,
    concurrency: Infinity
  });
}

Here my "test.ts" file that I refer in "karma.conf.js"

// This file is required by karma.conf.js and loads recursively all the .spec and framework files
import 'core-js/es6';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';

import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

import './polyfills';
// Built by the core Angular team for mocking dependencies
import 'angular-mocks';

// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;

// Prevent Karma from running prematurely.
__karma__.loaded = function () {
};

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();

And this is tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "baseUrl": "",
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowJs": false,
    "target": "es5",
    "lib": [
    "es2016",
    "dom"
    ],
    "outDir": "../out-tsc/app",
    "module": "commonjs",
    "types": []
  },
  "exclude": [
  "test.ts",
  "**/*.spec.ts",
  "**/tmpl*.*",
  "*.tmpl.ts",
  "**/*.temp.ts",
  "app/templates/*/package/**"
  ]
}

Steps to reproduce the behaviour

@guilhermejcgois
Copy link

You are excluding your test.ts and all yours spec files on your tsconfig, maybe you need to use a tsconfig for running your tests, extending that tsconfig and including your test.ts and all specs.

Something like that:

{
  "extends": "path/to/tsconfig.json",
  "compilerOptions": {
    "outDir": "out-tsc/spec",
    "baseUrl": "..",
    "module": "commonjs",
    "target": "es5",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "test.ts"
  ],
  "include": [
    "**/*.spec.ts"
  ]
}

@zaminabbaskhan
Copy link
Author

Hi, I have tried this but its not working.

@devoto13
Copy link
Collaborator

It looks like a misconfiguration or a problem with one of the many plugins used, hence there is not much we can do about it in the Karma core.

Please use StackOverflow for questions and support in the future as we want to keep the issue tracker for bug reports and feature requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants