测试上有一些小问题
来源:5-3 React 测试工具简介
慕圣3754052
2019-02-12
老师您好,按视频说的,我在src/setupTests.js里面添加了config代码
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
但是测试会报错,说我需要配置(我明明配置了)
FAIL src/components/__test__/TotalPrice.test.js (8.764s)
● test TotalPrice component › component should render correct income&outcome number
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none.
To configure an adapter, you should call `Enzyme.configure({ adapter: new Adapter() })`
before using any of Enzyme's top level APIs, where `Adapter` is the adapter
corresponding to the library currently being tested. For example:
import Adapter from 'enzyme-adapter-react-15';
官方文档说的也是模模糊糊,没说具体怎么配置
只有直接在测试组件内写配置才能运行src/components/__test__/TotalPrice.js
import React from 'react'
import { shallow, configure } from 'enzyme'
import TotalPrice from '../TotalPrice'
import Adapter from 'enzyme-adapter-react-16'
configure({ adapter: new Adapter() })
const props = {
income: 1000,
outcome: 2000
}
describe('test TotalPrice component', () => {
it('component should render correct income&outcome number', () => {
const wrapper = shallow(<TotalPrice {...props} />)
expect(wrapper.find('.income').text() * 1).toEqual(1000)
expect(wrapper.find('.outcome').text() * 1).toEqual(2000)
})
})
// 这样是可以正常运行的
但是明显这样写不是最佳选择,所以请问老师我哪里写错了?
写回答
1回答
-
张轩
2019-02-13
需要先确认几点欧:1 你是用的 create-react-app 脚手架工具对吧? 2 是在 src 文件夹下 并且 命名正确对吗 3 是运行的 npm test 命令对吗?
022019-02-13
相似问题