javascript - ReferenceError: window is not defined, consider using the "jsdom" test environment (jest V28) - S

I would like some assistance in resolving this error.Am using Jest V28.0.0.Here's how my package.

I would like some assistance in resolving this error.

Am using Jest V28.0.0.

Here's how my package.json's test script & devDependencies look like

    "scripts":{
       ...,
       "test": "jest --env=node --watchAll --coverage --verbose",
    },

   "devDependencies": {
        ...
        "@babel/preset-env": "^7.16.11",
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.0",
        "@types/jest": "^27.4.1",
        "babel-jest": "^28.0.0",
        "jest": "^28.0.0",
        "jest-environment-jsdom": "^28.0.1",
        "jsdom": "^19.0.0",
    }
}

I also have a jest.config.js file that looks like so

module.exports = {
    roots: ['<rootDir>/tests/'],
    testEnvironment: 'jsdom',
    testMatch: ['**/?(*.)+(test).js'],
    transform: {
        '^.+\\.js?$': 'babel-jest',
    },
    moduleNameMapper: {
        ...
    },
}

My actual test file looks like so


import React from 'react'
import { render, cleanup, screen } from '@testing-library/react'
import renderer from 'react-test-renderer'
import '@testing-library/jest-dom'

/**
 * @jest-environment jsdom
 */

//ponents
import MyComponent from '../../../../src/website/Components/MyComponent'

test('should have the Add recipients text label', () => {
    const addRecipientsLabel = screen.getByTestId('label-element')
    expect(true).toBe(true)
    render(<MyComponent />)
    expect(addRecipientsLabel).toHaveTextContent('Add Recipients')
})

I would like some assistance in resolving this error.

Am using Jest V28.0.0.

Here's how my package.json's test script & devDependencies look like

    "scripts":{
       ...,
       "test": "jest --env=node --watchAll --coverage --verbose",
    },

   "devDependencies": {
        ...
        "@babel/preset-env": "^7.16.11",
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.0",
        "@types/jest": "^27.4.1",
        "babel-jest": "^28.0.0",
        "jest": "^28.0.0",
        "jest-environment-jsdom": "^28.0.1",
        "jsdom": "^19.0.0",
    }
}

I also have a jest.config.js file that looks like so

module.exports = {
    roots: ['<rootDir>/tests/'],
    testEnvironment: 'jsdom',
    testMatch: ['**/?(*.)+(test).js'],
    transform: {
        '^.+\\.js?$': 'babel-jest',
    },
    moduleNameMapper: {
        ...
    },
}

My actual test file looks like so


import React from 'react'
import { render, cleanup, screen } from '@testing-library/react'
import renderer from 'react-test-renderer'
import '@testing-library/jest-dom'

/**
 * @jest-environment jsdom
 */

//ponents
import MyComponent from '../../../../src/website/Components/MyComponent'

test('should have the Add recipients text label', () => {
    const addRecipientsLabel = screen.getByTestId('label-element')
    expect(true).toBe(true)
    render(<MyComponent />)
    expect(addRecipientsLabel).toHaveTextContent('Add Recipients')
})

Share Improve this question asked Apr 27, 2022 at 7:57 muriukialexmuriukialex 932 silver badges7 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You need to include @jest-environment at the top of the file. Above even the imports.

https://jestjs.io/docs/configuration#testenvironment-string

Eg.:

/**
 * @jest-environment jsdom
 */

import React from 'react'
      
//ponents
import MyComponent from '...'

test('should have the Add recipients text label', () => {
    // test
})

(OP already did this, but documenting for future visitors.)

As of Jest 28.x, if you are using JSDOM test environment, the jest-environment-jsdom package now must be installed separately:

npm install --save-dev jest-environment-jsdom

OR

yarn add --dev jest-environment-jsdom

Migration guide:

https://jest-archive-august-2023lify.app/docs/28.x/upgrading-to-jest28#jsdom

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744283884a4566705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信