Angular CLI-如何在src文件夹之外拾取.spec.ts文件? [英] Angular CLI - How to pick up .spec.ts files outside of the src folder?

查看:49
本文介绍了Angular CLI-如何在src文件夹之外拾取.spec.ts文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Angular CLI项目,其 NgModule 位于/src 文件夹的外部(最终,该NgModule将打包为npm模块,但现在我是只是将其保留在/src 之外).

I have an Angular CLI project with a NgModule located outside of the /src folder (eventually this NgModule will be packaged as a npm module but for now I'm just leaving it outside of /src).

我正在尝试为此NgModule编写单元测试,但是 ng测试似乎没有在/之外拾取 .spec.ts 文件src 文件夹.

I'm trying to write unit tests for this NgModule but ng test doesn't seem to pick up .spec.ts files outside of the /src folder.

我尝试更改/src/test.ts 中的路径:

// Original
const context = require.context('./', true, /\.spec\.ts$/);

// Does not work
const context = require.context('../', true, /\.spec\.ts$/);

// Does not work
const context = require.context('/absolute/path/to/ngmodule', true, /\.spec\.ts$/);

但是我得到一个错误:模块构建失败:错误:/absolute/path/to/file.spec.ts不是编译的一部分.请通过文件"或包含"属性确保它在您的tsconfig中.

But I get an error: Module build failed: Error: /absolute/path/to/file.spec.ts is not part of the compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.

我还尝试过在 tsconfig.spec.json include 属性中将路径添加到我的NgModule:

I have also tried adding the path to my NgModule in the include property of tsconfig.spec.json:

"include": [
  "../my-ng-module/**/*.spec.ts",  // Added this
  "**/*.spec.ts",
  "**/*.d.ts"
]

但是没有成功.有什么想法吗?

But no success. Any ideas?

推荐答案

在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

这篇关于Angular CLI-如何在src文件夹之外拾取.spec.ts文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆