【发布时间】:2021-04-23 13:14:52
【问题描述】:
我在我的 React 应用程序中使用 jest 和酶运行测试,虽然我的简单完整性检查测试(2+2 期望 4)有效,但当我去浅层渲染组件时会引发此错误。当我尝试将 shallow() 替换为 render() 时,它也会抛出
Projects\MyProject\__tests__\app.test.js:17
const wrapper = (0, _enzyme.shallow)(<_App.App />);
^
SyntaxError: Unexpected token '<'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.523 s
Ran all test suites.
error Command failed with exit code 1.
这里是 app.test.js 文件:
import React from "react";
import { expect } from "chai";
import { shallow } from "enzyme";
import { App } from "../src/App";
import Header from "../src/components/header/header.component";
describe("<App />", () => {
it("renders one Header component", () => {
const wrapper = shallow(<App />);
expect(wrapper.find(Header)).to.have.lengthOf(1);
});
});
babel.config.js:
module.exports = {
presets: [["@babel/preset-react", { targets: { node: "current" } }]],
plugins: ["@babel/plugin-syntax-jsx", "transform-export-extensions"],
only: ["./**/*.jsx", "./**/*.js", "node_modules/jest-runtime"],
};
编辑:我添加了 babel-jest,现在这是错误:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from "react";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.344 s
Ran all test suites.
error Command failed with exit code 1.
组件是.jsx 文件类型。有没有人见过这个错误?我认为我不需要更改配置中的任何内容,这似乎是一个公关
【问题讨论】:
-
可能是因为描述中的符号。尝试删除并执行单元测试 ->
describe("<App />", -
@SarunUK 谢谢,我试过了,但没有任何改变。我确实添加了 babel-jest,现在我在 import 语句中遇到错误。我更新了我的问题。
标签: javascript reactjs jestjs enzyme babel-jest