While running karma unit tests, I encountered a similar issue and here is what I found:
One of my unit tests was writing data to a json file, resulting in the following error:
ERROR in TypeError: Cannot read property 'length' of undefined
at createSourceFile (...\node_modules\typescript\lib\typescript.js:15460:109)
at parseSourceFileWorker (...\node_modules\typescript\lib\typescript.js:15389:26)
at Object.parseSourceFile (...\node_modules\typescript\lib\typescript.js:15338:26)
at Object.createSourceFile (...\node_modules\typescript\lib\typescript.js:15192:29)
at new TypeScriptFileRefactor (...\node_modules\@ngtools\webpack\src\refactor.js:79:35)
at Object.findLazyRoutes (...\node_modules\@ngtools\webpack\src\lazy_routes.js:18:22)
at AotPlugin._findLazyRoutesInAst (...\node_modules\@ngtools\webpack\src\plugin.js:220:50)
at _donePromise.Promise.resolve.then.then.then.then.then (...\node_modules\@ngtools\webpack\src\plugin.js:499:24)
at process._tickCallback (internal/process/next_tick.js:109:7)
To troubleshoot, I added console.log statements in createSourceFile()
and AotPlugin._findLazyRoutesInAst
.
Upon closer inspection of AotPlugin._findLazyRoutesInAst
, I noticed the loop structure:
for (const filePath of changedFilePaths) { }
. Printing out 'changedFilePaths', I observed the following:
These are changedFilePaths ==>
[ '.../src/polyfills.ts',
'.../src' <--- Please note the missing file name.
]
This realization led me to understand the source of the error.
The problem persisted whenever changes were made to any files, not just .ts files. Even modifying a json file produced the same error.
I attempted to address the issue by making adjustments in karma.conf.js (following official config documentation):
exclude: ["./src/nonCodeRelatedData/**"]
.and
files: [ {pattern: "./src/nonCodeRelatedData/**", watched: false, included: false, served: false}]
However, these changes did not resolve the problem.
If someone could provide guidance on how to fix this issue, it would be greatly appreciated.