Jasmine: "Incomplete: No specs found" in Angular Typescript project
Asked Answered
S

9

29

For some reason I can't understand, Karma says that Jasmine can't find any of my test specs. I'm using Angular 9, Typescript and ng test to run the tests. I also ran jasmine init to create the jasmine.json config file. I've tried several config changes, creating a dummy test javascript file and more but I'm still getting the same "No specs found" message. This is getting really frustrating and I'm sure I'm just overlooking something obvious here.

Tests folder structure:

spec
    services
        address.service.spec.ts
        login.service.spec.ts
        ...
     support
         jasmine.json

jasmine.json:

{
  "spec_dir": "spec",
  "spec_files": [
  "**/*[sS]pec.js", // tried with "**/*[sS]pec.ts"
  "services/*[sS]pec.js" // tried with "services/*[sS]pec.ts"
  ],
  "helpers": [
  "helpers/**/*.js" // tried with "helpers/**/*.ts"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": false
}

karma.conf.js :

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular-devkit/build-angular/plugins/karma'),
        ],
        client: {
            clearContext: false, // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(
                __dirname,
                './coverage/censored',
            ),
            reports: ['html', 'lcovonly', 'text-summary'],
            fixWebpackSourcePaths: true,
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false,
        restartOnFileChange: true,
    });
};
Septuagenarian answered 6/12, 2019 at 12:40 Comment(2)
As Angular 9 is not released yet, i advice you to open an issue to github as more people may encounter such case and it needs to be resolved by the core team.Dulcedulcea
I should probably verify, if it works with Angular 8 beforehand.Septuagenarian
S
0

Solved it. Once i moved the .spec files into the /src folder, Jasmine detected them without any problems.

Septuagenarian answered 11/12, 2019 at 12:25 Comment(0)
E
20

I had this error today, for me it was that I had a syntax-level error in my .ts files, so they couldn't really compile.

In above situation, I think, the tests should not start (and instead show compile error).

But for whatever reason, somehow the tests start, and Jasmine fails with "No specs found" log.

Edwin answered 17/2, 2021 at 19:35 Comment(0)
A
5

In my case, I saw some errors in the terminal when I run the

ng test

but when I open the web app in the browser it showed no specs found.

ERROR IN PROJECT

enter image description here

this was the error. After fixing the above error it showed all the specs with the result of executed test cases

Ardrey answered 20/3, 2021 at 16:25 Comment(3)
Same here, basically the tests only run after those errors have been fixedGlitter
@ArmandoGuarino guys, how do you fix these errors? I have a lot of them in the console when I run ng test. There are no such errors when starting the project.Humerus
Not sure what sort of errors you have, @Vladislav. What I can tell is that until all of those are fixed, Jasmine will yell No specs found.Glitter
N
1

I had a similar issue and apparently it was due to an invalid import.
This was the specific import: import 'rxjs/add/observable/throw'

I just removed the import and everything worked fine.

ng test seemed to execute just fine, so it was a little confusing.

Nisbet answered 30/11, 2022 at 16:44 Comment(0)
O
1

I had this issue appear while I was making code changes in VSCode. I stashed my changes to confirm that the tests still run with the original version. They did. When I reapplied my stash, the tests still worked.

Seems totally random, but maybe this will help someone else who encounters this issue.

Onus answered 28/6, 2023 at 14:0 Comment(0)
S
0

Solved it. Once i moved the .spec files into the /src folder, Jasmine detected them without any problems.

Septuagenarian answered 11/12, 2019 at 12:25 Comment(0)
M
0

I had the same issue but in my case I upgraded from angular 9 to angular 10 and tests stopped running, I was getting a lot of:

src/app/xxxxx/basic-info/basic-info.component.spec.ts:391:9 - error TS2532: Object is possibly 'undefined'.

391 this.whatToDelete = ["mumbo", "jumbo"];

So in the 'describe' I changed the arrow function to a regular function because an arrow function does not have it's own 'this'

*

Unlike regular functions, arrow functions do not have their own this . The value of this inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of this in the closest non-arrow parent function.

Hope this can help one of you and save you some time

Mcmullin answered 25/10, 2020 at 9:35 Comment(0)
E
0

Once I moved the .spec file into the the /spec dir it found the tests.

Ethbun answered 19/3, 2021 at 1:5 Comment(0)
B
0

I had the same issue but in my case while compiling the " ng test --watch --code-coverage --include=/data-builder.component.spec.ts" I have given passing the "ng test --watch --code-coverage --include=/data-builder.component.ts"

this error also accrued during mismatches of the path

Bathometer answered 9/10, 2023 at 11:35 Comment(0)
S
0

My issue ended up being in the test.ts file after a migration.

It appears that zone.js reworked its folder/exports structure at some point and the angular cli at the time of my migration wasn't in-sync with that change.

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

// import 'zone.js/dist/zone-testing'; <-- Does not work
import 'zone.js/testing'; // <-- Works

// ...
Symphonize answered 8/4 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.