Skip to content

bug: Angular 13 Jest Tests fail while opening overlays: Cannot set property '$lazyInstance$' of undefined (present is not a function) #24590

@tobiloeb

Description

@tobiloeb

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x

Current Behavior

The error occurs when an alert or modal will be presented or any other function is called (e.g. onDidDismiss). Everything works fine when testing it in the browser, but if we run a jest test the following error is thrown:

Cannot set property '$lazyInstance$' of undefined
TypeError: Cannot set property '$lazyInstance$' of undefined
    at Object.registerInstance (/Users/.../node_modules/@ionic/core/dist/cjs/index-8138ba57.js:1988:90)
    at new Alert (/Users/.../node_modules/@ionic/core/dist/cjs/overlays-b91eb626.js:432:13)
    at Function.invokeTheCallbackFunction.construct (/Users/.../node_modules/jsdom/lib/jsdom/living/generated/CustomElementConstructor.js:27:30)
    at createElement (/Users/...node_modules/jsdom/lib/jsdom/living/helpers/create-element.js:204:33)
    at DocumentImpl.createElement (/Users/...node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:719:12)
    at Document.createElement (/Users/.../node_modules/jsdom/lib/jsdom/living/generated/Document.js:261:60)
    at /Users/.../node_modules/@ionic/core/dist/cjs/overlays-b91eb626.js:4073:32
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

We dig into this issue and see that some methods are not defined. We can create an alert with alert.create({})
but methods like present, onDidDismiss are not defined.

Expected Behavior

The modals or alerts can be used in jest test like in the browser. We can't reproduce this issue with ionic 5.

Steps to Reproduce

We have setup a very plain Angular 13 project with the angular cli. We added our Jest configuration to run the unit tests with Jest and added @ionic/angular 6.0.2

Everything is working fine except for alert.create().then(alert => alert.present())

Code Reproduction URL

https://github.com/tobiloeb/ionic6-jest-issue

Ionic Info

Ionic:

Ionic CLI : 5.4.16

Utility:

cordova-res : not installed
native-run : not installed

System:

NodeJS : v14.16.1
npm : 6.14.12
OS : macOS Big Sur

Additional Information

First we thought it is a jest problem, but everything is working fin except for the ionic overlays. And we found a very similar issue for ionic/vue: #24503
In our case we already import the AlertController from '@ionic/angular' and not from core.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions