Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I serve assets when I run ng test using latest angular-cli (beta 15 w/webpack)?

When I try and run an ng test, all of my modules and components load correctly, but any of the assets I include in <img> tags do not render because they're not being served in the /assets folder (like they would in a build or during development using ng serve)

In addition to this, it would be nice to know how to get the global styles.scss/css file to be included in the test, as I can only get these styles to render if I drop the CSS into a component and disable ViewEncapsulation.

I'm on the latest angular-cli webpack release (Beta 15)

Any help is greatly appreciated.

like image 933
ProgrammerInProgress Avatar asked Oct 17 '16 14:10

ProgrammerInProgress


People also ask

What is used for running test cases in angular?

Jasmine and Karma frameworks are used for Unit Testing of the Angular applications.

What is NG test in angular?

ng test <project> [options] ng t <project> [options] Description. Takes the name of the project, as specified in the projects section of the angular. json workspace configuration file. When a project name is not supplied, it will execute for all projects.

How do I disable NG test?

Disable a Test with @Test( enabled=false ) To disable a test in TestNG, we should set the enabled attribute of the @Test annotation to false . The default attribute value of enabled is true.


1 Answers

I found the answer!

By default, the karma setup in the angular-cli (webpack) doesn't serve your assets folder by default, but this is very simple to add (once you sift through the documentation)

below is a screenshot of my results and the code I added to get it to work

enter image description here

To the left you can see my image of Billy Mays is now being served, to the right if you look at the 'file' section of the json, I added the following:

{ pattern: './src/assets/**', watched: false, included:false, nocache:false, served:true }

I also added a proxies property, to take the served content (served by default at http://localhost:[karma port number]/base

proxies: {
   '/assets/': '/base/src/assets/'
},

By specifying /assets/ as the folder proxy, karma uses the path localhost:[karma port number]/assets instead of the default.

I'm happy I was able to my own question, and hope this helps some people starting with the angular-cli!

like image 80
ProgrammerInProgress Avatar answered Oct 16 '22 12:10

ProgrammerInProgress