Repository navigation
Closed
Labels
area: testingIssues related to Angular testing features, such as TestBedneeds reproductionThis issue needs a reproduction in order for the team to investigate further
Milestone
Description
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
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
Metadata
Metadata
Assignees
Labels
area: testingIssues related to Angular testing features, such as TestBedneeds reproductionThis issue needs a reproduction in order for the team to investigate further

Activity
JoostK commentedon Aug 16, 2023
Your production code (
route-state.service.ts) seems to be importing code that eventually (throughrouter.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.JoostK commentedon Aug 16, 2023
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.
abdurrahmanar commentedon Aug 16, 2023
this is router.util.ts, it's not importing any test code;
@JoostK
JeanMeche commentedon Aug 16, 2023
Are there no imports in your file ?
JoostK commentedon Aug 16, 2023
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.
abdurrahmanar commentedon Aug 16, 2023
yes, it's just utility class, there are two functions in this class.
JeanMeche commentedon Aug 16, 2023
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 !
kussmaul commentedon Aug 16, 2023
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(notcdk,cli,material) and got the same error.Current versions:
Before update:
JeanMeche commentedon Aug 16, 2023
If you enable the sourcemap you'll be able to tell us which file is throwing. Set
"sourceMap": { "scripts": true, "vendor": true }in yourangular.json.kussmaul commentedon Aug 16, 2023
@JeanMeche, thank you for the suggestion. Here's what I get:
JoostK commentedon Aug 16, 2023
@JeanMeche I'd suggest that
beforeEach(getCleanupHook(false));(and the other one) is changed toglobalThis.beforeEach?.(getCleanupHook(false));, given that the change in #50063 is legitimately breaking since the call is now unguarded where it used to be guarded.JeanMeche commentedon Aug 16, 2023
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.
9 remaining items
kussmaul commentedon Aug 17, 2023
@JeanMeche
You asked about
@angular/core/testing- it's only imported in my.spec.tsfiles and intest.ts(required by Karma).I got the ReferenceError with
ng serve. Withng buildand the default builder, there are no build errors andhttp-serverserves without errors. Withng buildandesbuild(which I've never tried), there were some warnings (some of which I fixed), andhttp-serverserves - 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. :-)
dzhavat commentedon Aug 18, 2023
I had the exact same issue after upgrading from 16.1.7 to 16.2.0, and using
RouterTestingModulein Storybook. Fixed it by replacingRouterTestingModulewithprovideRouter+provideLocationMocks. (PR https://github.com/Energinet-DataHub/greenforce-frontend/pull/1964)fix(core): guard the jasmine hooks (#51394)
chriswoodie commentedon Aug 24, 2023
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:
With the spec file itself looking like this:
Running the tests on their own works just fine.
JeanMeche commentedon Aug 24, 2023
This is a whole other issue. Why are your test files loaded/run by your app ?
chriswoodie commentedon Aug 24, 2023
@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.
JeanMeche commentedon Aug 24, 2023
Could you be able to share a narrowed down repro on github ? That would ease the investigation.
chriswoodie commentedon Aug 24, 2023
@JeanMeche Here you go, removed as much "fluff" as I could:
https://github.com/skog-newglue/describe-repro
Run
nx serve publicand open console.JeanMeche commentedon Aug 24, 2023
@skog-newglue In your case, I'm not sure
libs/stylekit/src/index.tsshould export the spec tests and the storybook stories. Removing them fixes your issue.chriswoodie commentedon Aug 24, 2023
@JeanMeche Ahaaa, completely missed that those were included. The script which creates the index file should exclude them indeed. Thank you 🥇
angular-automatic-lock-bot commentedon Sep 24, 2023
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.
fix(core): guard the jasmine hooks (angular#51394)
fix(core): guard the jasmine hooks (angular#51394)