【发布时间】:2020-01-05 22:28:52
【问题描述】:
经过 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 | }
它在运行时正常工作。
有解决办法吗?
【问题讨论】:
-
你能分享你的
tsconfig.json吗?尝试在tsconfig.json中将"esModuleInterop": true添加到您的compilerOptions? -
我试过了。但它是相同的状态。 {“compilerOptions”:{“target”:“es5”,“baseUrl”:“./”,“rootDir”:“./”,“jsx”:“react”,“module”:“es2015”,“moduleResolution “:“节点”,“noUnusedLocals”:真,“漂亮”:真,“sourceMap”:真,“resolveJsonModule”:真,“esModuleInterop”:真,“lib”:[“es2015”,“dom”]} , "排除": ["node_modules", "*/node_modules/"] }
标签: reactjs typescript jestjs enzyme