Life cycle methods not invoked in unit tests

I’m following this unit testing example, referenced on the Ionic blog and I’ve found that the Ionic lifecycle methods (eg. ionViewDidLoad) do not get called when executing these tests. I modified page1 to set some text in ionViewDidLoad and then display that text on the page - a common pattern I use in my app and would like to test. However, the test fails because ionViewDidLoad does not get called. I was expecting either createComponent or detectChanges to invoke the Ionic lifecycle. Am I missing something?


import { Component } from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';

  selector: 'page-page1',
  templateUrl: 'page1.html'
export class Page1 {
  title: string = 'Ionic did not load';

  constructor(public navCtrl: NavController) {

  ionViewDidLoad(): void {
    this.title = 'Ionic loaded...';



    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    <ion-title>Page One</ion-title>

<ion-content padding>
  <h3>{{ title }}</h3>

  <img src="assets/icon/favicon.ico" alt="" />

    If you get lost, the <a href="">docs</a> will show you the way.

  <button ion-button secondary menuToggle>Toggle Menu</button>


import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By }           from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { Page1 } from './page1';
import { IonicModule, Platform, NavController} from 'ionic-angular/index';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

describe('Page1', () => {
  let de: DebugElement;
  let comp: Page1;
  let fixture: ComponentFixture<Page1>;

  beforeEach(async(() => {
      declarations: [Page1],
      imports: [
      providers: [

  beforeEach(() => {
    fixture = TestBed.createComponent(Page1);
    comp = fixture.componentInstance;
    de = fixture.debugElement.query(By.css('h3'));

  it('should create component', () => expect(comp).toBeDefined());

  it('should have expected <h3> text', () => {
    const h3 = de.nativeElement;
    expect(h3.innerText).toMatch('Ionic loaded...',
      '<h3> should say something about "Ionic"');

  it('should show the favicon as <img>', () => {
    const img: HTMLImageElement = fixture.debugElement.query(By.css('img')).nativeElement;


Error: Expected ‘Ionic did not load’ to match ‘Ionic loaded…’, ’

should say something about “Ionic”’.

1 Like

First, in my opinion with unit testing, you should be testing your code, and not what is being displayed. Testing elements in the DOM is usually done with end-to-end testing.

Second, when you test results of lifecycle methods, you actually need to call them. I know, seems weird, but there you are. This is how I’d test it:

it('should have correct title', () => {
    expect(component.title).toEqual('Ionic did not load');
    expect(component.title).toEqual('Ionic loaded...');

I always have this link open when I am working on unit tests:

Hope this helps.

That makes sense. I will look into end-to-end testing. Thanks for the info!