简体   繁体   English

当我使用最新的angular-cli(beta 15 w / webpack)运行测试时,如何提供资产?

[英]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) 当我尝试运行ng测试时,我的所有模块和组件都正确加载,但我在<img>标签中包含的任何资产都不会渲染,因为它们没有在/ assets文件夹中提供(就像它们在使用ng服务构建或开发期间)

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. 除此之外,最好知道如何将全局styles.scss / css文件包含在测试中,因为如果我将CSS放入组件并禁用ViewEncapsulation,我只能渲染这些样式。

I'm on the latest angular-cli webpack release (Beta 15) 我正在使用最新的angular-cli webpack版本(Beta 15)

Any help is greatly appreciated. 任何帮助是极大的赞赏。

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) 默认情况下,angular-cli(webpack)中的karma设置默认情况下不会为您的assets文件夹提供服务,但添加起来非常简单(一旦筛选完文档)

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: 在左边你可以看到我的Billy Mays图像现在正在服务,如果你看一下json的'file'部分就在右边,我添加了以下内容:

{ 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 我还添加了一个代理属性,以获取所提供的内容(默认情况下在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. 通过指定/assets/作为文件夹代理,karma使用路径localhost:[karma port number]/assets而不是默认值。

I'm happy I was able to my own question, and hope this helps some people starting with the angular-cli! 我很高兴我能够提出自己的问题,并希望这可以帮助一些人开始使用angular-cli!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM