question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ng test command crashed due to Karma webpack couldn't resolve the CoreModule.

See original GitHub issue

Vesrions used:

@angular/core”: “^12.2.15”, “@angular-devkit/build-angular”: “~12.2.15”,

“karma”: “~6.3.4”, “karma-chrome-launcher”: “~3.1.0”, “karma-cli”: “~2.0.0”, “karma-coverage-istanbul-reporter”: “~3.0.2”, “karma-jasmine”: “~4.0.0”, “karma-jasmine-html-reporter”: “^1.6.0”, “karma-junit-reporter”: “^1.2.0”,

The following error message appears only after running ng test :

 An error was thrown in afterAll
  Uncaught TypeError: Cannot read properties of undefined (reading 'CoreModule')
  TypeError: Cannot read properties of undefined (reading 'CoreModule')
      at Module.CoreModule (http://localhost:9876/_karma_webpack_/main.js:4105:108)
      at Module.3467 (http://localhost:9876/_karma_webpack_/webpack:/src/app/home/home.module.ts:17:5)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.36747 (http://localhost:9876/_karma_webpack_/main.js:271:75)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.82182 (http://localhost:9876/_karma_webpack_/main.js:15425:73)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.44466 (http://localhost:9876/_karma_webpack_/main.js:17638:83)
      at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
      at Module.40294 (http://localhost:9876/_karma_webpack_/main.js:3226:83)

I have the following files structure :

 ->src/app/core/
              -> index.ts
              -> core.module.ts
              -> ..
->src/app/folder/
              -> module.ts

I’m trying to import CoreModule from src/app/core/index which includes export * from './core.module'; inside src/app/folder/module.ts using import { CoreModule } from '@app/core';

However it seems src/app/core/index.ts file fails to export CoreModule.

src/app/core/core.module.ts :

@NgModule({
  imports: [CommonModule, HttpClientModule, TranslateModule, RouterModule, SharedModule, TourMatMenuModule],
  providers: [
    {
      provide: HttpClient,
      useClass: HttpService,
    },
    {
      provide: RouteReuseStrategy,
      useClass: RouteReusableStrategy,
    },
    ApiService,
  ],
  declarations: [ComponentA, ComponentB],
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
    // Import guard
    if (parentModule) {
      throw new Error(`${parentModule} has already been loaded. Import Core module in the AppModule only.`);
    }
  }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
devoto13commented, Mar 3, 2022

This exactly means that you have a circular dependency, CoreModule attempts to access X to initialize, but X, in turn, attempts to access CoreModule, which can not proceed and throws.

0reactions
Waseem-Isaaccommented, Mar 6, 2022

I’m declaring a pipe which is importing some class from app.module.ts file. This pipe is declared inside a module which in turn is imported into CoreModule, And this was the problem.

But I still can’t realise what makes the test code detect it and crash .but not the production code 😃)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ng test command crashes due to TypeError: Cannot read ...
I'm trying to import CoreModule from src/app/core/index which includes export * from './core.module'; inside src/app/folder/module.ts using ...
Read more >
Upgrading from AngularJS to Angular
ngUpgrade bridges the related concepts of AngularJS transclusion and Angular content projection together. DOM element ownership in a hybrid application.
Read more >
Setting up Angular 2 Testing Environment with Karma and ...
This article will explain how to create an environment for Angular 2 testing. It uses Karma, webpack and some useful stuffs.
Read more >
Angular-Karma-Test-Explorer/Bugs - Gitter
I just installed the extension in an Angular 10 project (generated with the CLI). But it just rotates the "Reloading tests" icon and...
Read more >
Ionic 2 Unit Testing Setup: The Best Way - roblouie
Install dependencies for Jasmine and Karma, including some loaders for our Webpack config and Jasmine and Node types to keep Typescript happy. npm...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found