且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular CLI-如何在src文件夹之外拾取.spec.ts文件?

更新时间:2022-12-07 18:45:34

在tsconfig.spec.json

In tsconfig.spec.json

 "include": [
    "../*.spec.ts", // your spec file location parent of src or ../yourpath/
    "**/*.spec.ts",
    "**/*.d.ts"
  ] 

在test.ts

In test.ts

如果您选择父目录,它也会选择其他规范,因此当选择src的父目录,然后提供您的组件名称或指定您的规格的确切路径

if you pick parent dir it picks the other specs as well, hence when picking the parent dir of src then give your component name or specify the exact path of your spec

 const context1 = require.context('./', true, /\.spec\.ts$/);//
 const context = require.context('../', true, /app\.component\.spec\.ts$/);//
 context.keys().map(context);
 context1.keys().map(context1);

当您在src的父文件夹中时,同一上下文正在拾取node_modules中的文件,因此我们需要提及spec.ts名称.

When you are in the parent folder of src the same context is picking up the files which are in node_modules,hence we need to mention the spec.ts name.

spec.ts文件将像以前一样运行

spec.ts files which are in src will run like before

应用规范位于src的父级中,而游戏规范位于src文件夹中

app spec is in parent of src and game spec in is in src folder

如果只想指定根目录,则从那里开始选择所有规格文件,然后给出不同的命名约定与node_modules中的规范文件不同.说出哪些的src作为yourapp.componentout.spec.ts以及其中的内容insideapp.componentin.spec.ts,您可以给出一个路径,而不是两个目录

If you want to specify only the root directory and from there you want to pick all the spec files then we give a different naming convention which differs from the spec files in node_modules. Say which are out of src as yourapp.componentout.spec.ts and which are inside insideapp.componentin.spec.ts and you can give one path instead of two directories

const context = require.context('../', true, /out\.spec\.ts$|in\.spec\.ts$/);//
context.keys().map(context);

或以其他方式遵循仅针对src并放入

or the other way follow the naming convention for the files which are outside of src only and put

 const context1 = require.context('./', true, /\.spec\.ts$/);//
 const context = require.context('../', true, /out\.spec\.ts$/);//
 context.keys().map(context);
 context1.keys().map(context1);

如果您不想更改spec文件名,请提供模块位置的路径,而不是其父级,可以跳过node_modules部分.

if you don't want to change the spec file names , then give the path of your module location instead of its parent where you can skip the node_modules part.

基本上,我们可以根据需要更改以下内容

Basically, We can change the below according to our needs

  require.context(directory, useSubdirectories = false, regExp = /^\.\//)

希望这会有所帮助!

引用: https://webpack.js.org/guides/dependency-management/#require-context