1

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

enter image description here

skyboyer
  • 22,209
  • 7
  • 57
  • 64
rmcsharry
  • 5,363
  • 6
  • 65
  • 108

2 Answers2

0

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 
  • Thanks but I don't wish to install Jest globally as I have many Angular projects all using different versions of Angular and different versions of Jest. – rmcsharry May 10 '20 at 20:59
  • In that case you can use - ./node_modules/jest/bin/jest.js -f shared.service.spec.ts ./node_modules/jest/bin/jest.js --testPathPattern=src/app/shared/components – Abhinesh Gour May 10 '20 at 21:07
  • 1
    Thanks again but it seems I have to use "ng test" -> Jest on its own does not recognise all the decorators used by Angular and just throws errors on every test. – rmcsharry May 10 '20 at 22:17
0

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.

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • 2
    Does not work for me, returns Unknown option: 'HomeComponent' – fires3as0n Mar 19 '21 at 09:22
  • 2
    What partially helped is ng test --testNamePattern="HomeComponent" where HomeComponent is the name of the test suite, not the name of the component. It still runs all the files but skips test suites that do not match the provided name. – fires3as0n Mar 19 '21 at 10:07