question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:16
  • Comments:13

github_iconTop GitHub Comments

6reactions
LoaderB0Tcommented, Jun 3, 2019

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:

 files: [
    { pattern: 'assets/Scripts/**/*.js', watched: true, included: true, served: true },
    { pattern: 'assets/Scripts/**/*.ttf', watched: false, included: false, served: true },
    { pattern: 'assets/Scripts/**/*.woff', watched: false, included: false, served: true },
    { pattern: 'assets/Scripts/**/*.woff2', watched: false, included: false, served: true },
    { pattern: 'assets/Scripts/**/*.css', watched: false, included: false, served: true },
],

And to add an entry in the proxies array like so:

proxies: {
    "/assets/": "/base/assets/"
}
5reactions
alan-agius4commented, Jun 22, 2019

Just had a look at this and it seems to be a problem from copy-webpack-plugin which are not handling Windows back slash separators. This issue occurs only on Windows OS.

What is happening is webpack’s MemoryFileSystem will contain below entries

MemoryFileSystem {
  data:
   { _karma_webpack_:
      { '': true,
        'main.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'polyfills.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'polyfills-es5.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'styles.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'vendor.js':
         <Buffer 28 77 69 6e 64 6f 77 5b 22 77 65 62 70 61 63 6b 4a 73 6f 6e 70 22 5d 20 3d 20 77 69 6e 64 6f 77 5b 22 77 65 62 70 61 63 6b 4a 73 6f 6e 70 22 5d 20 7c ... >,
        'favicon.ico':
         <Buffer 00 00 01 00 02 00 10 10 00 00 00 00 20 00 68 04 00 00 26 00 00 00 20 20 00 00 00 00 20 00 a8 10 00 00 8e 04 00 00 28 00 00 00 10 00 00 00 20 00 00 00 ... >,
        assets: [Object],
        'assets\\test.json':
         <Buffer 7b 0d 0a 20 20 22 74 65 73 74 22 3a 20 74 72 75 65 0d 0a 7d 0d 0a> } } }

Instead of

MemoryFileSystem {
  data:
   { _karma_webpack_:
      { '': true,
        'main.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'polyfills.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'polyfills-es5.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'styles.js':
         <Buffer 2f 2a 2a 2a 2a 2a 2a 2f 20 28 66 75 6e 63 74 69 6f 6e 28 6d 6f 64 75 6c 65 73 29 20 7b 20 2f 2f 20 77 65 62 70 61 63 6b 42 6f 6f 74 73 74 72 61 70 0a ... >,
        'vendor.js':
         <Buffer 28 77 69 6e 64 6f 77 5b 22 77 65 62 70 61 63 6b 4a 73 6f 6e 70 22 5d 20 3d 20 77 69 6e 64 6f 77 5b 22 77 65 62 70 61 63 6b 4a 73 6f 6e 70 22 5d 20 7c ... >,
        'favicon.ico':
         <Buffer 00 00 01 00 02 00 10 10 00 00 00 00 20 00 68 04 00 00 26 00 00 00 20 20 00 00 00 00 20 00 a8 10 00 00 8e 04 00 00 28 00 00 00 10 00 00 00 20 00 00 00 ... >,
        assets: [Object] } } }

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found