作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所有模拟尝试都会产生错误:
import useDebounce from 'use-debounce'
jest.mock('use-debounce')
TypeError: (0 , _useDebounce.useDebounce) is not a function or its return value is not iterable
尝试仅针对该模拟也失败:
jest.mock('use-debounce',() => {
return {
useDebounce: jest.fn(value => [value])
}
})
也给出了同样的错误。使用模拟计时器也不起作用。
最佳答案
编辑:这是因为您混淆了导入。 According to npm它确实被用作命名导入
import { useDebounce } from 'use-debounce';
所以,只要你像☝️那样导入它,然后你就可以按照你说的方式 mock 它:
jest.mock('use-debounce',() => {
return {
useDebounce: jest.fn(value => [value])
}
})
首选 fake timers
请记住,我强烈鼓励使用模拟的替代方法,即使用 fake timers .
import { ComponentThatUsesUseDebounce } from './something';
jest.useFakeTimers(); // <- must appear in your test file
describe('...',() => {
it('should call a debounce function',() => {
const thing = render(<ComponentThatUsesUseDebounce/>);
fireEvent.click(thing.something);
jest.runAllTimers(); // or runOnlyPendingTimers() or advanceTimersByTime(n)
expect(something).toHaveBeenCalled();
});
});
原始答案
useDebounce
不是命名导出,它是默认
导出:
jest.mock('use-debounce',() => {
return {
__esModule: true,
default: jest.fn(value => [value])
}
})
关于javascript - 你如何模拟 useDebounce ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67511217/
所有模拟尝试都会产生错误: import useDebounce from 'use-debounce' jest.mock('use-debounce') TypeError: (0 , _useD
所有模拟尝试都会产生错误: import useDebounce from 'use-debounce' jest.mock('use-debounce') TypeError: (0 , _useD
当我尝试使用 https://usehooks-ts.com/react-hook/use-local-storage 时通过以下方式在 Next.js 中,我得到 Unhandled Runtime
我是一名优秀的程序员,十分优秀!