Reference Error:未定义窗口,请考虑使用“ JSdom”。测试环境(Jest V28)
我想帮助解决此错误。
正在使用开玩笑的V28.0.0。
这是我的包装的方法。 DevDepentencies看起来像
"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",
}
}
我还有一个Jest.config.js文件,看起来像
module.exports = {
roots: ['<rootDir>/tests/'],
testEnvironment: 'jsdom',
testMatch: ['**/?(*.)+(test).js'],
transform: {
'^.+\\.js?$': 'babel-jest',
},
moduleNameMapper: {
...
},
}
我的实际测试文件看起来像
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
*/
//components
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?
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
*/
//components
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')
})
: 'babel-jest',
},
moduleNameMapper: {
...
},
}
My actual test file looks like so
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在文件顶部包括
@jest-environment
。甚至是进口。https://jestjs.io/docs/docs/configuration#testenvestenvestenvencorment #testenvestenvironment-vernvorment-string-string
eg。
You need to include
@jest-environment
at the top of the file. Above even the imports.https://jestjs.io/docs/configuration#testenvironment-string
Eg.:
( op已经做到了,但是为未来的访问者记录
。必须单独安装:
或
迁移指南:
https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28#jsdom
(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:OR
Migration guide:
https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28#jsdom