spectator icon indicating copy to clipboard operation
spectator copied to clipboard

Do not import `@jest/globals` outside of the Jest test environment

Open dapperdandev opened this issue 1 year ago • 3 comments

Is this a regression?

No

Description

Error when importing from "@ngneat/spectator/jest".

TLDR; I suspect this is due to using @angular-devkit/build-angular:jest rather than jest-preset-angular

Please provide a link to a minimal reproduction of the bug

N/A

Please provide the exception or error you saw

`Do not import `@jest/globals` outside of the Jest test environment`

Please provide the environment you discovered this bug in

Angular CLI: 17.0.8
Node: 20.11.0
Package Manager: npm 10.2.4
OS: win32 x64

Angular: 17.0.8
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router, upgrade

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1701.2
@angular-devkit/build-angular   17.1.2
@angular-devkit/core            17.1.2
@angular-devkit/schematics      17.0.8 (cli-only)
@ngtools/webpack                17.1.2
@schematics/angular             17.0.8 (cli-only)
rxjs                            7.8.1
typescript                      5.2.2
webpack                         5.89.0
zone.js                         0.14.2

Anything else?

Simple spec file:

import { Spectator, createComponentFactory } from "@ngneat/spectator/jest";
import { ContactsComponent } from "./contacts.component";
import { HttpClientTestingModule } from "@angular/common/http/testing";
import { ContactsService } from "@kno2/data-access/contacts";
import { AppModule } from "AppNgx/app/app.module";
import { SocketService } from "@kno2/shared";

describe("ContactsComponent", () => {
    let spectator: Spectator<ContactsComponent>;
    const createComponent = createComponentFactory({
        component: ContactsComponent,
        imports: [HttpClientTestingModule],
        overrideModules: [[AppModule, { set: { providers: [] } }]],
        mocks: [ContactsService, SocketService],
        shallow: true
    });

    beforeEach(() => (spectator = createComponent()));

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

Do you want to create a pull request?

No

dapperdandev avatar Feb 08 '24 23:02 dapperdandev

Hello, is there any workaround for this issue? I also get error Do not import @jest/globals outside of the Jest test environment, when using:

  • test builder @angular-devkit/build-angular:jest
  • package @ngneat/spectator/jest in my spec file
  • Angular v18.0.0

teokosmo avatar Jun 14 '24 08:06 teokosmo

@teokosmo

I've yet to try again. I ended up using Angular's basic testing modules + ng-mocks

dapperdandev avatar Jun 17 '24 14:06 dapperdandev