简体   繁体   English

开玩笑:“ SyntaxError:意外令牌{”

[英]Jest: “SyntaxError: Unexpected token {”

This is a very simple alteration of the jest tutorial example for react DOM unit testing. 这是对用于响应DOM单元测试的讲解示例示例的非常简单的更改。 I have a counter component and I want to test that it works. 我有一个计数器组件,我想测试它是否有效。 You can see my commit at the bottom. 您可以在底部看到我的提交。

Error: SyntaxError: Unexpected token { 错误: SyntaxError: Unexpected token {错误: SyntaxError: Unexpected token {

There's only 2 { in the .test.js file and when I remove the jsx ={1} replacing it with ="1" , it doesn't remove the error. .test.js文件中只有2 {,当我删除jsx ={1}替换为=“ 1”时,它并没有删除错误。 The other { is in the function call () => { 另一个{在函数调用() => {

Update 1 (Response to Answer) 更新1(答复)

I have changed the .test.js file to test.tsx , and made a change to the counter component to allow strings removing the jsx {} and the only other { besides the function constructor. 我已将.test.js文件更改为test.tsx,并对计数器组件进行了更改,以允许字符串删除jsx {}和除函数构造函数之外的唯一其他{

https://github.com/Falieson/react15-meteor1.5-typescript2.4/commit/36a327e79700b5532258f7fb8a171122ed260329 https://github.com/Falieson/react15-meteor1.5-typescript2.4/commit/36a327e79700b5532258f7fb8a171122ed260329

Update 2 更新2

I've discovered through trying to expose this issue that this error is only happening on my work machine, and not my personal developer machine. 通过尝试揭露此问题,我发现此错误仅发生在我的工作计算机上,而不是我的个人开发人员计算机上。 Here's the code that works on my machine but not on my work machine. 这是可以在我的机器上工作的代码,但不适用于我的工作机器。 https://github.com/Falieson/react-typescript-meteor https://github.com/Falieson/react-typescript-meteor

Update 3 I don't get any problems from jest when I run npm run test:jest but when I run meteor npm run test:jest I get this issue, going to go ask on the meteor boards . 更新3当我运行npm run test:jest时,我从jest没有任何问题,但是当我运行meteor npm run test:jest我得到了这个问题,将在流星板上询问。

Your tests are js files that are importing typescript ts files. 您的测试是导入typescript ts文件的js文件。 According to your config you have 根据您的配置

'^.+\\\\.jsx?$': 'babel-jest'

Which I guess will use the babel-jest parser instead of the typescript compiler. 我想它将使用babel-jest解析器而不是打字稿编译器。 You will have to write your tests as ts files and let typescript do its magic. 您将必须将测试编写为ts文件,并让typescript发挥作用。

对于遇到此问题的其他任何人,我建议将Meteor更新到最新的1.6-beta *,以便获得Node 8,而不是依赖Node4。正如OP中链接的meteor论坛线程中所讨论的那样,Node 4是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM