angular-cli: TypeError: Cannot read property 'length' of undefined, when karma tests re-load
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;
}
About this issue
- Original URL
- State: closed
- 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
I have encountered the same issue with 1.6.5, 1.7.4 (latest) and 1.6.0 (downgraded as per suggested above). It’s caused by exactly the same reason as suggested by @SergeyShurygin above. Any update on this would be awesome as I can’t afford to patch node modules from public registry.
Would be nice to know if there was a fix for this by now. First reported in Feb. Anyone have any ideas?
Please. There’s no need to delete node_modules. This issue happens for a mismatch between you global
@angular/cli
, the angular tools (installed locally) and typescrypt (installed locally, also).I would recommend to run:
And before running the tests, make a compilation to avoid fixing things in the
@ngtools/webpack
on your own.Just tested this in production when I switched to my master branch and those errors appeared.
Take into consideration adding
npm i -g @angular/cli@latest
to thepostinstall
of your npm scripts.What @Adam-Pond suggested will only work if you are lucky and the three breaking points I mentioned are in harmony.
Hope it helps! 🐋
I solved this problem (error during refactoring test). All is good for angular v4.3.* and v4.4*
rm -rf node_modules
npm install --no-optional
I was having a similar problem on my Windows machine using Angular CLI 1.7.3, Node 6.11.0, and Angular 4.4.6.
It was trying to get the
sourceText.length
of a file that was actually a directory (the root directory of the project actually…src
).WebpackCompilerHost.getChangedFilePaths()
had thesrc
directory in it.I tracked that back to
Watchpack
’s_onChange
method._dirWatcher
was detecting a change, and calling_onChange
, with theitem
param being the directory, and thefile
param being the file.This is the block of code that adds the directory to the list of changes:
Once I changed this to:
The
length
error went away, and my tests rebuilt flawlessly.Also, for reference, these are my versions:
Verify your environments names.
Update: now it’s working fine for me with
ng test
.These days I’m using
Angular CLI: 1.7.4
withng test --watch --poll 256 -sm=false
and it’s working as expected. I haven’t triedng test
in a while.Same here, downgrade from 1.7.4 to 1.6.0 needed to avoid the issue when watching tests