Angular CLI测试配置:分离单元测试与功能测试的执行
Got it, let's tackle this Angular test setup problem step by step. You want to keep your unit tests (.spec.ts) separate from functional tests (.func.spec.ts) so they don't interfere with each other—here's how to make it work smoothly:
1. Make ng test run only unit tests by default
Open your angular.json file, navigate to the projects -> [your-project-name] -> architect -> test -> options section. You have two straightforward ways to exclude functional tests:
Option A: Explicitly include only unit test files
Update the include array to target just .spec.ts files:
"include": [ "src/**/*.spec.ts" ]
Option B: Exclude functional test files
Add an exclude array to skip .func.spec.ts files:
"exclude": [ "src/**/*.func.spec.ts" ]
Either way, running ng test will now only execute your standard unit tests, no functional tests included.
2. Run functional tests independently
To run only your .func.spec.ts files, use the --include flag directly with ng test:
ng test --include="src/**/*.func.spec.ts"
Bonus: Add a shortcut command
If you don't want to type that long path every time, add a script to your package.json:
"scripts": { "test": "ng test", "test:func": "ng test --include=\"src/**/*.func.spec.ts\"" }
Now you can just run npm run test:func (or yarn test:func if you use Yarn) to kick off your functional tests whenever you need.
This setup keeps your regular test workflow clean and lets you run functional tests on demand, exactly what you're looking for!
内容的提问来源于stack exchange,提问作者Yoann B




