gpt4 book ai didi

javascript - shallowWrapper 为空,正在测试快照 Jest/Enzyme?

转载 作者:行者123 更新时间:2023-12-02 02:38:16 27 4
gpt4 key购买 nike

我不知道为什么在运行快照测试时得到空的shallowWrapper对象,我使用JestEnzyme:

我在App.test.js.snap文件中得到的内容:

// Jest Snapshot v1, "goo.gl/fbAQLP"
exports[`renders App component 1`] = `ShallowWrapper {}`;

我的package.json文件:

{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.30",
"@fortawesome/free-solid-svg-icons": "^5.14.0",
"@fortawesome/react-fontawesome": "^0.1.11",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"bootstrap": "^4.5.2",
"install": "^0.13.0",
"npm": "^6.14.8",
"react": "^16.13.1",
"react-bootstrap": "^1.3.0",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.0-next.98"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.4",
"jest": "^26.4.2"
}
}

App.test.js文件

import { shallow } from 'enzyme';
import React from 'react';
import App from './App';


it('renders App component', () => {
expect(shallow(<App />)).toMatchSnapshot();
})

setupTests.js 文件:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({
adapter: new Adapter()
});

为什么我得到那个空的shallowWrapper对象,我经过一番搜索怀疑是jest的版本,但即使降级也没有解决。

最佳答案

我遇到了类似的问题, Jest 帮助我解决了这个问题。我建议您可以清除 jest 缓存,然后使用 jest --clearCache && jest -u 更新快照,然后运行测试。

对于您的快照,我建议您使用 enzyme-to-json 序列化程序 ( https://www.npmjs.com/package/enzyme-to-json ) 并将您的测试修改为:

import { shallow } from 'enzyme';
import React from 'react';
import App from './App';
import { shallowToJson } from 'enzyme-to-json';

it('renders App component', () => {
const app = shallow(<App />);
expect(shallowToJson(app)).toMatchSnapshot();
})

关于javascript - shallowWrapper 为空,正在测试快照 Jest/Enzyme?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64003690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com