我的历史课程如下:
import createHistory from 'history/createBrowserHistory';
export default createHistory({
basename: '/admin/',
});
当针对连接到商店并使用路由器呈现的类编写/运行任何单元测试时,我在测试中收到以下警告:
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".
示例测试如下:
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();
});
});
如何使用MemoryRouter模拟这个历史道具? 我已经尝试过传递历史对象,但是我被告知这个道具被内存路由器忽略了。
答案 0 :(得分:3)
您始终可以从Jest配置中模拟出该网址。
我的方法通常是将其包含在我的package.json
对于您而言,我希望它类似于-
"jest": {
"testURL": "http://some-domain.tld/admin"
}
然后,您可以通过在beforeEach()
块中添加以下内容来在每次测试的基础上进行更改
window.history.pushState({}, 'Foo Title', '/admin/foo');
答案 1 :(得分:0)
对于使用create-react-app的用户:更新package.json并在测试命令行的末尾添加url,如下所示:
{{settings.py:174}} INFO - settings.configure_orm(): Using pool settings. pool_size=5, pool_recycle=1800, pid=6740
{{__init__.py:51}} INFO - Using executor SequentialExecutor
{{models.py:273}} INFO - Filling up the DagBag from
ncalls tottime percall cumtime percall filename:lineno(function)
997 443.441 0.445 443.441 0.445 {built-in method io.open}
198 186.978 0.944 483.629 2.443 zipfile.py:198(is_zipfile)
642 65.069 0.101 65.069 0.101 {method 'close' of '_io.BufferedReader' objects}
1351 45.924 0.034 45.946 0.034 <frozen importlib._bootstrap_external>:830(get_data)
7916 39.403 0.005 39.403 0.005 {built-in method posix.stat}
2/1 22.927 11.464 544.419 544.419 dag_processing.py:220(list_py_file_paths)
33 18.992 0.576 289.797 8.782 models.py:321(process_file)
22 8.723 0.397 8.723 0.397 {built-in method posix.scandir}
412 2.379 0.006 2.379 0.006 {built-in method posix.listdir}
9 1.301 0.145 3.058 0.340 linecache.py:82(updatecache)
1682/355 0.186 0.000 0.731 0.002 sre_parse.py:470(_parse)
1255 0.183 0.000 0.183 0.000 {built-in method marshal.loads}
3092/325 0.143 0.000 0.647 0.002 sre_compile.py:64(_compile)
59 0.139 0.002 0.139 0.002 {built-in method builtins.compile}
25270 0.134 0.000 0.210 0.000 sre_parse.py:253(get)
52266 0.132 0.000 0.132 0.000 {method 'append' of 'list' objects}
4210/4145 0.131 0.000 1.760 0.000 {built-in method builtins.__build_class__}
答案 2 :(得分:0)
您需要为testURL
设置适当的jest
。这是在jest.config.js
中完成的,例如:
module.exports = {
...
testURL: 'http://localhost/admin',
}