Jest 和 Enzyme 测试的一个组件有一些功能。然后这个函数使用一个导入的库。
它会犯一些错误。
测试代码
it('Emoji should be rendered without error', () => {
const messageItem = shallow(
<MessageItem {...props}/>
)
...
})
消息项import * as getUrls from 'get-urls';
export class MessageItem extends Component <Props> {
state = {
isOpenThread: false,
isAddEmoji: false,
containedUrl: ''
}
getContainUrl = () => {
//getUrls makes an error!
const urls = getUrls(this.props.content).values();
const firstUrl: string = urls.next().value;
return firstUrl
}
...
render(){
return (... <UrlInfoArea {...props} url={this.getContainUrl()} />))
}
}
错误信息 TypeError: getUrls is not a function
37 |
38 | getContainUrl = () => {
> 39 | const urls = getUrls(this.props.content).values();
| ^
40 | const firstUrl: string = urls.next().value;
41 | return firstUrl
42 | }
它在运行时正常工作。有解决办法吗?
最佳答案
get-urls
导出单个函数和 get-urls
的类型定义uses the export =
syntax .
基于 TypeScript documentation on export =
正确的导入方式get-urls
将使用 import = require()
像这样的语法:
import getUrls = require('get-urls');
关于reactjs - "Type Error : is not a function"在 Jest 测试中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57764495/