Skip to content

Uncaught ReferenceError: beforeEach is not defined #51382

Closed

Description

@abdurrahmanar

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

After updated Angular version 15 to 16.2 while application starting testing.mjs throws error.

Uncaught ReferenceError: beforeEach is not defined

image

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Uncaught ReferenceError: beforeEach is not defined

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 16.2.0
Node: 16.15.0
Package Manager: npm 9.6.7
OS: win32 x64

Angular: 16.2.0
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, material, platform-browser, platform-browser-dynamic
... router

Package                         Version
"@angular/animations": "^16.2.0",
    "@angular/cdk": "^16.2.0",
    "@angular/common": "^16.2.0",
    "@angular/compiler": "^16.2.0",
    "@angular/core": "^16.2.0",
    "@angular/forms": "^16.2.0",
    "@angular/localize": "^16.2.0",
    "@angular/platform-browser": "^16.2.0",
    "@angular/platform-browser-dynamic": "^16.2.0",
    "@angular/router": "^16.2.0",
    "@angular/service-worker": "^16.2.0",
    "@babel/core": "^7.22.10",
     *
     *
     *
   "rxjs": "~7.6.0",
    "sass-loader": "10.4.1",
    "shake.js": "^1.2.2",
    "subsink": "^1.0.0",
    "tabulator-tables": "^5.5.0",
    "tempusdominus-bootstrap-4": "^5.39.0",
    "tslib": "^2.6.1",
    "utif": "^3.1.0",
    "uuid": "^8.3.2",
    "validate.js": "^0.13.1",
    "@types/node": "^18.17.0",
    "@types/signalr": "^2.2.35",
    "@typescript-eslint/eslint-plugin": "6.4.0",
    "@typescript-eslint/parser": "6.4.0",
    "cordova-plugin-camera": "^4.0.3",
    "vis-network": "^5.4.1",
    "wnumb": "^1.2.0",
    "xlsx": "^0.17.5",
    "zone.js": "~0.13.1"

  "devDependencies": {
    "@angular-devkit/build-angular": "^16.2.0",
    "@angular-devkit/schematics": "^16.2.0",
    "@angular-eslint/builder": "16.1.0",
    "@angular-eslint/eslint-plugin": "16.1.0",
    "@angular-eslint/eslint-plugin-template": "16.1.0",
    "@angular-eslint/schematics": "16.1.0",
    "@angular-eslint/template-parser": "16.1.0",
    "@angular/cli": "^16.2.0",
    "@angular/compiler-cli": "^16.2.0",
    "@angular/language-service": "^16.2.0",
    "eslint": "^8.47.0",
    "jasmine-core": "^5.1.0",
    "jasmine-spec-reporter": "^7.0.0",
    "karma": "^6.4.2",
    "karma-chrome-launcher": "^3.2.0",
    "karma-coverage-istanbul-reporter": "^3.0.3",
    "karma-jasmine": "^5.1.0",
    "karma-jasmine-html-reporter": "^2.1.0",
    "karma-viewport": "^1.0.9",
    "protractor": "~7.0.0",

Anything else?

No response

Activity

  1. JoostK commented on Aug 16, 2023

    @JoostK
    Member

    Your production code (route-state.service.ts) seems to be importing code that eventually (through router.util.ts) imports from a testing module, which is unexpected. I don't know how the update to Angular 16 has made a difference here, but such setup is to be avoided.

  2. JoostK commented on Aug 16, 2023

    @JoostK
    Member

    It was changed in #50063. I'd be interested to learn your exact setup to determine if that is a reasonable thing to do or not.

  3. abdurrahmanar commented on Aug 16, 2023

    @abdurrahmanar
    Author

    this is router.util.ts, it's not importing any test code;
    @JoostK

    export class RouterUtil {
        static getRouterData(state, parent, propertyName: string): Array<any> {
            let data = [];
            if (parent && parent.snapshot.data && parent.snapshot.data[propertyName] && parent.component) {
                data.push({
                    url: '/' + RouterUtil.getFullUrl(parent.snapshot).reverse().join('/'),
                    property: parent.snapshot.data[propertyName]
                });
            }
    
            if (state && parent) {
                data.push(...RouterUtil.getRouterData(state, state.firstChild(parent), propertyName));
            }
            return data;
        }
    
        static getFullUrl(snapshot): string[] {
            let data = [];
            if (snapshot.routeConfig && snapshot.routeConfig.path) {
                let url = snapshot.routeConfig.path;
                if (snapshot.params) {
                    Object.keys(snapshot.params).forEach((key: string) => {
                        url = url.replace(':' + key, snapshot.params[key]);
                    });
                }
                data.push(url);
            }
    
            if (snapshot.parent) {
                data.push(...RouterUtil.getFullUrl(snapshot.parent));
            }
            return data;
        }
    }
    
  4. JeanMeche commented on Aug 16, 2023

    @JeanMeche
    Member

    Are there no imports in your file ?

  5. JoostK commented on Aug 16, 2023

    @JoostK
    Member

    The stack trace seems to indicate that there is an import (somehow). Could you share a runnable reproduction that showcases the issue? With just code snippets we aren't able to investigate, unfortunately.

  6. added
    needs reproduction
    on Aug 16, 2023
  7. abdurrahmanar commented on Aug 16, 2023

    @abdurrahmanar
    Author

    Are there no imports in your file ?

    yes, it's just utility class, there are two functions in this class.

  8. added
    area: testing
    on Aug 16, 2023
  9. added this to the Backlog milestone on Aug 16, 2023
  10. JeanMeche commented on Aug 16, 2023

    @JeanMeche
    Member

    Then you'll have to provide a reproduction (on stackblitz or a github repo). There is nothing actionnable right now with the information you gave us !

  11. kussmaul commented on Aug 16, 2023

    @kussmaul

    FWIW, I got the same error today when updating 16.1 to 16.2 (no error with 16.1).
    After the normal update failed, I rolled back changes, and updated just @angular/core (not cdk, cli, material) and got the same error.

    Uncaught ReferenceError: beforeEach is not defined
        at 46517 (testing.mjs:27113:1)
        at __webpack_require__ (bootstrap:19:1)
        at 87783 (router.mjs:6964:1)
        at __webpack_require__ (bootstrap:19:1)
        at 23830 (login.module.ts:19:35)
        at __webpack_require__ (bootstrap:19:1)
        at 79804 (abs.component.ts:223:27)
        at __webpack_require__ (bootstrap:19:1)
        at 28746 (app.component.ts:88:3)
        at __webpack_require__ (bootstrap:19:1)
    
    46517			@	testing.mjs:27113
    __webpack_require__	@	bootstrap:19
    87783			@	router.mjs:6964
    __webpack_require__	@	bootstrap:19
    23830			@	login.module.ts:19
    __webpack_require__	@	bootstrap:19
    79804			@	abs.component.ts:223
    __webpack_require__	@	bootstrap:19
    28746			@	app.component.ts:88
    __webpack_require__	@	bootstrap:19
    14913			@	environment.ts:23
    __webpack_require__	@	bootstrap:19
    __webpack_exec__	@	lib|url:1
    (anonymous)		@	lib|url:1
    __webpack_require__.O	@	chunk loaded:23
    (anonymous)		@	lib|url:1
    webpackJsonpCallback	@	jsonp chunk loading:34
    (anonymous)		@	main.js:1
    

    Current versions:

    $ ng version
    
    Angular CLI: 16.2.0
    Node: 16.18.1
    Package Manager: npm 8.18.0
    OS: linux x64
    
    Angular: 16.2.1
    ... animations, common, compiler, compiler-cli, core, forms
    ... platform-browser, platform-browser-dynamic, router
    
    Package                         Version
    ---------------------------------------------------------
    @angular-devkit/architect       0.1602.0
    @angular-devkit/build-angular   16.2.0
    @angular-devkit/core            16.2.0
    @angular-devkit/schematics      16.2.0
    @angular/cdk                    16.2.0
    @angular/cli                    16.2.0
    @angular/material               16.2.0
    @schematics/angular             16.2.0
    rxjs                            7.8.1
    typescript                      5.1.6
    zone.js                         0.13.1
    

    Before update:

    $ npm outdated
    Package                            Current  Wanted  Latest  Location                                        Depended by
    @angular-devkit/build-angular       16.1.6  16.2.0  16.2.0  node_modules/@angular-devkit/build-angular      gilt
    @angular/animations                 16.1.7  16.2.1  16.2.1  node_modules/@angular/animations                gilt
    @angular/cdk                        16.1.6  16.2.0  16.2.0  node_modules/@angular/cdk                       gilt
    @angular/cli                        16.1.6  16.2.0  16.2.0  node_modules/@angular/cli                       gilt
    @angular/common                     16.1.7  16.2.1  16.2.1  node_modules/@angular/common                    gilt
    @angular/compiler                   16.1.7  16.2.1  16.2.1  node_modules/@angular/compiler                  gilt
    @angular/compiler-cli               16.1.7  16.2.1  16.2.1  node_modules/@angular/compiler-cli              gilt
    @angular/core                       16.1.7  16.2.1  16.2.1  node_modules/@angular/core                      gilt
    @angular/forms                      16.1.7  16.2.1  16.2.1  node_modules/@angular/forms                     gilt
    @angular/material                   16.1.6  16.2.0  16.2.0  node_modules/@angular/material                  gilt
    @angular/platform-browser           16.1.7  16.2.1  16.2.1  node_modules/@angular/platform-browser          gilt
    @angular/platform-browser-dynamic   16.1.7  16.2.1  16.2.1  node_modules/@angular/platform-browser-dynamic  gilt
    @angular/router                     16.1.7  16.2.1  16.2.1  node_modules/@angular/router                    gilt
    
    
  12. JeanMeche commented on Aug 16, 2023

    @JeanMeche
    Member

    If you enable the sourcemap you'll be able to tell us which file is throwing. Set "sourceMap": { "scripts": true, "vendor": true } in your angular.json.

  13. kussmaul commented on Aug 16, 2023

    @kussmaul

    @JeanMeche, thank you for the suggestion. Here's what I get:

    Uncaught ReferenceError: beforeEach is not defined
        at 46517 (test_hooks.ts:19:1)
        at __webpack_require__ (bootstrap:19:1)
        at 87783 (router.ts:3:4)
        at __webpack_require__ (bootstrap:19:1)
        at 23830 (login.module.ts:19:35)
        at __webpack_require__ (bootstrap:19:1)
        at 79804 (abs.component.ts:223:27)
        at __webpack_require__ (bootstrap:19:1)
        at 28746 (app.component.ts:88:3)
        at __webpack_require__ (bootstrap:19:1)
    46517			@	test_hooks.ts:19
    __webpack_require__	@	bootstrap:19
    87783			@	router.ts:3
    __webpack_require__	@	bootstrap:19
    23830			@	login.module.ts:19
    __webpack_require__	@	bootstrap:19
    79804			@	abs.component.ts:223
    __webpack_require__	@	bootstrap:19
    28746			@	app.component.ts:88
    __webpack_require__	@	bootstrap:19
    14913			@	environment.ts:23
    __webpack_require__	@	bootstrap:19
    __webpack_exec__	@	lib|url:1
    (anonymous)		@	lib|url:1
    __webpack_require__.O	@	chunk loaded:23
    (anonymous)		@	lib|url:1
    webpackJsonpCallback	@	jsonp chunk loading:34
    (anonymous)		@	main.js:1
    
    
  14. JoostK commented on Aug 16, 2023

    @JoostK
    Member

    @JeanMeche I'd suggest that beforeEach(getCleanupHook(false)); (and the other one) is changed to globalThis.beforeEach?.(getCleanupHook(false));, given that the change in #50063 is legitimately breaking since the call is now unguarded where it used to be guarded.

  15. JeanMeche commented on Aug 16, 2023

    @JeanMeche
    Member

    Yes I was thinking about adding that guard back. But it's weird that the content of this file can make it into a build.

  16. 9 remaining items

  17. kussmaul commented on Aug 17, 2023

    @kussmaul

    @JeanMeche
    You asked about @angular/core/testing - it's only imported in my .spec.ts files and in test.ts (required by Karma).

    I got the ReferenceError with ng serve. With ng build and the default builder, there are no build errors and http-server serves without errors. With ng build and esbuild (which I've never tried), there were some warnings (some of which I fixed), and http-server serves - not perfectly, but nothing like this issue.

    So (at least for me) the problem only occurs with ng serve, which wasn't clear from the earlier discussion. Thank you for catching this.

    I understand the value of a minimal repro. I will try to work on it, but have some pressing deadlines. :-)

  18. dzhavat commented on Aug 18, 2023

    @dzhavat
    Contributor

    Using Nx and upgraded from 15 to latest with Angular 16.2.0 yesterday also has this issue when import RouterTestingModule in storybook. Thought it was caused by new version of nx or storybook, but finally figured out this is from angular, fixed by downgrade to 16.1.9.

    I had the exact same issue after upgrading from 16.1.7 to 16.2.0, and using RouterTestingModule in Storybook. Fixed it by replacing RouterTestingModule with provideRouter + provideLocationMocks. (PR https://github.com/Energinet-DataHub/greenforce-frontend/pull/1964)

  19. added a commit that references this issue on Aug 21, 2023
    36f434e
  20. chriswoodie commented on Aug 24, 2023

    @chriswoodie

    I'm not sure this is entirely solved (or if this new error is even related). However, I'm still experiencing a similar error while serving my application after upgrading to v16.2.2:

    anchor.component.spec.ts:4 Uncaught ReferenceError: describe is not defined
        at 12635 (anchor.component.spec.ts:4:1)
        at __webpack_require__ (bootstrap:19:1)
        at 54904 (mydirective.directive.ts:19:37)
        at __webpack_require__ (bootstrap:19:1)
        at 1560 (main.js:17:71)
        at __webpack_require__ (bootstrap:19:1)
        at 59829 (app.routes.ts:3:37)
        at __webpack_require__ (bootstrap:19:1)
        at __webpack_exec__ (util.inspect:1:1)
        at util.inspect:1:1
    

    With the spec file itself looking like this:

    import { ComponentFixture, TestBed } from '@angular/core/testing';
    import { provideRouter } from '@angular/router';
    import { LibAnchorComponent } from './anchor.component';
    
    describe('LibAnchorComponent', () => {
      let component: LibAnchorComponent;
      let fixture: ComponentFixture<LibAnchorComponent>;
    
      beforeEach(async () => {
        await TestBed.configureTestingModule({
          providers: [
            provideRouter([])
          ]
        }).compileComponents();
    
        fixture = TestBed.createComponent(LibAnchorComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });
    
      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });
    

    Running the tests on their own works just fine.

  21. JeanMeche commented on Aug 24, 2023

    @JeanMeche
    Member

    This is a whole other issue. Why are your test files loaded/run by your app ?

  22. chriswoodie commented on Aug 24, 2023

    @chriswoodie

    @JeanMeche This is something I would like to know as well 😀 Would this be a problem with the tsconfig for the libraries that have spec files or where should I even start looking? I haven't changed anything that Nx generates, so I'm not sure what or where the problem comes from.

  23. JeanMeche commented on Aug 24, 2023

    @JeanMeche
    Member

    Could you be able to share a narrowed down repro on github ? That would ease the investigation.

  24. chriswoodie commented on Aug 24, 2023

    @chriswoodie

    @JeanMeche Here you go, removed as much "fluff" as I could:

    https://github.com/skog-newglue/describe-repro

    Run nx serve public and open console.

  25. JeanMeche commented on Aug 24, 2023

    @JeanMeche
    Member

    @skog-newglue In your case, I'm not sure libs/stylekit/src/index.ts should export the spec tests and the storybook stories. Removing them fixes your issue.

  26. chriswoodie commented on Aug 24, 2023

    @chriswoodie

    @JeanMeche Ahaaa, completely missed that those were included. The script which creates the index file should exclude them indeed. Thank you 🥇

  27. angular-automatic-lock-bot commented on Sep 24, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  28. locked and limited conversation to collaborators on Sep 24, 2023
  29. added a commit that references this issue on Jan 23, 2024
    b59066d
  30. added a commit that references this issue on Jan 31, 2024
    a9b3c00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testingneeds reproduction

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions