This question does not hold the answer for Angular 9/Jest, so looking for an up-to-date one.
Although the docs do mention it:
--include
It fails
First of all, try installing same version of Jest globally which is present in your package.json
. Using this command -
npm i -g jest@xx.yy.zz
Replace xx.yy.zz with the version mentioned in package.json
. Then run this command to test single file.
jest -f "shared.service.spec.ts"
Should work for a single file but still if you want to run all tests in one directory or folder use this command -
jest --testPathPattern=src/app/shared/components
OR
Simply run it from node_modules of your current angular project. Run these commands from your angular root folder.
./node_modules/jest/bin/jest.js -f shared.service.spec.ts
./node_modules/jest/bin/jest.js --testPathPattern=src/app/shared/components
The solution is to use
ng test -f <ComponentName>
where <ComponentName>
is the actual name of the component, not the path to it and not the name of the spec file, eg:
ng test -f HomeComponent
More info on angular github repo here. It explains that using Jest in Angular adds a custom builder, so that is why --include
is not supported.