Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用Jest测试使用了taro-ui的组件时,报错SyntaxError。 #4076

Closed
yzyanchao opened this issue Aug 5, 2019 · 4 comments
Closed

用Jest测试使用了taro-ui的组件时,报错SyntaxError。 #4076

yzyanchao opened this issue Aug 5, 2019 · 4 comments
Assignees

Comments

@yzyanchao
Copy link

yzyanchao commented Aug 5, 2019

问题描述
用Jest测试一个自定义组件,该组件中使用了taro-ui组件。运行测试后报错:
Test suite failed to run

\node_modules\_taro-ui@2.2.1@taro-ui\dist\weapp\index.js:1
 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Taro from '@tarojs/taro'
                                                                                                 ^^^^

 SyntaxError: Unexpected identifier

   at ScriptTransformer._transformAndBuildScript (node_modules/_jest-runtime@23.6.0@jest-runtime/build/script_transformer.js:403:17)
   at Object.<anonymous> (node_modules/_taro-ui@2.2.1@taro-ui/dist/index.js:5:22)

可以发现,这里应用了微信版本的taro-ui文件: \node_modules_taro-ui@2.2.1@taro-ui\dist\weapp\index.js:1
在taro-ui\dist\index.js中,有如下代码:

if (process.env.TARO_ENV === 'h5') {
    module.exports = require('./h5/index')
    module.exports.default = module.exports
  } else {
    module.exports = require('./weapp/index')
    module.exports.default = module.exports
  }

Jest测试时,TARO_ENV未定义,所以使用了微信版本的taro-ui文件。但我找不到地方去设置TARO_ENV。
请帮忙解决这个问题,谢谢

复现步骤
[复现问题的步骤]

  1. 使用Jest测试一个引用了taro-ui组件的自定义组件
  2. 写一个测试用例
  3. 报问题描述阶段错误
// 这里可以贴代码
// 点击链接查看如何插入代码:
// https://coding.net/help/doc/project/markdown.html#i-5

// ```js
// 代码放这里(前后的 "```" 有助 Mardown 整理代码格式和加入代码高亮)
// ```

// 提供完整可复现的代码和整理好代码格式,有助于我们快速定位问题,节省你我时间。
// 代码提供不全或代码格式混乱的 issues 【有可能被忽略】,烦请各位注意。

期望行为
测试用例能运行

报错信息

\node_modules\_taro-ui@2.2.1@taro-ui\dist\weapp\index.js:1
   ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Taro from '@tarojs/taro'
                                                                                                   ^^^^

   SyntaxError: Unexpected identifier

     at ScriptTransformer._transformAndBuildScript (node_modules/_jest-runtime@23.6.0@jest-runtime/build/script_transformer.js:403:17)
     at Object.<anonymous> (node_modules/_taro-ui@2.2.1@taro-ui/dist/index.js:5:22)

系统信息

Taro v1.2 及以上版本已添加 taro info 命令,方便大家查看系统及依赖信息,运行该命令后将结果贴下面即可。

👽 Taro v1.3.10

Taro CLI 1.3.10 environment info:
System:
OS: Windows 10
Binaries:
Node: 10.16.0 - C:\Program Files\nodejs\node.EXE
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD

补充信息
[可选]
[根据你的调查研究,出现这个问题的原因可能在哪里?]
Jest测试时,TARO_ENV未定义,所以使用了微信版本的taro-ui文件。但我找不到地方去设置TARO_ENV。

@taro-bot
Copy link

taro-bot bot commented Aug 5, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@yzyanchao yzyanchao changed the title 用Jest测试使用了taro-ui的组件时,报错SyntaxError。原因是process.env.TARO_ENV为undefined 用Jest测试使用了taro-ui的组件时,报错SyntaxError。 Aug 5, 2019
@yzyanchao
Copy link
Author

我的思路是错的,TARO_ENV设成h5报一样的错,只是报错文件换成h5的文件了。

@taro-bot
Copy link

taro-bot bot commented Aug 6, 2019

CC @luckyadam

@yzyanchao
Copy link
Author

问题已解决。
在jest.config.js中,添加:

'transformIgnorePatterns': ['<rootDir>/node_modules/(?!(taro-ui|_taro-ui.*)/)'],
'setupFiles': ['<rootDir>/jestSetupFile.js'],

在根目录中,新建文件jestSetupFile.js,内容为:
process.env.TARO_ENV = 'h5'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants