I am running tests with jest and enzyme in my React application, and while my simple sanity check test (2+2 expect 4) works, this error is thrown when I go to shallow render a component. It also throws when I try to replace shallow() with 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.
Here is the app.test.js file:
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"],
};
EDIT : I added babel-jest and now this is the error:
({"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.
The components are .jsx filetypes. Has anyone seen this error before? I don't think I need to change anything in my config, this seems to be a pr