简体   繁体   English

运行测试时,Jest无法处理TypeScript TSX文件

[英]Jest cannot handle TypeScript TSX files when running tests

Running tests using Jest with TypeScript TSX occurs in errors. 使用Jest和TypeScript TSX运行测试会发生错误。

I have the following base set up: 我有以下基本设置:

https://github.com/joe-glasgow/tsx-jest-unit https://github.com/joe-glasgow/tsx-jest-unit

However when running 但是在跑步时

yarn run test

the following error occurs: 发生以下错误:

const checkbox = ReactTestUtils.renderIntoDocument(labelOn, "On", labelOff = "Off" /  >
                                                                                      ^
SyntaxError: Unexpected token >

I was under the impression, that by using the preprocessor that TSX would be handled correctly? 我的印象是,使用预处理器可以正确处理TSX?

Thanks in advance. 提前致谢。

After cloning the repo to have a look, it turns out the code is fine, but there's a weird formatting issue. 克隆回购库后,结果发现代码很好,但是存在一个奇怪的格式化问题。 Here's a git diff showing my changes (or lack of) and the test passing. 这是一个git diff,显示了我的更改(或缺少更改)和测试通过。 I added a new line at the end of the file and that's it. 我在文件末尾添加了新行,仅此而已。

As for the cause, I'm not really sure. 至于原因,我不确定。 It could be something to do with line endings, a problem with the preprocessor, a problem with your editor etc, it's hard to tell! 这可能与行尾,预处理器问题,编辑器问题等有关,这很难说! Anyway, I hope this helps. 无论如何,我希望这会有所帮助。

[ [ 1]

After some digging around and trying out various configurations, "transformIgnorePatterns" to the jest configuration package.json solved the issue: 经过一番摸索并尝试了各种配置之后,将“ transformIgnorePatterns”添加到最简单的配置package.json中解决了该问题:

"jest" : {
        "transformIgnorePatterns": [
            "node_modules/"
        ],
        ...
}

Go figure! 去搞清楚!

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

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