Har*_*lue 7 unit-testing reactjs jestjs enzyme
我有一个历史课,看起来像:
import createHistory from 'history/createBrowserHistory';
export default createHistory({
basename: '/admin/',
});
Run Code Online (Sandbox Code Playgroud)
当针对连接到商店并使用路由器呈现的类编写/运行任何单元测试时,我在测试中收到以下警告:
console.error node_modules/warning/warning.js:51
Warning: You are attempting to use a basename on a page whose URL path does not begin with the basename. Expected path "blank" to begin with "/admin".
Run Code Online (Sandbox Code Playgroud)
示例测试如下:
import React from 'react';
import { MemoryRouter as Router } from 'react-router-dom';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import TenantListContainer from '../../../src/containers/TenantList';
import TenantList from '../../../src/containers/TenantList/TenantList';
const mockStore = configureStore();
const store = mockStore({
tenants: {
tenants: ['foo'],
loading: true,
},
});
describe('TenantListContainer', () => {
it('should render the TenantList components', () => {
const wrapper = mount(
<Router>
<TenantListContainer store={store} />
</Router>
);
expect(wrapper.find(<TenantList />)).toBeTruthy();
});
});
Run Code Online (Sandbox Code Playgroud)
如何使用MemoryRouter模拟这个历史道具?我试过传递历史对象,但是我被告知这个道具被内存路由器忽略了.
小智 7
您始终可以从 Jest 配置中模拟出 url。
我的方法通常是将其包含在我的 package.json
在您的情况下,我希望这类似于-
"jest": {
"testURL": "http://some-domain.tld/admin"
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过在您的beforeEach()块中包含以下内容,在每次测试的基础上更改此设置
window.history.pushState({}, 'Foo Title', '/admin/foo');
Run Code Online (Sandbox Code Playgroud)
对于使用 create-react-app 的用户:更新 package.json 并在测试命令行的末尾添加 url,如下所示:
react-scripts-ts test --env=jsdom --watch --testURL http://localhost/foo
Run Code Online (Sandbox Code Playgroud)
来源:https : //github.com/facebook/create-react-app/issues/3425
| 归档时间: |
|
| 查看次数: |
1093 次 |
| 最近记录: |