简体   繁体   English

具有业力和茉莉花的Angular2应用会使用多级目录结构引发错误

[英]An Angular2 app with karma & jasmine throws errors with multi-level directory structure

I am building an Angular 2 app with the current 4.0.0-beta.1 release. 我正在使用当前的4.0.0-beta.1版本构建Angular 2应用程序。 After many hours of struggle, I got karma running. 经过许多小时的努力,我得到了业力。 I'm now trying to get a jasmine tautology test (true=true) to run, but karma reports: 我现在正在尝试运行茉莉重言式测试(true = true),但因果报应:

"Uncaught ReferenceError: describe is not defined" “未捕获的ReferenceError:描述未定义”

Here are the relevant code sections: 以下是相关的代码部分:

karma.conf.js karma.conf.js

module.exports = function(config) {
config.set({

basePath: '',
frameworks: ['jasmine'],
// these plugins seem to be necessary. Without them, an error about a missing require in app.component.js occurs in karma
plugins: [
  require('karma-jasmine'),
  require('karma-chrome-launcher'),
  require('karma-htmlfile-reporter')
],

files: [
  'src/client/app/*.js',
  'src/client/app/components/**/*.js'
], 

1st.spec.ts 第一规格

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By }  from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

////////  SPECS  /////////////
describe('1st tests', () => {
  it('true is true', () => expect(true).toBe(true));
});

The 1st.spec.ts file is in the 'src/client/app/*.js' folder, which is specified in the files: array of karma.conf.js 1st.spec.ts文件位于src / client / app / *。js文件夹中,该文件夹在以下文件中指定:karma.conf.js数组

What is missing? 什么东西少了? How do you get Angular2 to recognize jasmine functions? 您如何使Angular2识别茉莉花功能? I do not have any of the testing components imported into app.module.ts because I haven't seen any tutorials mention that. 我没有将任何测试组件导入到app.module.ts中,因为我没有看到任何教程提到这一点。 Is that the secret? 那是秘密吗? If so what goes in there? 如果是这样,那里面有什么?

After weeks of reading tutorials, karma docs, and buying ng-book2, I discovered that no one describes how to set test configs when app.component.ts is several levels down in the directory structure. 在阅读了数周的教程,业力文档和购买ng-book2之后,我发现当app.component.ts在目录结构中降低了几层时,没有人描述如何设置测试配置。 Karma docs may explain this, but the docs are so verbose and abstract, without examples, I was not able to gain any meaning from them. 业力文档可能会对此进行解释,但是这些文档是如此冗长和抽象,没有示例,我无法从中获得任何意义。 I also found that Angular's quickstart apps have various 'secret sauces' to enable karma & jasmine testing, eg, karma-test-shim.js (which you cannot get via npm). 我还发现Angular的quickstart应用程序具有各种“秘密调味料”以启用业力和茉莉花测试,例如karma-test-shim.js(无法通过npm获得)。

With no help from the above docs, I re-started with Angular's Setup. 在上述文档的帮助下,我重新开始使用Angular的安装程序。 Setup uses systemjs. 安装程序使用systemjs。 AngularCLI uses Webpack. AngularCLI使用Webpack。 Read about this. 阅读有关此内容。 It makes a difference for my solution below and for your longer term actions. 这对下面的我的解决方案以及您的长期行动有所不同。 Jasmine tests work off the shelf for Angular Setup's Quickstart. Jasmine测试适用于Angular Setup的快速入门。 I nudged the directory structure to my multi-level and tweaked lots of things to find configs that kept karma & jasmine working. 我将目录结构微调到多级,并进行了很多调整,以查找使业力和茉莉花正常工作的配置。 Bottom line - Systemjs and karma.config.js both have to change - together. 底线-Systemjs和karma.config.js都必须一起更改。 A change in only 1 of them just creates new and novel console errors. 仅更改其中的1个会创建新的新颖的控制台错误。

Systemjs - change app: in the map object to your path. Systemjs-将地图对象中的应用更改为您的路径。 In my case, its 就我而言,

map: {
  app: 'src/client/app',
  // the rest of map
}

karma.conf.js - change appBase and appSrcBase in the module.exports function karma.conf.js-在module.exports函数中更改appBase和appSrcBase

var appBase    = 'src/client/app/';  //originally appBase='app/';
var appSrcBase = 'src/client/app/';

The Console errors make it sound like the problem is in the @angular bundle paths. 控制台错误使它听起来像问题出在@angular捆绑包路径中。 Changes there just created different errors. 那里的更改只是造成了不同的错误。 Don't get sucked in. 不要被吸引。

Hope this saves you lots of time in getting tests going on an app that has a real life directory structure. 希望这为您节省了在具有真实目录结构的应用程序上进行测试的大量时间。

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

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