Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve test.ts when running ng test?

Tags:

I'm using Angular CLI and when I run ng test, it starts launching the browser, then I suddenly get an error:

userName@UserName:~/devApp/profiles$ng test
10% building modules 1/1 modules 0 active18 09 2017 16:16:51.891:WARN [karma]: No captured browser, open http://localhost:9876/
18 09 2017 16:16:51.903:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
18 09 2017 16:16:51.903:INFO [launcher]: Launching browser Chrome with unlimited concurrency
18 09 2017 16:16:51.915:INFO [launcher]: Starting browser Chrome

ERROR in Entry module not found: Error: Can't resolve '/Users/xxx/devApp/profiles/src/test.ts' in '/Users/xxx/devApp/profiles/node_modules/@angular/cli/models/webpack-configs'

Here is the file: test.ts as generated via Angular CLI when I did set up the project:

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

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

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

angular-cli.json:

    {
  "project": {
    "name": "profiles"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "testTsconfig": "tsconfig.json",
      "prefix": "app",
      "styles": [
        "styles.css",
        "custom_theme.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "files": "src/**/*.ts",
      "project": "src/tsconfig.json"
    },
    {
      "files": "e2e/**/*.ts",
      "project": "e2e/tsconfig.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "class": {
      "spec": false
    },
    "component": {
      "spec": true,
      "inlineStyle": false,
      "inlineTemplate": false
    }
  }
}
like image 530
k.vincent Avatar asked Sep 18 '17 14:09

k.vincent


1 Answers

The error was in karma.conf.js file as the path to test.ts was pointing to src folder which is wrong.:

...
files: [
    { pattern: './src/test.ts', watched: false }
],
preprocessors: {
    './src/test.ts': ['@angular/cli']
}
..., 

Solution: I had remove the file test.ts from root to /src folder: /src/test.ts and it runs the command as expected.

like image 124
k.vincent Avatar answered Oct 11 '22 14:10

k.vincent