#Adding a CanActivateFn guard to my router causes test env compilation failure

2 messages · Page 1 of 1 (latest)

earnest marsh
#

I created a guard function, and created a route for it in my app-routing.module.ts:

const routes: Routes = [
{
  canActivate: [MyGuard],
  component: MyComponent
  path: 'somewhere',
},
...

When I run the app, everything (including the guard behavior) works, but now when I tun my tests, I get:

Chrome Headless 130.0.0.0 (Mac OS 10.15.7) ERROR
  An error was thrown in afterAll
  Uncaught ReferenceError: Cannot access 'AppRoutingModule' before initialization
  ReferenceError: Cannot access 'AppRoutingModule' before initialization
      at Module.AppRoutingModule (http://localhost:9876/_karma_webpack_/main.js:139:65)
      at Module.36747 (http://localhost:9876/_karma_webpack_/webpack:/src/app/app.module.ts:26:5)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.32494 (http://localhost:9876/_karma_webpack_/main.js:1753:76)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.90158 (http://localhost:9876/_karma_webpack_/main.js:155:82)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.60492 (http://localhost:9876/_karma_webpack_/main.js:16:77)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at __webpack_exec__ (http://localhost:9876/_karma_webpack_/main.js:149555:48)
Chrome Headless 130.0.0.0 (Mac OS 10.15.7): Executed 0 of 0 ERROR (0.014 secs / 0 secs)

The stack trace is showing this is coming from app.module.ts where AppRoutingModule is being added to the imports array.

If I comment out canActivate: [MyGuard], in the route, then compilation succeeds and tests run........

Why does having canActivate set to a CanActivateFn cause my tests to not compile?

unkempt stratus
#

This looks like a circular import (in terms of ES module imports, not Angular module imports).