Jest errors: Need to call TestBed.initTestEnvironment() first AND zone-testing.js is needed...

Discussão em 'Angular' iniciado por Aria, Novembro 22, 2024.

  1. Aria

    Aria Guest

    This is an example of a test:

    import { ComponentFixture, TestBed } from '@angular/core/testing';

    import { FooterComponent } from './footer.component';

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

    beforeEach(async () => {
    await TestBed.configureTestingModule({
    declarations: [ FooterComponent ]
    })
    .compileComponents();
    });

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

    it('should create', () => {
    expect(component).toBeTruthy();
    });
    });


    when I run my test, I get these errors:

    Need to call TestBed.initTestEnvironment() first


    and

    zone-testing.js is needed for the fakeAsync() test helper but could not be found.
    Please make sure that your environment includes zone.js/testing


    I tried this and tried to inject it as a main in my angular.json. and also tried using the test.ts which contains the zone imports, but it didn't fix the issue. I have tried my preset with ts-jest and jest-preset-angular. I also tried this as my transform:

    '^.+\\.ts?$': ['jest-preset-angular', {
    babel: true,
    tsconfig: '<rootDir>/src/tsconfig.spec.json',
    stringifyContentPathRegex: '\\.(html|svg)$',
    }]


    and for my transformignorepatterns :

    transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],


    nothing I try seems to fix the issue. My tests were using karma before so I'm trying to switch to Jest. What is the simplest way to fix this issue? I seem to be missing something

    Continue reading...

Compartilhe esta Página