Haha TTpro
Haha TTpro

Reputation: 5546

How to print debug message in Angular test?

Edit: this question is totally wrong because Angular DO NOT call http request in unit test. You cannot print the truly response from request. The unit test "fake" the response of the request then put into place. If you come into same question as I do, please read documentation on Angular unit test again.


I want to console.log("my text") or something similar in unittest.

the-cat-api.service.ts

import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class TheCatApiService {
  private catApi="05f72f39-87d9-4166-b4f1-6dfb55db1744"
  private REST_API = "https://api.thecatapi.com/v1/images/search?api_key="
  constructor(private httpClient: HttpClient) { }

  getCat(){
    return this.httpClient.get(this.REST_API + this.catApi);
  }
  
}

the-cat-api.service.spec.ts

import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import {HttpClientModule} from '@angular/common/http';
import { TheCatApiService } from './the-cat-api.service';

describe('TheCatApiService', () => {
  let service: TheCatApiService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule], 
      providers: [TheCatApiService]
    });
    service = TestBed.inject(TheCatApiService);
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should return cat', () =>{
    service.getCat().subscribe((data) => {
      console.log("test the cat api ")
      console.log(data)
    })
  })
});

When I run ng test it show

➜  learn-service git:(master) ✗ ng test                           
10% building 2/2 modules 0 active24 07 2020 13:58:33.558:WARN [karma]: No captured browser, open http://localhost:9876/
24 07 2020 13:58:33.562:INFO [karma-server]: Karma v5.0.9 server started at http://0.0.0.0:9876/
24 07 2020 13:58:33.563:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
24 07 2020 13:58:33.567:INFO [launcher]: Starting browser Chrome
24 07 2020 13:58:39.580:WARN [karma]: No captured browser, open http://localhost:9876/
24 07 2020 13:58:39.724:INFO [Chrome 84.0.4147.89 (Linux x86_64)]: Connected on socket MWHJE0_oK6DZDg3_AAAA with id 30651510
WARN: 'Spec 'TheCatApiService should return cat' has no expectations.'
Chrome 84.0.4147.89 (Linux x86_64): Executed 5 of 6 SUCCESS (0 secs / 0.276 secs)
Chrome 84.0.4147.89 (Linux x86_64): Executed 6 of 6 SUCCESS (0.409 secs / 0.282 secs)

I check developer console of chrome (Karma test framework) and see no console log.

Upvotes: 2

Views: 7055

Answers (2)

Haha TTpro
Haha TTpro

Reputation: 5546

This question is totally wrong because Angular DO NOT call http request in unit test. You cannot print the truly response from request. The unit test "fake" the response of the request then put into place. If you come into same question as I do, please read documentation on Angular unit test again.

https://angular.io/guide/http#testing-http-requests

Upvotes: 0

huan feng
huan feng

Reputation: 8623

Refer to this thread for more information:

https://github.com/karma-runner/karma-mocha/issues/47

karma.conf.js:

client: {
  captureConsole: true,
  mocha: {
    bail: true
  }
}

Upvotes: 2

Related Questions