Receiving error 'SyntaxError: Unexpected identifier' for import statement using Jest with React
Receiving error 'SyntaxError: Unexpected identifier' for import statement using Jest with React
以前,Jest 可以很好地处理 import 语句;但是,在安装依赖项 'react-router-transition' 后,我无法执行任何测试并收到以下错误:
`
失败 src/pages/Costs/Costs.test.js
● 测试套件未能 运行
/Users/***/Code/cmp/cmp-client-portal/node_modules/react-router-transition/lib/index.js:5
import React, { cloneElement, createElement, useEffect, useRef, useState } from 'react';
^^^^^
SyntaxError: Unexpected identifier
> 1 | import { spring } from "react-router-transition"
| ^
2 | // we need to map the `scale` prop we define below
3 | // to the transform style property
4 | function mapStyles(styles) {
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (src/utils/transitions/fadeInOutBounce.js:1:1)
测试套件:1 个失败,总共 1 个
测试:总计 0
快照:共 0 个
时间:2.83s
运行 所有匹配 /src/pages/Costs/i 的测试套件。`
解决方案是将 React 脚本更新到最新版本。
旧版本的 react-scripts 显然有一个错误配置的 babel 加载程序,因此在测试期间无法正确转换代码。
以前,Jest 可以很好地处理 import 语句;但是,在安装依赖项 'react-router-transition' 后,我无法执行任何测试并收到以下错误:
` 失败 src/pages/Costs/Costs.test.js ● 测试套件未能 运行
/Users/***/Code/cmp/cmp-client-portal/node_modules/react-router-transition/lib/index.js:5
import React, { cloneElement, createElement, useEffect, useRef, useState } from 'react';
^^^^^
SyntaxError: Unexpected identifier
> 1 | import { spring } from "react-router-transition"
| ^
2 | // we need to map the `scale` prop we define below
3 | // to the transform style property
4 | function mapStyles(styles) {
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (src/utils/transitions/fadeInOutBounce.js:1:1)
测试套件:1 个失败,总共 1 个 测试:总计 0 快照:共 0 个 时间:2.83s 运行 所有匹配 /src/pages/Costs/i 的测试套件。`
解决方案是将 React 脚本更新到最新版本。
旧版本的 react-scripts 显然有一个错误配置的 babel 加载程序,因此在测试期间无法正确转换代码。