gpt4 book ai didi

reactjs - "Type Error : is not a function"在 Jest 测试中

转载 作者:行者123 更新时间:2023-12-04 13:57:56 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com