5

I want to test Angular project with 'ng test' command, but I'm obtaining error 'No project support the test target. Does anyone know what should I add in project dependencies to run tests with mentioned command? I don't want to create another project just to have those dependencies installed and copy code from existing project. Which libraries should I install? Npm install karma and jasmine is not enough, maybe I should add something in config files, but I don't know what should I add. Any help would be appreciated.

blackRose
  • 180
  • 1
  • 9
  • What's your current project structure? Is it a multi-project repo? Have you tried `ng test {project name}`? – Roddy of the Frozen Peas Nov 20 '19 at 14:48
  • It's not multi-project repo, it's only single project. I've tried ng test {project name}, but still not working. I've took a look inside package.json file and in scripts section I don't have "test": "ng test", so I think that some of dependencies are missed. – blackRose Nov 20 '19 at 14:52
  • 1
    No look in angular.json; that's where such script should be. – Roddy of the Frozen Peas Nov 20 '19 at 14:52
  • And I don't have in my project tsconfig.spec.json – blackRose Nov 20 '19 at 14:56
  • 2
    Sounds like you may have a minimal app? Is this relevant: https://stackoverflow.com/questions/50707187/add-tests-to-an-angular-minimal-cli-app – Roddy of the Frozen Peas Nov 20 '19 at 15:29
  • Thank you, It helped a lot :) Except from those steps I needed to add "test" definiton in angular.json file which looks like this: "test": {"builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.spec.json", "karmaConfig": src/karma.conf.js", "stylePreprocessorOptions": { "includePaths": [ "src/app/shared/styles"]}, "styles": ["src/styles.scss"],"scripts": [], "assets": [ "src/favicon.ico", "src/assets", "src/manifest.json"]} } – blackRose Nov 21 '19 at 08:01
  • And I've added "test": "ng test" in package.json file – blackRose Nov 21 '19 at 08:02

2 Answers2

1

Copying from blackRose's self-answer in comments:

Add something like the following to your angular.json, on the same level as the "build" entry in "architect".

"test": {
  "builder": "@angular-devkit/build-angular:karma",
  "options": {
    "main": "src/test.ts",
    "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.spec.json",
    "karmaConfig": "src/karma.conf.js",
    "stylePreprocessorOptions": {
      "includePaths": [ "src/app/shared/styles"]
    },
    "styles": ["src/styles.scss"],
    "scripts": [],
    "assets": [ "src/favicon.ico", "src/assets", "src/manifest.json"]
  }
}

Also add

"test": "ng test"

to the scripts part of package.json.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
  • Updateted to Angular 12 --> this is what you need to add to ANGULAR.JSON FILE "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "main": "src/test.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.spec.json", "karmaConfig": "karma.conf.js", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/styles.css" ], "scripts": [] } – Welyngton Dal Prá Nov 08 '21 at 18:48
1

I had similar issue and I used "npm run test" instead and that worked for me.

In my case it was an amateur mistake but that's how you learn i guess.

Carlos Gregorio
  • 396
  • 3
  • 6