运行 包含导入 CoffeeScript 文件的 Jest 测试时的语法错误
SyntaxError when running a Jest test containing import of a CoffeeScript file
我正在编写 Jest 集成测试来测试我的服务器。这是我的测试:
import { app, port } from '../server' // the other lines are not so important
...
当 运行 jest
这引发错误:
__tests__/graphql.test.js
● Test suite failed to run
<project folder>/node_modules/papercut/lib/papercut.coffee:3
{FileStore, S3Store, TestStore } = require('./store')
^
SyntaxError: Unexpected token =
我在 Jest 测试中需要 server.js
,此文件需要 upload.js
,并且此文件需要名为 papercut
的节点模块。问题是,它是用 CoffeeScript 写的,不是纯 JS。
一开始我有这个错误:。我将 coffee
添加到我的 package.json
的 jest
配置中,如下所示:
"jest": {
"moduleFileExtensions": ["js", "json", "jsx", "node", "coffee"]
},
但现在我遇到了上面描述的错误。
我该如何处理?
您需要为 Jest 设置一个 preprocessor,因为它无法处理咖啡脚本。
这个问题已经有人回答了before所以我不再赘述。 es6 工作的原因 "out of the box" 是因为 jest-babel 已被提供,因为 es6 实际上在大多数 React 代码库中都是如此。
我最终通过做两件事修复了它:
- 为咖啡文件添加预处理器(如其他问题)
- 将
"preprocessorIgnorePatterns": []
添加到我的 Jest 配置中。 Jest 会忽略 node_modules/
中的 .coffee 文件,如果您不明确告诉它您也想编译这些文件。
我正在编写 Jest 集成测试来测试我的服务器。这是我的测试:
import { app, port } from '../server' // the other lines are not so important
...
当 运行 jest
这引发错误:
__tests__/graphql.test.js
● Test suite failed to run
<project folder>/node_modules/papercut/lib/papercut.coffee:3
{FileStore, S3Store, TestStore } = require('./store')
^
SyntaxError: Unexpected token =
我在 Jest 测试中需要 server.js
,此文件需要 upload.js
,并且此文件需要名为 papercut
的节点模块。问题是,它是用 CoffeeScript 写的,不是纯 JS。
一开始我有这个错误:coffee
添加到我的 package.json
的 jest
配置中,如下所示:
"jest": {
"moduleFileExtensions": ["js", "json", "jsx", "node", "coffee"]
},
但现在我遇到了上面描述的错误。
我该如何处理?
您需要为 Jest 设置一个 preprocessor,因为它无法处理咖啡脚本。
这个问题已经有人回答了before所以我不再赘述。 es6 工作的原因 "out of the box" 是因为 jest-babel 已被提供,因为 es6 实际上在大多数 React 代码库中都是如此。
我最终通过做两件事修复了它:
- 为咖啡文件添加预处理器(如其他问题)
- 将
"preprocessorIgnorePatterns": []
添加到我的 Jest 配置中。 Jest 会忽略node_modules/
中的 .coffee 文件,如果您不明确告诉它您也想编译这些文件。