[英]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:在我们的项目中,为了减少运行测试的数量,并在开发期间仅针对特定文件运行特定测试,我们使用我们自己的脚本和这种技术:
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"
]
}
}
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
文件,我们不再需要指示它这样做.
"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测试——是强制只运行那些测试。
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.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.