简体   繁体   English

迁移到 Angular 15 后,上下文从 test.ts 中删除

[英]After migration to Angular 15, context was removed from test.ts

In our project in order to reduce the quantity of running tests and during development run the specific tests only for certain file we used our own script with this technique:在我们的项目中,为了减少运行测试的数量,并在开发期间仅针对特定文件运行特定测试,我们使用我们自己的脚本和这种技术:

StackOverflow 上的一个答案,得分为 124。答案的正文详细说明了如何将测试的通配符匹配器替换为仅匹配单个测试的匹配器。

However after the migration to Angular 15, this 'context' stuff was removed from test.ts file.但是,在迁移到 Angular 15 之后,这个“上下文”内容已从 test.ts 文件中删除。

Is there a way to still use the same technique in Angular 15?有没有办法在 Angular 15 中仍然使用相同的技术?

I tried to return 'context' to the test.ts with no success.我试图将“上下文”返回到 test.ts 但没有成功。

test.ts file is no longer generated by angular, and the property main used to link the file is no longer allowed in angular.json, you can use the include property in angular.json to add patterns or file name angular不再生成test.ts文件,并且angular.json中不再允许用于链接文件的属性main ,您可以使用angular.json中的include属性添加模式或文件名

"test": {
    ...
    "include": [
      "**/app.component.spec.ts"
    ]
  }
}

Simple solution: just remove the call to require.context简单的解决方案:只需删除对require.context的调用

I just faced the same problem, converting an app from Angular 14 to 15, and it turns out the solution is even simpler: just delete the two lines at the bottom of src/test.ts .我刚刚遇到了同样的问题,将一个应用程序从 Angular 14 转换为 15,结果证明解决方案更简单:只需删除src/test.ts底部的两行。 The call to require.context() is no longer needed.不再需要调用require.context()

I used to have我曾经有

const context = require.context("./", true, /\.spec\.ts$/);
context.keys().forEach(context);

at the end of src/test.ts , but now I have deleted those two lines and my tests all run just like they used to with Angular 14.src/test.ts的末尾,但现在我已经删除了这两行,我的测试都像以前那样运行 Angular 14。

I could not find proper documentation for this change (that's why I landed on this question in the first place) but my assumption is that Karma now automatically finds all the *.spec.ts files and we no longer need to direct it to do so.我找不到适合此更改的文档(这就是我首先提出这个问题的原因)但我的假设是 Karma 现在会自动找到所有*.spec.ts文件,我们不再需要指示它这样做.

Narrow the test suites with "include"使用"include"缩小测试套件

The "include" property, added under "test": / "options": , is only needed if you want to restrict the list of spec files to run."test": / "options":下添加的"include"属性仅在您想限制要运行的规范文件列表时才需要。

So, according to my tests, configuring angular.json like this:因此,根据我的测试,像这样配置angular.json

   "test": {
     "options": {
        "include": ["**/*.spec.ts"],
        ...
     },
     ...

or the same without "include" , all spec files are exercised.或没有"include" ,所有规范文件都被执行。

But configuring angular.json like this:但是像这样配置angular.json

   "test": {
     "options": {
        "include": ["**/app.component.spec.ts"],
        ...
     },
     ...

only spec files call app.component.spec.ts actually get exercised.只有调用app.component.spec.ts spec 文件才真正得到执行。

I don't know why you'd want to restrict which spec files are exercised, but I think it's worth documenting this here to understand what is going on.我不知道您为什么要限制执行哪些规范文件,但我认为值得在此处进行记录以了解发生了什么。

Another approach to running specific tests during development - since these are jasmine tests - is to f orce just those to run.在开发期间运行特定测试的另一种方法——因为这些是jasmine测试——是强制只运行那些测试。

  1. Change describe(…) to fdescribe(…) (and/or it(…) to fit(…) ) for the specific specs you are working on. describe(…)更改为fdescribe(…) (和/或it(…)更改为fit(…) )以适应您正在处理的特定规范。 Only these will be run.只有这些会运行。
  2. Get them working.让他们工作。
  3. Change fdescribe(…) / fit(…) back to describe(…) / it(…) . fdescribe(…) / fit(…)改回describe(…) / it(…) All the specs will run again.所有规格将再次运行。

There may be similar functionality for other testing frameworks.其他测试框架可能有类似的功能。

Note: Similarly, specs can be e x cluded with xdescribe(…) and xit(…) .注意:类似地,规格可以包含在xdescribe xdescribe(…)xit(…)中。

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

相关问题 从angular4升级到角度5后,npm测试失败“TypeScript编译中缺少test.ts” - npm test fails after upgrade from angular4 to angular 5 “test.ts is missing from the TypeScript compilation” 如何覆盖 Angular 项目中的 test.ts 文件 - How to cover the test.ts file in an Angular project angular - ReferenceError:在http:// localhost:9877src / test.ts中找不到变量:URLSearchParams - angular - ReferenceError: Can't find variable: URLSearchParams in http://localhost:9877src/test.ts Angular 6单元测试:如何使用test.ts进行业力测试 - Angular 6 Unit testing : how to use test.ts for running karma tests 运行ng测试时如何解决test.ts? - How to resolve test.ts when running ng test? 正则表达式:所有以.ts结尾且没有单词index.ts和test.ts的字符串 - Regex: All strings that finishes with .ts and does not have the word index.ts and test.ts 找不到条目模块中的错误:错误:无法解析/src/test.ts - ERROR in Entry module not found: Error: Can't resolve /src/test.ts 从 Angular 8 迁移后的 Angular 9 编译错误 - Angular 9 compilation errors after migration from Angular 8 Angular 8 从 Angular 7 迁移后构建失败 - Angular 8 fails to build after migration from Angular 7 从Angular2 beta1迁移到Angular2 beta15-.map()错误 - Migration from Angular2 beta1 to Angular2 beta15 - .map() error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM