reactjs - 我正在尝试将函数从上下文导出到另一个文件以与 jest 一起使用
问题描述
我想用 jest 在我的测试文件中运行一些函数。我是新手,所以请善待。我正在从我的应用程序的上下文文件中导入此函数。
let isProduction2 = () => {
if (production) {
return true
} else {
return false
}
}
export {
ProductProvider,
ProductConsumer,
ProductContext,
isProduction2
};
import {
isProduction2
} from './context'
test('Fake Test', () => {
expect(isProduction2).toBeTruthy();
});
//Error Jest encountered an unexpected token
这通常意味着您正在尝试导入 Jest 无法解析的文件,例如它不是纯 JavaScript。
默认情况下,如果 Jest 看到 Babel 配置,它将使用它来转换您的文件,而忽略“node_modules”。
解决方案
问题是你没有isProduction2
在你的期望语句中执行你的函数(即)。您目前只是传递一个参考。
代替
test('Fake Test', () => {
expect(isProduction2).toBeTruthy();
});
它应该是
test('Fake Test', () => {
expect(isProduction2()).toBeTruthy();
//note the `()` after function name. This executes it and returns the result.
});
推荐阅读
- opencart - opencart 中的电子商务分析
- oracle - 在 SQL 中使用 UTL_FILE 时 readbuffer 和 max_linesize 之间的最小大小差异应该是多少
- neo4j - 如何在 Neo4j 中将第三个节点与第一个节点链接
- xamarin - Google Maps not showing on phone
- json - 将 ABAP 数据转换为以 JSON-XML 格式表示它们的 iXML 对象
- ios - Firebase 推送通知对某些用户停止工作
- django - Django:有没有办法在搜索后重新聚合总和?
- android - 错误:使用 fullScreenIntent 需要 USE_FULL_SCREEN_INTENT 权限
- amazon-web-services - 能否在 EC2 用户数据中安装 AWS Logs 代理?
- java - 创建图像数组以传递给 imageDisplay 活动