我想测试渲染内的内容,但 getLocationParse()需要重新 location.search 。
export default class EstateDetail extends Component {
constructor(){
super();
const parsed = this.getLocationParse();
...
}
getLocationParse(){
const queryString = require('query-string');
const parsed = queryString.parse(location.search);
return parsed;
}
render() {...}
}
所以我试图模仿 getLocationParse 但是失败了。
知道怎么解决吗?
const getLocationParse = jest.fn().mockReturnValueOnce({id:21});
let app = shallow(
<EstateDetail/>
);
答案 0 :(得分:0)
你可以这样做:
import { foo } from './example';
jest.mock('./example', () => (
...require.requireActual('./example'),
foo: jest.fn()
));
test('foo should be a mock function', () => {
expect(foo('mocked!')).toHaveBeenCalledWith('mocked!');
});
有关详细信息,请参阅GH问题:https://github.com/facebook/jest/issues/936