如果在特定文件夹中,Angular CLI 生成的测试将失败 [英] Test generated by angular CLI fails if in a specific folder

查看:19
本文介绍了如果在特定文件夹中,Angular CLI 生成的测试将失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过运行 ng g c components/foo 使用 angular CLI (v6.1.4) 在我的组件文件夹中生成了一个组件,然后运行 ​​ng test.新组件的测试失败并出现以下错误:

I've generated a component in my components folder using the angular CLI (v6.1.4) by running ng g c components/foo and have then run ng test. The test for the new component fails with the following errors:

async() 测试助手需要区域,但找不到.请确保您的环境包括 zone.js/dist/zone.js 抛出

Zone is needed for the async() test helper but could not be found. Please make sure that your environment includes zone.js/dist/zone.js thrown

错误:非法状态:无法加载指令 FooComponent 的摘要.

Error: Illegal state: Could not load the summary for directive FooComponent.

奇怪的是,如果我运行 ng g c foo(即它不在 components 文件夹中生成),则测试通过.我的测试都适用于其他文件夹,但这是我的 components 文件夹中的第一个测试(也是一个组件的第一个测试).其他测试都不是异步的.

Oddly, if I run ng g c foo (i.e. so it's not generated in the components folder), the test then passes. My tests are all working for other folders, but this is the first test in my components folder (and is also the first test for a component). None of the other tests are asynchronous.

我对 ng test 还很陌生,所以我可能会遗漏一些非常明显的东西.我没想到需要使用 zone.js 设置任何东西来运行默认测试.我应该在哪里配置它?还是我在这里遗漏了其他东西,也许是我的 components 文件夹特有的东西?

I'm fairly new to ng test so I might be missing something very obvious. I wasn't expecting to need to set up anything with zone.js for the default test to run. Where should I configure this? Or is there something else I'm missing here, perhaps something specific to my components folder?

推荐答案

foo.component.spec.ts 顶部添加以下内容修复了问题,但我仍然不明白为什么如果它们不在 components 文件夹中,测试就会工作.如果有人有解释,或者没有在每个组件的测试文件中重复这一点的解决方案,我很乐意听到!

Adding the following to the top of foo.component.spec.ts fixed the issue, but I still don't understand why the tests worked if they weren't in the components folder. If someone has an explanation, or a solution that doesn't involve repeating this in every component's test file, I'd love to hear it!

import 'zone.js';
import 'zone.js/dist/async-test.js';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';

这篇关于如果在特定文件夹中,Angular CLI 生成的测试将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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