question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property 'length' of undefined, when karma tests re-load

See original GitHub issue

This bug report is from using Angular2 and Karma/Jasmine But the error is emitted from Typescript. I am hoping that is the relevant part.

Versions

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Angular CLI: 1.6.5
Node: 8.7.0
OS: darwin x64
Angular: 4.3.6
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router, tsc-wrapped

@angular/.DS_Store: error
@angular/cli: 1.6.5
@angular-devkit/build-optimizer: 0.0.41
@angular-devkit/core: 0.0.28
@angular-devkit/schematics: 0.0.51
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.5
@schematics/angular: 0.1.16
typescript: 2.3.4
webpack-bundle-analyzer: 2.9.2
webpack: 3.10.0

----
Some more versions that might be helpful

        "jasmine-core": "^2.99.1",
        "jasmine-expect": "^3.8.3",
        "jasmine-matchers": "^0.2.3",
        "jasmine-spec-reporter": "~4.1.0",
        "karma": "~1.7.0",
        "karma-chrome-launcher": "^2.2.0",
        "karma-cli": "~1.0.1",
        "karma-coverage-istanbul-reporter": "^1.2.1",
        "karma-jasmine": "^1.1.1",
        "karma-jasmine-html-reporter": "^0.2.2",
        "karma-jasmine-matchers": "^3.7.0",

I am using macOS Sierra, 10.12.6

Repro steps

When I run $ npm run test, my tests pass fine.
But if I then save one of my spec files (even if I don’t change it) Then a new run is kicked off, but always produces this error:

Observed behavior

ERROR in TypeError: Cannot read property 'length' of undefined
    at createSourceFile (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/typescript/lib/typescript.js:15464:110)
    at parseSourceFileWorker (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/typescript/lib/typescript.js:15389:26)
    at Object.parseSourceFile (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/typescript/lib/typescript.js:15338:26)
    at Object.createSourceFile (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/typescript/lib/typescript.js:15192:29)
    at new TypeScriptFileRefactor (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/@ngtools/webpack/src/refactor.js:79:35)
    at Object.findLazyRoutes (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/@ngtools/webpack/src/lazy_routes.js:18:22)
    at AotPlugin._findLazyRoutesInAst (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/@ngtools/webpack/src/plugin.js:217:50)
    at _donePromise.Promise.resolve.then.then.then.then.then (/Users/jschank/ddc-advocacy/projects/ddca-iceland/node_modules/@ngtools/webpack/src/plugin.js:496:24)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Desired behavior

Saving my spec files should result in karma tests being run again. (I know that isn’t the responsibility of this repo. But the error Appears to me to be in Typescript)

Mention any other details that might be useful (optional)

Most of the googling about this error, leads to the advice of putting a console.log(fileName); statement. To dump the offending file to the console. When I look at that location, I see this

function createSourceFile(fileName, languageVersion, scriptKind) {
	// code from createNode is inlined here so createNode won't have to deal with special case of creating source files
	// this is quite rare comparing to other nodes and createNode should be as fast as possible
	var sourceFile = new SourceFileConstructor(265 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length);
	nodeCount++;
	sourceFile.text = sourceText;
	sourceFile.bindDiagnostics = [];
	sourceFile.languageVersion = languageVersion;
	sourceFile.fileName = ts.normalizePath(fileName);
	sourceFile.languageVariant = getLanguageVariant(scriptKind);
	sourceFile.isDeclarationFile = ts.fileExtensionIs(sourceFile.fileName, ".d.ts");
	sourceFile.scriptKind = scriptKind;
	return sourceFile;
}

It looks to me, like the problem is that sourceText, is undefined. And we’re calling length on it here.
If I reset the sourceText to an empty string, when it is undefined or null. My tests re-run on changes, with no errors. Unfortunately, I have no idea if that is a reasonable solution to the problem.

function createSourceFile(fileName, languageVersion, scriptKind) {
	if (!sourceText) {
		// console.log("*** Filename: ", fileName);
		// console.log("*** languageVersion: ", languageVersion);
		// console.log("*** scriptKind: ", scriptKind);
		// Note: this seems to allow the typescript parser to continue when a file is updated
		sourceText = "";
	}
	// code from createNode is inlined here so createNode won't have to deal with special case of creating source files
	// this is quite rare comparing to other nodes and createNode should be as fast as possible
	var sourceFile = new SourceFileConstructor(265 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length);
	nodeCount++;
	sourceFile.text = sourceText;
	sourceFile.bindDiagnostics = [];
	sourceFile.languageVersion = languageVersion;
	sourceFile.fileName = ts.normalizePath(fileName);
	sourceFile.languageVariant = getLanguageVariant(scriptKind);
	sourceFile.isDeclarationFile = ts.fileExtensionIs(sourceFile.fileName, ".d.ts");
	sourceFile.scriptKind = scriptKind;
	return sourceFile;
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

22reactions
Adam-Pondcommented, Mar 7, 2018

As identified in https://github.com/angular/angular-cli/issues/5053, downgrading Angular CLI to 1.6.0 works. Seems to be a known issue

npm uninstall -g @angular/cli
npm uninstall --save-dev @angular/cli
npm install -g @angular/cli@1.6.0

rmdir /s node_modules
npm install --save-dev @angular/cli@1.6.0
npm install
6reactions
SergeyShurygincommented, Apr 2, 2018

I have the same error with cli 1.6.8

I’ve done some debugging and figured out that problem is in the next line in \node_modules@ngtools\webpack\src\plugin.js file:

const changedFilePaths = this._compilerHost.getChangedFilePaths();

in 1.6.0 version getChangedFilePaths returns only several files ( [ ‘…/app/src/polyfills.ts’, ‘…/app/node_modules/classlist.js/package.json’, ‘…/app/src/app/data-models/store/test.side-effects.spec.ts’ ], but in 1.6.8 it returns them all plus ‘…/app/src/’ directory, which does not have any sourceText and cause the mentioned error

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Cannot read property 'length' of undefined" when unit testing
In MockCodesService getCodes() is returning an observable from an array but in your component this.service.getCodes() expects an observable ...
Read more >
Cannot read properties of undefined (reading 'length') - TrackJS
This message indicates that our code expects to have an object with a length property, but that object was not present.
Read more >
Cannot Read Property Length of Undefined in JavaScript
The JavaScript TypeError: Cannot read property 'length' of undefined occurs when the length property is read on an undefined variable.
Read more >
Angular Unit Test Cannot Read Properties of Undefined
.cli TypeError: Cannot read property 'length' of undefined when karma tests reload This bug report is from using Angular2 and Karma/Jasmine.
Read more >
Resolving the JavaScript Promise Error "TypeError: Cannot ...
TypeError - Cannot read property 'then' of undefined is thrown when the caller is expecting a Promise to be returned and instead receives ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found