简体   繁体   English

如何让Karma在工作区内运行特定角度6组件库的测试

[英]How to get Karma to run tests of a specific angular 6 component library inside workspace

I have an angular 6 workspace (called miami-space) and have created a component library called "vice-lib". 我有一个角度6工作空间(称为迈阿密空间),并创建了一个名为“vice-lib”的组件库。 This directory tree looks like this: 此目录树如下所示:

.
└── miami-space       <-- This is my workspace directory
    ├── e2e
    │   └── src
    ├── node_modules
    ...
    ├── projects      <-- Projects directory for created libraries
    │   └── vice-lib
    │       └── src
    │           └── lib
    └── src           <-- The source directory for the app
        ├── app
        ├── assets
        └── environments

(I created the library with ng new library vice-lib --ext=vlib) (我使用ng new library vice-lib创建了库--ext = vlib)

If I go to the root dir (miami-space) and run the unit tests with 如果我去根目录(迈阿密空间)并运行单元测试

npm run test npm运行测试

Karma runs the tests in the browser, but because there are tests for the app and tests for the library, karma runs the tests in 2 phases. Karma在浏览器中运行测试,但由于有应用程序的测试和库的测试,因此karma分两个阶段运行测试。

The first phase ONLY runs the tests for the app and you see this in the console: 第一阶段运行应用程序的测试,您在控制台中看到:

λ ~/dev/tasks/t-122/cat/miami-space/ npm run test

> miami-fake-app@0.0.0 test /Users/Plastikfan/dev/tasks/t-122/cat/miami-space
> ng test

 10% building modules 1/1 modules 0 active(node:41963) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
21 06 2018 13:47:36.106:WARN [karma]: No captured browser, open http://localhost:9876/
21 06 2018 13:47:36.111:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
21 06 2018 13:47:36.111:INFO [launcher]: Launching browser Chrome with unlimited concurrency
21 06 2018 13:47:36.116:INFO [launcher]: Starting browser Chrome
21 06 2018 13:47:39.894:WARN [karma]: No captured browser, open http://localhost:9876/    
21 06 2018 13:47:40.006:INFO [Chrome 67.0.3396 (Mac OS X 10.13.4)]: Connected on socket _5z2mq2NuMiHHahdAAAA with id 8795036
Chrome 67.0.3396 (Mac OS X 10.13.4): Executed 3 of 3 SUCCESS (0.154 secs / 0.138 secs)

... and the test results for the app show up in the browser ...并且应用程序的测试结果显示在浏览器中

If you hit < CTRL >-C, Karma kills the browser and then runs the tests for the library ONLY ( note the ^C denoting when I hit CTRL-C ): 如果你点击< CTRL > -C,Karma会杀死浏览器,然后只运行库的测试( 注意^ C表示我点击CTRL-C时 ):

^C  0% compiling21 06 2018 13:50:21.034:WARN [karma]: No captured browser, open http://localhost:9876/
21 06 2018 13:50:21.035:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
21 06 2018 13:50:21.035:INFO [launcher]: Launching browser Chrome with unlimited concurrency
21 06 2018 13:50:21.044:INFO [launcher]: Starting browser Chrome
21 06 2018 13:50:24.453:WARN [karma]: No captured browser, open http://localhost:9876/  
21 06 2018 13:50:24.544:INFO [Chrome 67.0.3396 (Mac OS X 10.13.4)]: Connected on socket FelqTteskkryAtnYAAAB with id 63644171
Chrome 67.0.3396 (Mac OS X 10.13.4): Executed 5 of 5 SUCCESS (0.112 secs / 0.093 secs)

So I don't really understand how Karma is running here, presuming this is intended behaviour. 所以我真的不明白Karma是如何在这里运行的,假设这是预期的行为。

Are you only allowed to run tests for the component library after running the application tests? 您是否只允许在运行应用程序测试后运行组件库的测试? What if I have a lot of tests in the app but I only want to run the library tests? 如果我在应用程序中有很多测试但我只想运行库测试怎么办?

If I "cd" to the vice-lib directory and try to run the tests using npm run test, then because the package.json is a cutdown config which doesn't include a scripts section, npm fails understandably: 如果我“cd”到副lib目录并尝试使用npm run test运行测试,那么因为package.json是一个不包含脚本部分的cutdown配置,所以npm无法理解:

echo 'Error: no test specified' echo'错误:没有指定测试'

So I read up further on the karma documentation and saw the 'basePath' (by default set to empty string). 所以我进一步阅读了karma文档并看到了'basePath'(默认情况下设置为空字符串)。 This is described as: 这被描述为:

The root path location that will be used to resolve all relative paths defined in files and exclude. 将用于解析文件中定义的所有相对路径并排除的根路径位置。 If the basePath configuration is a relative path, then it will be resolved to the __dirname of the configuration file. 如果basePath配置是相对路径,则它将解析为配置文件的__dirname。

I thought I could update this to point to the root of the library (hopefully with the option of specifying on the command line, so I could write multiple script entries, see further down below) ie 我以为我可以更新它以指向库的根目录(希望在命令行上指定选项,所以我可以编写多个脚本条目,请参见下面的内容),即

./src/projects/vice-lib ./src/projects/vice-lib

But this seems to have no effect on the behaviour of Karma and again both sets of tests are run, with a CTRL-C required to get to the 2nd phase running the library tests. 但这似乎对Karma的行为没有影响,并且再次运行两组测试,需要CTRL-C才能进入运行库测试的第二阶段。 This seems to be a very cumbersome workflow and I hope there is a way round this, so I can just run the library tests straight away. 这似乎是一个非常麻烦的工作流程,我希望有一个方法,所以我可以立即运行库测试。

Let's say I inserted another library into the workspace, so now there are 2 component libraries and an app. 假设我将另一个库插入工作区,所以现在有2个组件库和一个应用程序。 Does that mean I would have to run first the app tests, then vice-lib tests, then the new library tests? 这是否意味着我必须首先运行应用程序测试,然后是副lib测试,然后是新的库测试? I think you can see where I'm going with this and it just doesn't seem to be right, unless I'm doing something wrong (most likely!). 我想你可以看到我的目标,这似乎是不对的,除非我做错了什么(最有可能!)。

I have a default test.ts file which is unchanged from the default which includes the line (for finding the test cases to run): 我有一个默认的test.ts文件,该文件与默认值相同,包括该行(用于查找要运行的测试用例):

// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);

but I'm not sure how I could change this on a per library level. 但我不确定如何在每个库级别上更改此设置。

I was hoping that I could define multiple entries in package.json: scripts, something like: 我希望我可以在package.json:scripts中定义多个条目,例如:

"test:app": "ng test",
"test:vice": "ng test --<some option to refer to .projects/vice-lib>"

so that I could directly run the tests for the library I want. 这样我就可以直接运行我想要的库的测试了。

So how is Karma running here, and is it not designed to work with multiple projects in a workspace? 那么Karma如何在这里运行,是不是设计用于工作区中的多个项目?

UPDATE: I also tried to use fdescribe on one of teh vice-lib test fixtures, but this had no effect other than a warning on the console: 更新:我还尝试在其中一个副lib测试装置上使用fdescribe ,但这除了控制台上的警告之外没有任何效果:

Chrome 67.0.3396 (Mac OS X 10.13.4) ERROR: 'DEPRECATION: fit and fdescribe will cause your suite to report an 'incomplete' status in Jasmine 3.0' Chrome 67.0.3396(Mac OS X 10.13.4)错误:'弃用:适合和fdescribe会导致您的套件在Jasmine 3.0中报告“不完整”状态

If you need to run only tests for the library, you can use ng test library-name , like you use a build command to generate the dist files. 如果只需要运行库的测试,则可以使用ng test library-name ,就像使用build命令生成dist文件一样。 Is explained here . 这里解释一下

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

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