4

I have setting up a unit test for one of my component, the test code is :

describe('CompanyDetails', () => {
let fixture: ComponentFixture<CompanyDetailsComponent>;
let component: CompanyDetailsComponent;

beforeEach(() => {

    TestBed.configureTestingModule({
        declarations: [CompanyDetailsComponent],
        imports: [AppUiModule, getTranslocoModule(), RouterTestingModule.withRoutes([])],
        providers: [],
    });
});

beforeEach(() => {
    fixture = TestBed.createComponent(CompanyDetailsComponent);
    component = fixture.componentInstance;
});

});

for simplicity I have removed all of the providers from the code, because I have found those doesn't make any difference with this error.

When I run this test I am getting following error:

An error was thrown in afterAll
Uncaught ReferenceError: Cannot access 'CompanyDetailsComponent' before initialization
ReferenceError: Cannot access 'CompanyDetailsComponent' before initialization
  at Module.CompanyDetailsComponent (http://localhost:9876/_karma_webpack_/main.js:689:72)
  at Module.15819 (http://localhost:9876/_karma_webpack_/webpack:/src/app/broker-journey/broker-journey-routing.module.ts:39:16)
  at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
  at Module.94517 (http://localhost:9876/_karma_webpack_/main.js:3608:106)
  at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
  at Module.67221 (http://localhost:9876/_karma_webpack_/main.js:709:140)
  at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
  at Module.420 (http://localhost:9876/_karma_webpack_/main.js:655:84)
  at __webpack_require__ (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
  at webpackContext (http://localhost:9876/_karma_webpack_/webpack:/src|sync|/company-details.component/.spec/.ts$:9:1)

I have no idea why this error is coming from. If I replace this CompanyDetailsComponent with any other component from my project it works fine.

  • Have you reviewed all `declarations` for `CompanyDetailsComponent`. I suspect there is a circular reference. – Adam Cox Apr 25 '23 at 14:17

0 Answers0