Problem running jest: Error: Do not import `@jest/globals` outside of the Jest test environment
Asked Answered
A

2

9

I am trying to use Jest.js to test some typescript code I wrote. I am getting an error "Error: Do not import @jest/globals outside of the Jest test environment" but I thought I was in a jest testing environment. Here is my code:

import {NewMarketContractRequest, NewContractReponse, MarketSalesDataReply, MessageResonse} from '../models';
import {describe, expect, test} from '@jest/globals';

import {deployNewMarketContract} from '../market-contract-lib';

describe('deployNewMarketContract', () => {
    it('should deploy a new market contract', async () => {
        var request: NewMarketContractRequest = {
            contractId: 'market3.testnet',
            ownerAccount: 'market3.testnet',
            ftTokenIds: [],
            chainId: 'testnet',
            toJSON: () => {return "";},
            toObject: () => { return new Object();}
        };
        const response = await deployNewMarketContract(request);
        expect(response.id).toBeDefined();
        expect(response.createdTime).toBeDefined();
        expect(response.chainId).toBeDefined();
        expect(response.transactionHash).toBeDefined();
    });
});

And when I run "npm test" this is what I get:

Error: Do not import `@jest/globals` outside of the Jest test environment
    at Object.<anonymous> (C:\s\keypom-api\node_modules\@jest\globals\build\index.js:12:7)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (C:\s\keypom-api\src\__tests__\market-contract-tests.ts:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Function.Module._load (node:internal/modules/cjs/loader:930:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

This test file is the only one that references jest at all so the error message doesn't even seem correct to me.

Achaemenid answered 21/2, 2023 at 21:56 Comment(0)
R
2

You don't have to import jest globals, they will be available in your testing environment, as the documentation states:

In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.

You just need to create test files, by default they are

  • .js, .jsx, .ts and .tsx files inside of __tests__ folders,
  • Any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js)
  • Files called test.js or spec.js

So just delete import {describe, expect, test} from '@jest/globals' from your test files as the error message says.

If you had some warnings in your editor with TypeScript it's just because of Jest's typescript definitions, you can just install @types/jest with

npm i -D @types/jest
Rema answered 21/2, 2023 at 23:42 Comment(4)
expect.anything() doesn't work unless I import jest globals and then tests that run say 'don't import jest globals' - installing the types doesn't fix the problem.Convolution
"You don't have to import jest globals" ... only by default, see injectGlobals.Animated
I'm getting reference errors for describe, so I don't think it's auto injecting them.Undertrick
This is not correct. The docs you link specifically say "The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs". @types/jest is a separate, third party set of type definitions that make them global, but they're not as typesafe or correct as @jest/globals, which are maintained as part of jest. The definitelytyped ones predate jest including its own types.Iridissa
B
0

Maybe you need to check, which version of jest you are using.

I just had a similar problem, where it complained about:

TypeError: expect(...).toBeEmpty is not a function

Turns out I tried to use test(), when jest version 26 only supported it().

Not sure how backwards compatible jest is, but your example is using it(). Maybe try changing that to test():

describe('deployNewMarketContract', () => {
test('should deploy a new market contract', async () => {
    var request: NewMarketContractRequest = {
Basic answered 23/4 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.