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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SplitButton causes ReferenceError: Cannot access 'TieredMenu' before initialization #12945

Closed
bendehghan opened this issue Apr 25, 2023 · 13 comments · Fixed by #13597
Closed
Labels
LTS-PORTABLE Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@bendehghan
Copy link

bendehghan commented Apr 25, 2023

Describe the bug

Looks like a new bug in 15.4.0
While running tests into my application I got the following error:

    ReferenceError: Cannot access 'TieredMenu' before initialization

      37 | import {RadioButtonModule} from 'primeng/radiobutton';
      38 | import {SpeedDialModule} from 'primeng/speeddial';
    > 39 | import {SplitButtonModule} from 'primeng/splitbutton';
         | ^
      40 | import {TabViewModule} from 'primeng/tabview';
      41 | import {TieredMenuModule} from 'primeng/tieredmenu';
      42 | import {ToggleButtonModule} from 'primeng/togglebutton';

      at Object.<anonymous> (node_modules/primeng/fesm2020/primeng-tieredmenu.mjs:195:243)
      at Object.<anonymous> (node_modules/primeng/fesm2020/primeng-splitbutton.mjs:8:21)
      at Object.<anonymous> (src/app/rc/dm-rc.module.ts:39:1)
      at Object.<anonymous> (src/app/rc/main-rc/main-rc.component.spec.ts:10:1)

Environment

Mac Jest tests.

Angular version

        "@angular/animations": "15.2.8",
        "@angular/cdk": "15.2.8",
        "@angular/common": "15.2.8",
        "@angular/compiler": "15.2.8",
        "@angular/core": "15.2.8",
        "@angular/forms": "15.2.8",
        "@angular/platform-browser": "15.2.8",
        "@angular/platform-browser-dynamic": "15.2.8",
        "@angular/router": "15.2.8",
        "@angular-builders/custom-webpack": "15.0.0",
        "@angular-builders/jest": "15.0.0",
        "@angular-devkit/architect": "0.1502.6",
        "@angular-devkit/build-angular": "15.2.6",
        "@angular-devkit/core": "15.2.6",
        "@angular-eslint/builder": "15.2.1",
        "@angular-eslint/eslint-plugin": "15.2.1",
        "@angular-eslint/eslint-plugin-template": "15.2.1",
        "@angular-eslint/schematics": "15.2.1",
        "@angular-eslint/template-parser": "15.2.1",
        "@angular/cli": "15.2.6",
        "@angular/compiler-cli": "15.2.8",
        "@angular/language-service": "15.2.8",

PrimeNG version

14.1.0

Build / Runtime

Angular CLI App

Node version (for AoT issues node --version)

v19.6.1

Browser(s)

CLI

Steps to reproduce the behavior

import {SplitButtonModule} from 'primeng/splitbutton';
npx jest --clearCache && npx jest --no-cache --updateSnapshot .

Things I've tried

Using primeng 15.3.0 and zone.js 12 works fine. With 15.4.0 and required zone.js 13 I get the new errors.

@github-actions github-actions bot added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Apr 25, 2023
@bendehghan
Copy link
Author

This is a similar bug:
#11931

@viceice
Copy link

viceice commented May 16, 2023

I got the same errors when importing MenubarModule or TieredMenuModule.

ReferenceError: Cannot access 'Menubar' before initialization
ReferenceError: Cannot access 'TieredMenu' before initialization

@bendehghan
Copy link
Author

Can somebody please triage this so we can at least get it on the radar? What is the triage process? Are we missing any info to get going?
Thank you :)

@blackholegalaxy
Copy link

ping @cagataycivici @cetincakiroglu

@cetincakiroglu cetincakiroglu added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Jun 7, 2023
@cetincakiroglu cetincakiroglu added this to the 16.0.0 milestone Jun 7, 2023
@cetincakiroglu
Copy link
Contributor

Thanks for reporting, we're investigating the issue.

@cetincakiroglu cetincakiroglu modified the milestones: 16.0.0, 16.0.1, 16.Future Jun 12, 2023
@pudrik
Copy link

pudrik commented Jul 21, 2023

I got the same issue on 16.0.2, both Menubar and TiredMenu. From what i can tell the errors manifest themselfs different depending on the order of the import statement

@pudrik
Copy link

pudrik commented Jul 24, 2023

Looking closer into this issue the problem seem to be the combination of angular and primeng version. for instance latest source and angular 16.1.5 works but not angular 16.1.6. Same applies for many other version combinations.

Depending on what enviornment youre using for testing a possible workaround is to use a preloader to patch any modules / barrell that causing the undesired error by removing the imports. (think extra step in workflow file)

example
/libs/ui/ui.module.ts <-- has a ref to TieredMenu normaly used in program run but not used in Unit test (Remove lines so tests can run)
/libs/ui/component/somecomponent/somecomponent.spec.ts <-- what gets the error, but dont have any ref to TieredMenu

simple sed operation would work.
sed -i '/TieredMenu/d' /libs/ui/ui.module.ts

@bhalash
Copy link

bhalash commented Aug 14, 2023

Adding that this is still an issue for me and my team, even with the latest 16.2.0 release. Are there any suggested workarounds in the interim?

@vespertilian
Copy link
Contributor

As of 16.1.0 this is also an issue with the Mega Menu. 16.0.2 works.

@psbenz
Copy link

psbenz commented Aug 24, 2023

Possible workaround:

jest.mock('primeng/splitbutton', () => ({
SplitButtonModule: jest.fn(),
}));
jest.mock('primeng/contextmenu', () => ({
ContextMenuModule: jest.fn(),
}));

@osmolo
Copy link

osmolo commented Aug 25, 2023

jest.mock('primeng/splitbutton', () => ({
SplitButtonModule: jest.fn(),
}));

Suggested workaround doesn't work for me.
mockConstructor does not have a module def (ɵmod property)
Unexpected value 'mockConstructor' imported by the module 'SharedModule'. Please add an @NgModule annotation. (SharedModule is my module that imports SplitButtonModule)

@psbenz
Copy link

psbenz commented Aug 25, 2023

I added the mocks in setup-jest.ts and it works for me

@osmolo
Copy link

osmolo commented Aug 25, 2023

I'm using Angular 16.2 and endend up mocking this way in setup-jest.ts:

@NgModule()
export class SplitButtonMockModule { }

jest.mock('primeng/splitbutton', () => ({
  SplitButtonModule: SplitButtonMockModule,
}));

@mertsincan mertsincan removed this from the 16.Future milestone Sep 2, 2023
@mertsincan mertsincan added this to the 16.3.2 milestone Sep 2, 2023
@mertsincan mertsincan linked a pull request Sep 2, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LTS-PORTABLE Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Projects
None yet
Development

Successfully merging a pull request may close this issue.