Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

910
Visualizações
ReferenceError: Cannot access Service' before initialization

Im getting an error when i run a unit test with jasmine on angular8 app. I have a service and inside this service im injecting another service

@Injectable({
  providedIn: 'root'
})

export class FirstService {
  constructor(private http: HttpClient, private configService: ConfigurationService) { }

when i do npm run test i always got an error:

An error was thrown in afterAll ReferenceError: Cannot access 'ConfigurationService' before initialization at Module.ConfigurationService (http://localhost:9876/_karma_webpack_/main.js:1095:112)

Any help?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You would need to provide a mock of ConfigService because it is a dependency of FirstService. The easiest way to do that is with a spy.

Something like:

let firstService: FirstServicec;
let configServiceSpy: jasmine.SpyObj<ConfigService>;

beforeEach(() => {
  const spy = jasmine.createSpyObj('ConfigService', ['getValue']);

  TestBed.configureTestingModule({
    providers: [
      FirstService,
      { provide: ConfigService, useValue: spy }
    ]
  });
  // Inject both the service-to-test and its (spy) dependency
  configService = TestBed.get(ConfigService);
  configServiceSpy = TestBed.get(ValueService);
});

Then, you can consume the spy in the test like:

it('#getValue should return stubbed value from a spy', () => {
  const stubValue = 'stub value';
  configServiceSpy.getValue.and.returnValue(stubValue);

  expect(firstService.getValue())
    .toBe(stubValue, 'service returned stub value');
  expect(configServiceSpy.getValue.calls.count())
    .toBe(1, 'spy method was called once');
  expect(configServiceSpy.getValue.calls.mostRecent().returnValue)
    .toBe(stubValue);
});

For more information, check out this section of the Angular Docs

over 4 years ago · Santiago Trujillo Relatório

0

This can be a very misleading error message.

A circular dependency may be causing it, solve your circular dependencies!

You will not see a circular dependency warning when running 'npm run test', but you will get a warning when running 'npm run serve' or 'npm run build'.

over 4 years ago · Santiago Trujillo Relatório

0

In my case, it was a very stupid error. I was using abstract services and useClass stuff and had something like this in one file:

constructor(@Optional() private readonly baseService: BaseService) {
}
    
export abstract class BaseService {
    public abstract doSomething(): void;
}

Changing the order to

export abstract class BaseService {
    public abstract doSomething(): void;
}

constructor(@Optional() private readonly baseService: BaseService) {
}

solved the issue. OMG.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda