使用最新的angular-cli(beta 15 w/webpack)进行ng测试时,如何提供资产? [英] How do I serve assets when I run ng test using latest angular-cli (beta 15 w/webpack)?

查看:58
本文介绍了使用最新的angular-cli(beta 15 w/webpack)进行ng测试时,如何提供资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行ng测试时,我的所有模块和组件均正确加载,但是我包含在< img> 标记中的任何资产均未呈现,因为它们没有被放在/assets文件夹中(就像使用ng serve在构建或开发过程中一样)

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)

除此之外,很高兴知道如何将全局styles.scss/css文件包含在测试中,因为只有将CSS放到组件中,然后才能渲染这些样式.禁用ViewEncapsulation.

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.

我正在使用最新的angular-cli Webpack版本(测试版15)

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

任何帮助将不胜感激.

推荐答案

我找到了答案!

默认情况下,默认情况下,angular-cli(webpack)中的业力设置不提供您的资产文件夹,但这非常容易添加(一旦您浏览了文档)

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

在左侧,您可以看到我正在播放Billy Mays的图像;在右侧,如果您查看json的文件"部分,则添加了以下内容:

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 }

我还添加了一个代理属性,以获取所提供的内容(默认情况下在 http://localhost:[karma 端口号]/base

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/'
},

通过指定/assets/作为文件夹代理,业力使用路径 localhost:[因果端口号]/资产,而不是默认值.

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!

这篇关于使用最新的angular-cli(beta 15 w/webpack)进行ng测试时,如何提供资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆