Test suite failed to run: Jest Encountered an unexpected token

23 views Asked by At

I am trying to write tests for my create-react app. However, the test suite continually fails to run. This is the full error message:

Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

SyntaxError: /Users/me/Desktop/coding-projects-SB/capstone-2/bird-call-quiz/src/App.test.js: Support for the experimental syntax 'jsx' isn't currently enabled (6:11):

  4 |
  5 | it('renders without crashing', () => {
> 6 |   render (<App />);
    |           ^
  7 | });
  8 |

Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-jsx) to the 'plugins' section to enable parsing.

If you already added the plugin for this syntax to your config, it's possible that your config isn't being loaded.
You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded configuration:
    npx cross-env BABEL_SHOW_CONFIG_FOR=/Users/me/Desktop/coding-projects-SB/capstone-2/bird-call-quiz/src/App.test.js <your build command>
See https://babeljs.io/docs/configuration#print-effective-configs for more info.

  at constructor (../../../../.nvm/versions/node/v16.20.2/lib/node_modules/jest/node_modules/@babel/parser/src/parse-error.ts:74:19)
      at parser.next (<anonymous>)
      at normalizeFile.next (<anonymous>)
      at run.next (<anonymous>)
      at transform.next (<anonymous>)

I have deleted node_modules and package-lock.json and reinstall and I have added a jest.config.js file and babel.config.js file, in addition to the package.json. I am stuck on what to try next.

package.json
{
  "name": "bird-call-quiz",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/preset-env": "^7.24.0",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "babel-jest": "^29.7.0",
    "jest": "^29.7.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.22.3",
    "react-scripts": "5.0.1",
    "ts-jest": "^29.1.2",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "jest",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "babel-core": "^7.0.0-bridge.0"
  }
}


babel.config.js
module.exports = {
    presets: [
      '@babel/preset-env',
      '@babel/preset-react',
      '@babel/preset-flow',
    ],
    plugins: [
      'babel-plugin-styled-components',
      '@babel/plugin-proposal-class-properties',
    ]
  }

jest.config.js
module.exports = {
    preset: 'ts-jest',
    transform: {
      '^.+\\.(ts|tsx)?$': 'ts-jest',
      '^.+\\.(js|jsx)$': 'babel-jest',
    }
  };

App.test.js
import { render } from '@testing-library/react';
import React from "react";
import App from './App';

it('renders without crashing', () => {
  render (<App />);
});
0

There are 0 answers