Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage report shows 0% while all tests passed. #611

Closed
KenKeymolen opened this issue Nov 6, 2020 · 8 comments
Closed

Coverage report shows 0% while all tests passed. #611

KenKeymolen opened this issue Nov 6, 2020 · 8 comments

Comments

@KenKeymolen
Copy link

KenKeymolen commented Nov 6, 2020

馃悰 Bug Report

I'm unable to get a coverage report for my files. When I open the index.html file it's empty.
Can someone tell me what I'm doing wrong? I've setup everything as explained in the readme of Jest Preset Angular.

`
PASS src/app/pages/medication/subpages/week-view/week-view.component.spec.ts
PASS src/app/services/niss/niss.service.spec.ts
PASS src/app/pages/medication/subpages/day-view/day-view.component.spec.ts
PASS src/app/pages/medication/subpages/period-view/period-view.component.spec.ts
PASS src/app/services/user/user.service.spec.ts
PASS src/app/pages/valid-prescriptions/valid-prescriptions.component.spec.ts
PASS src/app/services/medication/medication.service.spec.ts
PASS src/app/ui-components/navigation-header/navigation-header.component.spec.ts
PASS src/app/pages/medication/medication.component.spec.ts
PASS src/app/pages/diary-notes/diary-notes.component.spec.ts
PASS src/app/pages/homepage/homepage.component.spec.ts
PASS src/app/pages/medication/subpages/complete-list/complete-list.component.spec.ts
PASS src/app/authentication/guards/auth.guard.spec.ts
PASS src/app/app.component.spec.ts
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------

Test Suites: 14 passed, 14 total
Tests: 17 passed, 17 total
Snapshots: 0 total
Time: 8.489 s
Ran all test suites.
`

my config in package.json:

"jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "<rootDir>/src/setup-jest.ts"
    ],
    "testPathIgnorePatterns": [
      "<rootDir>/src/test.ts",
      "<rootDir>/cypress/",
      "<rootDir>/src/environments/"
    ]
  }

To Reproduce

Steps to reproduce the behavior:

jest --coverage

Expected behavior

Jest generating a report showing the coverage for all files.

envinfo

System:
    OS: Windows

Angular 10

Npm packages:
    jest: 26.6.3
    jest-preset-angular: 8.3.2
    typescript: 4.0.2
@KenKeymolen KenKeymolen added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Nov 6, 2020
@ahnpnl
Copy link
Collaborator

ahnpnl commented Nov 6, 2020

Please see full discussion here kulshekhar/ts-jest#2090 :)

@ahnpnl ahnpnl added Upstream Bug and removed Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Nov 6, 2020
@KenKeymolen
Copy link
Author

KenKeymolen commented Nov 6, 2020

Thanks for your reply @ahnpnl
Upgrading gives me this issue: #527

@ahnpnl
Copy link
Collaborator

ahnpnl commented Nov 6, 2020

Do you mean test finishes but not exits ?

@KenKeymolen
Copy link
Author

Yes, it runs all tests correctly and keeps hanging on app.component.spec.ts giving following error: TypeError: Converting circular structure to JSON

@KenKeymolen
Copy link
Author

KenKeymolen commented Nov 6, 2020

I downgraded to jest: 25.5.4 and this solved TypeError: Converting circular structure to JSON
The coverage report seems fixed too, only some exclusions aren't working like the environment.ts file showing in the report.

"devDependencies": {
    "@angular-builders/jest": "^10.0.1",
    "@angular-devkit/build-angular": "~0.1001.7",
    "@angular/cli": "~10.1.7",
    "@angular/compiler-cli": "~10.1.6",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "concurrently": "^5.3.0",
    "cypress": "^5.5.0",
    "cypress-keycloak-commands": "^1.2.0",
    "jest": "^25.5.4",
    "jest-preset-angular": "^8.3.2",
    "protractor": "~7.0.0",
    "ts-mockito": "^2.6.1",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  },
  "jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "<rootDir>/src/setup-jest.ts"
    ],
    "testPathIgnorePatterns": [
      "<rootDir>/src/test.ts",
      "<rootDir>/cypress/",
      "<rootDir>/src/environments/"
    ]
  }

@ahnpnl
Copy link
Collaborator

ahnpnl commented Nov 6, 2020

The issue in #527 is actually partially related to jest. The example in #527 contains an invalid mock class. Maybe you can provide your repo and I can help to check. The hanging issue is related to test codes have problem.

@KenKeymolen
Copy link
Author

@ahnpnl I'm not allowed to share the repo but I can share the app.component.spec.ts

`
import { NissService } from './services/niss/niss.service';
import { UserService } from './services/user/user.service';
import { BnNgIdleService } from 'bn-ng-idle';
import { KeycloakService } from 'keycloak-angular';
import { GetWordFromSentence } from './shared/pipes/get-word-in-sentence.pipe';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { NavigationHeaderComponent } from './ui-components/navigation-header/navigation-header.component';
import { FormsModule } from '@angular/forms';
import { NgHttpLoaderModule } from 'ng-http-loader';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { instance, mock, when, anything, verify } from 'ts-mockito';
import { of } from 'rxjs';

describe('AppComponent', () => {
let mockTranslateService: TranslateService;
let mockKeycloakService: KeycloakService;
let mockIdleService: BnNgIdleService;
let mockUserService: UserService;
let mockNissService: NissService;
let component: AppComponent;
let fixture: ComponentFixture;

beforeEach(() => {
mockTranslateService = mock(TranslateService);
mockKeycloakService = mock(KeycloakService);
mockIdleService = mock(BnNgIdleService);
mockUserService = mock(UserService);
mockNissService = mock(NissService);

when(mockKeycloakService.getToken()).thenResolve("myToken");
when(mockIdleService.startWatching(anything())).thenReturn(of(false));
when(mockUserService.getPatient()).thenReturn(of(anything()));

});

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule,
NgHttpLoaderModule,
FormsModule,
TranslateModule.forRoot()
],
declarations: [
AppComponent,
NavigationHeaderComponent,
GetWordFromSentence
],
providers: [
{provide: TranslateService, useValue: instance(mockTranslateService)},
{provide: KeycloakService, useValue: instance(mockKeycloakService)},
{provide: BnNgIdleService, useValue: instance(mockIdleService)},
{provide: UserService, useValue: instance(mockUserService)},
{provide: NissService, useValue: instance(mockNissService)}
]
}).compileComponents();

fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;

});

it('should create the app', () => {
expect(component).toBeTruthy();
});

describe('ngOnInit', () => {
beforeEach(() => {
component.ngOnInit();
});

it('should check if user is idle', () => {
  verify(mockIdleService.startWatching(anything())).called();
});

});

describe('IdleService', () => {
it('should log the user out if he's idle', () => {
when(mockIdleService.startWatching(anything())).thenReturn(of(true));
component.ngOnInit();
verify(mockUserService.logoutUser()).called();
});

it('should keep the user logged in when the user isn\'t idle', () => {
  when(mockIdleService.startWatching(anything())).thenReturn(of(false));
  component.ngOnInit();
  verify(mockUserService.logoutUser()).never();
});

});
});

`

@ahnpnl
Copy link
Collaborator

ahnpnl commented Nov 6, 2020

maybe you can try with 9.0.0-next.1 at next tag. It might provide better errors. Don't forget to check CHANGELOG.md too to know how to migrate :)

@ahnpnl ahnpnl closed this as completed Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants