곽대용*_*곽대용 5 typescript reactjs jestjs enzyme
经过 Jest 和 Enzyme 测试的组件具有一定的功能。然后这个函数使用一个导入的库。
它会犯一些错误。
it('Emoji should be rendered without error', () => {
const messageItem = shallow(
<MessageItem {...props}/>
)
...
})
Run Code Online (Sandbox Code Playgroud)
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()} />))
}
}
Run Code Online (Sandbox Code Playgroud)
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 | }
Run Code Online (Sandbox Code Playgroud)
它在运行时正常工作。
有解决办法吗?
get-urls导出单个函数并get-urls 使用export =语法 的类型定义。
根据TypeScript 文档,export =正确的导入方法get-urls是使用import = require()如下语法:
import getUrls = require('get-urls');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10751 次 |
| 最近记录: |