Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dapperdandev opened this issue Feb 8, 2024 · 3 comments
Open

Comments

@dapperdandev
Copy link

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

@teokosmo
Copy link

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

@dapperdandev
Copy link
Author

@teokosmo

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

@Tommy228
Copy link
Contributor

I have created a bug on Angular CLI's side: angular/angular-cli#28722

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants