最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Getting "NullInjectorError: No provider for InjectionToken Application Configuration!" when trying t

programmeradmin4浏览0评论

I want to test guard functions in Angular with jest, using Angular v19 and a with the experimental Zoneless setup. When i try to start my unittests for the guard functions, I get the "NullInjectorError: No provider for InjectionToken Application Configuration!".

SO far I tried mocking my app-config from the main.ts.

This is my testfile so far:

import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { provideMockStore } from '@ngrx/store/testing';
import { TestBed } from '@angular/core/testing';
import { setLanguage } from './index';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { provideExperimentalZonelessChangeDetection } from '@angular/core';
import { TranslocoService } from '@jsverse/transloco';
import { provideHttpClient } from '@angular/common/http';

describe('setLanguage', () => {
  let activatedRouteSnapshotMock: ActivatedRouteSnapshot;
  let translocoServiceMock: jest.Mocked<TranslocoService>;

  const initialState = {
    setLanguage: jest.fn(),
    setInitialized: jest.fn(),
  };

  beforeEach(() => {
    translocoServiceMock = { setActiveLang: jest.fn(), load: jest.fn() } as any;
    TestBed.configureTestingModule({
      providers: [
        provideMockStore({ initialState }),
        {
          provide: Router,
          useValue: {
            navigate: jest.fn(),
          },
        },
        { provide: TranslocoService, useValue: translocoServiceMock },
        provideExperimentalZonelessChangeDetection(),
        provideHttpClient(),
      ],
    });

    activatedRouteSnapshotMock = {
      params: { lang: 'xy' },
    } as unknown as ActivatedRouteSnapshot;
  });
  it('should return true if the new language is already set', () => {
    activatedRouteSnapshotMock.params['lang'] = 'en';
    TestBed.runInInjectionContext(() => {
      expect(setLanguage(activatedRouteSnapshotMock)).toEqual(true);
    });
  });
});

I want to test guard functions in Angular with jest, using Angular v19 and a with the experimental Zoneless setup. When i try to start my unittests for the guard functions, I get the "NullInjectorError: No provider for InjectionToken Application Configuration!".

SO far I tried mocking my app-config from the main.ts.

This is my testfile so far:

import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { provideMockStore } from '@ngrx/store/testing';
import { TestBed } from '@angular/core/testing';
import { setLanguage } from './index';
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { provideExperimentalZonelessChangeDetection } from '@angular/core';
import { TranslocoService } from '@jsverse/transloco';
import { provideHttpClient } from '@angular/common/http';

describe('setLanguage', () => {
  let activatedRouteSnapshotMock: ActivatedRouteSnapshot;
  let translocoServiceMock: jest.Mocked<TranslocoService>;

  const initialState = {
    setLanguage: jest.fn(),
    setInitialized: jest.fn(),
  };

  beforeEach(() => {
    translocoServiceMock = { setActiveLang: jest.fn(), load: jest.fn() } as any;
    TestBed.configureTestingModule({
      providers: [
        provideMockStore({ initialState }),
        {
          provide: Router,
          useValue: {
            navigate: jest.fn(),
          },
        },
        { provide: TranslocoService, useValue: translocoServiceMock },
        provideExperimentalZonelessChangeDetection(),
        provideHttpClient(),
      ],
    });

    activatedRouteSnapshotMock = {
      params: { lang: 'xy' },
    } as unknown as ActivatedRouteSnapshot;
  });
  it('should return true if the new language is already set', () => {
    activatedRouteSnapshotMock.params['lang'] = 'en';
    TestBed.runInInjectionContext(() => {
      expect(setLanguage(activatedRouteSnapshotMock)).toEqual(true);
    });
  });
});

Share Improve this question edited Mar 11 at 12:03 GinoMartino asked Mar 11 at 11:49 GinoMartinoGinoMartino 14 bronze badges 1
  • 1 please share the minimal reproducible code – Naren Murali Commented Mar 11 at 11:52
Add a comment  | 

1 Answer 1

Reset to default 0

I fixed it by adding { provide: APP_CONFIG, useValue: {} }, to the providers for my test.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论