1

Can anyone help me why test is not executed.

I added spec file at: 'app/1st.spec.ts' as per angular.io testing web site.

describe('1st tests', () => {
  it('true is true', () => expect(true).toBe(true));
});

karma run with no issue. Here is the result:

Chrome 55.0.2883 (Windows 10 0.0.0): Executed 0 of 0 ERROR (0.002 secs / 0 secs)

I don't know the reason why my project doesn't work but I tested in another new project and the unit test is executed. Please advice!

Here is my package.json. I am also using Angular Universal.

{
  "name": "my-app",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "backend": "nodemon bin/server/index.js",
    "compile": "node_modules/.bin/webpack",
    "dump-task-list": "npm run compile && node bin/server/index.js dump-task-list",
    "server": "npm run compile && npm run backend",
    "server-host": "ng build && ts-node server/server.ts",
    "start": "npm run start-with-server",
    "start-with-server": "npm run dump-task-list && ng serve",
    "lint": "tslint \"src/**/*.ts\" \"log-server/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.13",
    "@ngrx/core": "^1.2.0",
    "@ngrx/effects": "^2.0.0",
    "@ngrx/store": "^2.2.1",
    "@ngrx/store-devtools": "^3.1.0",
    "@ngrx/store-log-monitor": "^3.0.2",
    "@progress/kendo-angular-buttons": "^0.13.4",
    "@progress/kendo-angular-grid": "^0.6.3",
    "@progress/kendo-angular-upload": "^0.10.4",
    "@progress/kendo-data-query": "^0.1.7",
    "@telerik/kendo-theme-bootstrap": "^0.10.0",
    "@types/winston": "0.0.32",
    "angular2-express-engine": "^2.0.11",
    "angular2-platform-node": "~2.0.11",
    "angular2-universal": "^2.0.11",
    "angular2-universal-polyfills": "^2.0.11",
    "body-parser": "^1.15.2",
    "bootstrap": "^4.0.0-alpha.5",
    "cheerio": "^0.22.0",
    "cookie-parser": "^1.4.3",
    "core-js": "^2.4.1",
    "express": "^4.14.0",
    "jsnlog": "^2.20.1",
    "node-uuid": "^1.4.7",
    "pouchdb": "^6.0.7",
    "preboot": "^4.5.2",
    "rxjs": "5.0.0-beta.12",
    "signalr": "^2.2.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@angularclass/resolve-angular-routes": "^1.0.9",
    "@types/body-parser": "0.0.29",
    "@types/chai": "^3.4.34",
    "@types/cheerio": "^0.17.31",
    "@types/compression": "0.0.29",
    "@types/cookie-parser": "^1.3.30",
    "@types/express": "^4.0.32",
    "@types/express-serve-static-core": "^4.0.33",
    "@types/hammerjs": "^2.0.32",
    "@types/jasmine": "^2.2.30",
    "@types/mime": "0.0.28",
    "@types/mkdirp": "^0.3.29",
    "@types/morgan": "^1.7.32",
    "@types/node": "^6.0.38",
    "@types/pouchdb": "^5.4.28",
    "@types/serve-static": "^1.7.27",
    "@types/uuid": "^2.0.29",
    "angular-cli": "1.0.0-beta.16",
    "angular2-schema-form": "1.0.0-alpha.32",
    "angular2-template-loader": "^0.4.0",
    "codelyzer": "~0.0.26",
    "concurrently": "^3.1.0",
    "imports-loader": "^0.6.5",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "json-loader": "^0.5.4",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "nodemon": "^1.10.0",
    "protractor": "4.0.9",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.5.4",
    "string-replace-loader": "github:gdi2290/string-replace-loader",
    "ts-loader": "^0.8.2",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "2.0.2",
    "webpack": "2.1.0-beta.22",
    "winston": "^2.3.0",
    "winston-daily-rotate-file": "^1.3.1"
  }
}

Here is my karma.config

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']
    },
    remapIstanbulReporter: {
      reports: {
        html: 'coverage',
        lcovonly: './coverage/coverage.lcov'
      }
    },
    angularCli: {
      config: './angular-cli.json',
      environment: 'dev'
    },
    reporters: ['progress', 'karma-remap-istanbul'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

here is test.ts

import './polyfills.ts';

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';

// 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 () {};


Promise.all([
  System.import('@angular/core/testing'),
  System.import('@angular/platform-browser-dynamic/testing'),
])
  // First, initialize the Angular testing environment.
  .then(([testing, testingBrowser]) => {
    testing.getTestBed().initTestEnvironment(
      testingBrowser.BrowserDynamicTestingModule,
      testingBrowser.platformBrowserDynamicTesting()
    );
  })
  // Then we find all the tests.
  .then(() => require.context('./', true, /\.spec\.ts/))
  // And load the modules.
  .then(context => context.keys().map(context))
  // Finally, start Karma to run the tests.
  .then(__karma__.start, __karma__.error);
8
  • The title and the tags are misleading. It is not just 'Angular 2 Unit Test'. It is Angular CLI. And the question obviously needs Karma config, this is the most relevant part, not package.json. Commented Jan 17, 2017 at 4:18
  • @estus, I added karma.config.js as well. Thanks! Commented Jan 17, 2017 at 5:09
  • Your spec is in 1st.spec.ts, and in config it is test.ts. Commented Jan 17, 2017 at 5:13
  • My another working project has same configuration for karma.config. I don't think it is the issue. Commented Jan 17, 2017 at 5:23
  • It is the issue. 'but it works in another place' can hardly be a good argument. You have provided no code that could prove that 1st.spec.ts should be executed. files is the place where specs that should be executed are specified, and in your case it appears like they are not. This is the only (and very substantial) clue on the possible problem that the question has. If you can't provide a reproducible example of the problem, I'm afraid nothing else can be done here. Commented Jan 17, 2017 at 6:18

3 Answers 3

2

I had the same problem.

try this on karma.conf.js:

mime: {
  'text/x-typescript': ['ts','tsx'],
  'text/javascript': ['js']
},
Sign up to request clarification or add additional context in comments.

Comments

0

In your karma.config make following changes -

files: [
      { pattern: './src/[path_to_your_app_folder]/*.spec.ts', watched: false }
    ]
preprocessors: {
      './tests/**/*.spec.js': ['angular-cli']
    }

It will resolve your issue.

Comments

0

In my case:

Added:

    mime: {
        'text/x-typescript': ['ts', 'tsx']
    },

Edit:

reporters: config.angularCli && config.angularCli.codeCoverage ?
            ['progress', 'karma-remap-istanbul'] :
            ['progress'],

Now test is working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.