我有以下错误: 无法读取未定义的属性“配置”。 我怎样才能让我的单元测试工作。
到目前为止,这是我的单元测试:
import React from 'react';
import { Enzyme, mount } from 'enzyme'
import layout from './layout';
import Adapter from 'enzyme-adapter-react-15';
//Unit Test V
Enzyme.configure({ adapter: new Adapter() });
describe ('layout />', () => {
it( 'should be defined ', () => {
expect(layout).toBeDefined()
})
it( 'should render ', () => {
const wrapper = mount(<layout />)
expect(wrapper).toMatchSnapshot()
})
it( 'should render class name', () => {
const wrapper = mount
(<div className = "empty-layout"/>)
expect(wrapper).toMatchSnapshot()
})
});
我希望测试能够通过。我可以在单元测试中实现的任何想法/提示或改进。
最佳答案
configure
步骤总是在名为 setupTests.js
的文件中全局完成,该文件通常位于项目的根目录中:
setupTests.js
:
import {configure} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});
请看这里:https://facebook.github.io/create-react-app/docs/running-tests#src-setuptestsjs
因此,使用上面提供的内容在项目的根文件夹中创建一个文件 setupTests.js
并删除行 Enzyme.configure({ adapter: new Adapter() });
来自你的测试,看看它是否有效。
关于javascript - 类型错误 : Cannot read property 'configure' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53963491/