Angular Tests Fail in Docker: Range Undefined Fix

Hi there, today I’d like to share with you a little caveat that might result to the Angular Tests being successful on your local development machine but failing while on build server in Docker Container.
The text you might see does not give much sense about what might be causing an issue. Check it yourself:
`ERROR [karma-server]: UnhandledRejection: Cannot read property 'range' of undefined
ERROR [karma-server]: TypeError: Cannot read property 'range' of undefined
at handleRangeHeaders (/app/node_modules/webpack-dev-middleware/lib/util.js:131:21)
at processRequest (/app/node_modules/webpack-dev-middleware/lib/middleware.js:98:19)
at ready (/app/node_modules/webpack-dev-middleware/lib/util.js:53:12)
at handleRequest (/app/node_modules/webpack-dev-middleware/lib/util.js:182:5)
at /app/node_modules/webpack-dev-middleware/lib/middleware.js:64:7
at new Promise (ng test.
The root cause of this issue might be some resource files you reference from assets folder. For instance the code as follows in one of your components would be sufficient for a random tests failure in Docker container:
<img src="../../../assets/welcome.jpg" title="Welcome" alt="Welcome" />
When executing tests through karma test runner the image is being loaded. Karma server should be configured appropriately so that it “knows” where to get the resource file from.
How To Fix It
If that’s your case a simple rule in karma.conf.js is sufficient for it. Simply add the following code after
`config.set({ proxies: { '/assets/': '/base/src/assets/' },` What it does is basically pointing all requests matching /assets/ pattern into the base foder of the project into src/assets/ folder. If the location of assets in your case is different, please adjust accordingly.
I hope that was helpful in building something great.
Take care,
Ievgen
Frequently Asked Questions
Why do Angular tests pass locally but fail in a Docker container?
Angular unit tests can pass on a local machine yet fail inside a Docker container with the error "Cannot read property 'range' of undefined", thrown from webpack-dev-middleware via the Karma test runner. A common root cause is a component referencing a resource file from the assets folder that the Karma server is not configured to serve, so the request for that file fails during the test run.
How do I fix the Angular Karma error "Cannot read property 'range' of undefined"?
Add a proxy rule to karma.conf.js so Karma can serve assets referenced by your components. Inside config.set({ ... }) add proxies: { '/assets/': '/base/src/assets/' }. This maps every request matching the /assets/ pattern to the project's src/assets/ folder. Adjust the target path if your assets live elsewhere, then re-run ng test.
What causes the "range of undefined" error in webpack-dev-middleware during ng test?
The error originates in handleRangeHeaders inside webpack-dev-middleware, which Angular uses together with the Karma test runner when you run ng test. It is typically triggered when a component loads a resource such as an image from the assets folder, for example an img tag pointing at ../../../assets/welcome.jpg, and Karma has not been told where to fetch that file from.
Does the karma.conf.js proxies setting need to match my assets path?
Yes. The proxies rule '/assets/': '/base/src/assets/' assumes your assets live in src/assets/. The left side is the URL pattern your components request, and the right side is where Karma serves the files from, under /base (the project root). If your assets are stored in a different folder, change the right-side path to match.