TypeError: Cannot read property ‘length’ of undefined

When running npm test, I had this error:


Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
at createSourceFile (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\typescript\lib\typescript.js:8586:109)
at parseSourceFileWorker (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\typescript\lib\typescript.js:8516:26)
at Object.parseSourceFile (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\typescript\lib\typescript.js:8477:26)
at Object.createSourceFile (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\typescript\lib\typescript.js:8339:29)
at new TypeScriptFileRefactor (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@ngtools\webpack\src\refactor.js:29:35)
at Object.resolveEntryModuleFromMain (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@ngtools\webpack\src\entry_resolver.js:105:20)
at AotPlugin._setupOptions (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@ngtools\webpack\src\plugin.js:143:50)
at new AotPlugin (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@ngtools\webpack\src\plugin.js:26:14)
at _createAotPlugin (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@angular\cli\models\webpack-configs\typescript.js:55:12)
at Object.exports.getNonAotTestConfig (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@angular\cli\models\webpack-configs\typescript.js:103:19)
at WebpackTestConfig.buildConfig (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@angular\cli\models\webpack-test-config.js:17:31)
at init (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\@angular\cli\plugins\karma.js:79:94)
at Array.invoke (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\di\lib\injector.js:75:15)
at Injector.get (D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\di\lib\injector.js:48:43)
at D:\tangu\Documents\UCL\cours\SINF2MS\Q4\Thesis\HAppi\node_modules\karma\lib\server.js:143:20
at Array.forEach (native)
npm ERR! Test failed. See above for more details.

With package.json

{
  "name": "HAppi-mobile",
  "author": "Geohealth Crew",
  "homepage": "https://github.com/GeoHealth/HAppi_mobile",
  "private": true,
  "scripts": {
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve",
    "test": "ng test --code-coverage",
    "test-travis": "npm run lint && ng test --single-run --code-coverage && npm run protractor",
    "build-translation": "po2json resources/locales/fr_BE/fr_BE.po src/assets/locales/fr_BE.json --pretty && po2json resources/locales/en/en.po src/assets/locales/en.json --pretty",
    "lint": "npm run tslint \"src/**/*.ts\" && npm run tslint \"e2e/**/*.ts\"",
    "tslint": "tslint",
    "protractor": "webdriver-manager update && protractor"
  },
  "config": {
    "ionic_webpack": "./config/webpack.config.js"
  },
  "dependencies": {
    "@angular/common": "^2.4.0",
    "@angular/compiler": "^2.4.0",
    "@angular/compiler-cli": "^2.4.0",
    "@angular/core": "^2.4.0",
    "@angular/forms": "^2.4.0",
    "@angular/http": "^2.4.0",
    "@angular/platform-browser": "^2.4.0",
    "@angular/platform-browser-dynamic": "^2.4.0",
    "@angular/platform-server": "^2.4.0",
    "@ionic/cloud-angular": "^0.9.1",
    "@ionic/storage": "1.1.7",
    "angular2-moment": "^1.2.0",
    "chart.js": "^2.5.0",
    "ionic-angular": "2.0.0-rc.5",
    "ionic-native": "2.2.11",
    "ionicons": "3.0.0",
    "localforage": "^1.4.3",
    "lokijs": "^1.4.1",
    "rxjs": "^5.1.0",
    "stacktrace-js": "^1.3.1",
    "sw-toolbox": "3.4.0",
    "zone.js": "^0.7.6"
  },
  "devDependencies": {
    "@angular/cli": "^1.0.4",
    "@angular/compiler-cli": "^2.4.0",
    "@ionic/app-scripts": "1.1.4",
    "@types/jasmine": "2.5.41",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "connect": "^3.6.2",
    "jasmine": "^2.6.0",
    "jasmine-core": "^2.5.2",
    "jasmine-spec-reporter": "^4.1.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-mocha-reporter": "^2.2.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "po2json": "^0.4.5",
    "protractor": "^5.1.2",
    "ts-node": "^3.0.4",
    "tslint": "~4.5.1",
    "tslint-loader": "^3.3.0",
    "typescript": "~2.0.0"
  },
  "cordovaPlugins": [
    "cordova-plugin-whitelist",
    "cordova-plugin-statusbar",
    "cordova-plugin-console",
    "cordova-plugin-device",
    "cordova-plugin-splashscreen",
    "ionic-plugin-keyboard"
  ],
  "cordovaPlatforms": [],
  "description": "HAppi: An Ionic project"
}

 

This solution helped me out: https://github.com/angular/angular-cli/issues/5053#issuecomment-283951922

TypeErrorCannot read property 'length' of undefined

Karma – 0 tests completed

Running unit testing with Karma on Google Chrome browser on a Angular 2 application.

No tests were running 😥

SUMMARY:
√ 0 tests completed

In the Chrome window, the console said

Refused to execute script from 'http://localhost:9876/base/src/tests/test.ts?b58784c03c82bb3f722dcb6d25bd26cdf145b12b' because its MIME type ('video/mp2t') is not executable

I found a solution here saying that I just need to add the following line to karma.conf.js

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