TypeError: Cannot read property 'length' of undefined, when karma tests re-load
See original GitHub issueThis 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:
- Created 6 years ago
- Reactions:16
- Comments:19 (2 by maintainers)
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
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