Karma not serving assets in angular 8
See original GitHub issueπ bug report
Affected Package
Karma tests (ng t)
Is this a regression?
Yes, in version 7 it was working
Description
When loading a http asset, angular 8 returns Failed: Http failure response for http://localhost:9877/assets/test.json: 404 Not Found
π¬ Minimal Reproduction
ng new ng8-test-assets
create a file in the assets folder named test.json with this content
{
"test": true
}
in app.component.spec.ts:
it('should get test.json', async () => {
const client: HttpClient = TestBed.get(HttpClient);
try {
const result = await client.get('/assets/test.json').toPromise();
} catch (err) {
fail(err);
}
});
Heres a link to a repo with the issue: https://github.com/plchampigny/ng8-test-assets
π₯ Exception or Error
Failed: Http failure response for http://localhost:9877/assets/test.json: 404 Not Found
π Your Environment
Angular Version:
Angular CLI: 8.0.0
Node: 10.16.0
OS: win32 x64
Angular: 8.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.800.0
@angular-devkit/build-angular 0.800.0
@angular-devkit/build-optimizer 0.800.0
@angular-devkit/build-webpack 0.800.0
@angular-devkit/core 8.0.0
@angular-devkit/schematics 8.0.0
@ngtools/webpack 8.0.0
@schematics/angular 8.0.0
@schematics/update 0.800.0
rxjs 6.4.0
typescript 3.4.5
webpack 4.30.0
Anything else relevant?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:16
- Comments:13
Top Results From Across the Web
Angular 8: assets not loading - Stack Overflow
The closest I've come to an answer are old threads: Angular 5 images not loading from assets directory Β· Assets folder issue. ng...
Read more >Angular CLI: Different ways to include assets - Εukasz Nojek
This approach has a disadvantage: after building, the asset will go to app/component1/assets folder in the dist folder: dist ββββour-Β ...
Read more >ng test - Angular
Option Description Value Type Defau...
ββbrowsers Override which browsers tests are run against. string
ββcodeβcoverage Output a code coverage report. boolean false
ββcodeβcoverageβexclude Globs to exclude...
Read more >Files - Karma test runner
The pattern is a glob which matches the specified image assets. Watched and included are not necessary as images are not tests. However,...
Read more >How do I serve assets when I run ng test using latest angular ...
I found the answer! ... proxies: { '/assets/': '/base/src/assets/' },. By specifying /assets/ as the folder proxy, karma uses the path localhost:[karma portΒ ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

We have the same problems since updating to Angular 8 today. The console gets spammed with errors of missing fonts and images π
For me the fix/workaround was to add the assets folder to the βfilesβ array in the karma.conf.js like so:
And to add an entry in the proxies array like so:
Just had a look at this and it seems to be a problem from
copy-webpack-pluginwhich are not handling Windows back slash separators. This issue occurs only on Windows OS.What is happening is webpackβs
MemoryFileSystemwill contain below entriesInstead of
When webpack-dev-middleware call
context.fs.statSync('/_karma_webpack_/assets/test.json');it will throw an error that the file cannot be found.I opened a PR to address this https://github.com/webpack-contrib/copy-webpack-plugin/pull/389