问题
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.
回答1:
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
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!
来源:https://stackoverflow.com/questions/40088462/how-do-i-serve-assets-when-i-run-ng-test-using-latest-angular-cli-beta-15-w-web