3

I'm experiencing an issue when running tests with Vitest in a React project. The test fails with an "Expression expected" error, and adding console logs in the test file does not display any output, which indicates that the test might not be running at all.

here is the vite configuration for vitest and the setupTest file

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react()],
    test: {
    globals:true,
        environment: "jsdom",
        setupFiles: ["./testSetup.js"],
    },
    server: {
        proxy: {
            "/api": {
                target: "http://localhost:3003",
                changeOrigin: true,
            },
        },
    },
});
import "@testing-library/jest-dom";
import { cleanup } from "@testing-library/react";
import { afterEach } from "vitest";

// runs a clean after each test case (e.g. clearing jsdom)
afterEach(() => {
    cleanup();
});

the error shown in terminal is this :

Error: Expression expected
 ❯ getRollupError node_modules/vitest/node_modules/rollup/dist/es/shared/parseAst.js:392:41
 ❯ convertProgram node_modules/vitest/node_modules/rollup/dist/es/shared/parseAst.js:1056:26
 ❯ parseAstAsync node_modules/vitest/node_modules/rollup/dist/es/shared/parseAst.js:1898:93
 ❯ ssrTransformScript node_modules/vitest/node_modules/vite/dist/node/chunks/dep-D8YhmIY-.js:52863:11

I tried to remove globals but see this error :

 ❯ node_modules/@testing-library/jest-dom/dist/index.mjs:10:1
      8| import 'css.escape';
      9| 
     10| expect.extend(extensions);
       | ^
     11| 
 ❯ testSetup.js:3:31

also i try to replace import "@testing-library/jest-dom"; by import "@testing-library/jest-dom/vitest" but the same problem

1
  • Your problem exactly matches mine. I'm struggling to find a solution. The only other similar question suggestions deleting package-lock.json, and node_modules. Unfortunately, that did nothing for me. Commented Jul 23, 2024 at 14:32

1 Answer 1

8

Okay, I had the same issue (Error: Expression expected) and this is what fixed it. The file you are testing, and the test file, must have the same extensions.

In my case, I had a component file named CompleteChip.jsx and a test file named CompleteChip.test.js. What fixed the error was changing the test file extension to CompleteChip.test.jsx

Sign up to request clarification or add additional context in comments.

1 Comment

This is a good hint, but I am sure it is 100% accurante. For my case, I had to ensure any files that were using JSX/TSX had the correct file extension: .jsx/.tsx instead of just .js/.ts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.