ReferenceError: React not defined in tests

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Solving the Mystery: ReferenceError: React not defined in Tests with Laravel Mix and Jest

As a senior developer working within the Laravel ecosystem, integrating front-end tooling like React testing can sometimes introduce subtle but frustrating configuration issues. I’ve encountered this specific error—ReferenceError: React is not defined—when attempting to run tests using Jest alongside Laravel Mix setups. It often feels like a simple missing import, but in reality, it points to a deeper problem in how the testing environment resolves modules and transpiles JSX within the context of your build process.

This post will dissect why this error occurs in a Laravel/Mix/Jest environment and provide a comprehensive, practical solution to ensure your React tests execute flawlessly.


Understanding the Root Cause

The ReferenceError: React is not defined error during testing means that when the Jest runner attempts to execute your test file, the global scope or the module scope it operates in does not recognize the React object. In a standard React environment, this object is injected by the transpilation process (Babel) and the module loader.

When working with Laravel Mix, Webpack, and Babel, the issue usually stems from one of three areas:

  1. Transpilation Failure: Babel is supposed to convert JSX syntax (<div />) into standard JavaScript function calls (React.createElement(...)). If the test runner doesn't correctly pick up these transpiled files or if the Babel configuration misses a necessary step, React remains undefined.
  2. Module Resolution Mismatch: Jest needs to know exactly which files to process and how to resolve dependencies (like react and react-test-renderer). The setup in your jest.config.js might not be correctly pointing to the source files or the transformation pipeline established by Webpack/Mix.
  3. Environment Isolation: Testing environments must be perfectly isolated. If the test environment doesn't inherit the context provided by the main application build, it fails to find core libraries like React.

Step-by-Step Solutions for Laravel Mix/Jest Setup

Given your specific configuration (using laravel-mix and custom Jest settings), here are the most effective steps to resolve this issue:

1. Verify Babel Configuration and Transpilation

Ensure that your Babel setup is correctly handling React syntax across both your source files and your test files. While you have a .babelrc, ensure that all necessary presets are active and correctly applied during the testing phase.

Check your .babelrc:
Your current configuration looks mostly correct, ensuring @babel/preset-react is used:

{
    "env": {
        "test": {
            "presets": ["@babel/preset-env", "@babel/preset-react"],
            // ... other plugins
        }
    }
}

If this doesn't work, ensure that the Babel setup used by Jest is explicitly configured to handle JSX files correctly. This often involves ensuring your Webpack build (managed by Laravel Mix) is correctly feeding the necessary module output into the testing environment.

2. Refine Jest Configuration for Module Loading

Your jest configuration defines where Jest looks for files (roots) and how it transforms them. Since you are dealing with compiled assets from Laravel Mix, ensure that the paths are precisely aligned with your source structure.

Examine your package.json setup:

"jest": {
    "roots": [
        "resources/js/app" // Check if this path correctly encompasses all React files being tested.
    ],
    "transform": {".*": "./node_modules/babel-jest"}
}

If your component files are located in a subdirectory (e.g., resources/js/components), ensure the roots path covers that structure, or consider using a more standard setup where Jest can directly access the module dependencies without relying solely on file system roots if you are testing pure React components.

3. The Dependency Check: Ensure Correct Installation

Sometimes, issues arise from mismatched dependency versions. Always run a clean installation to rule out corrupted node_modules:

rm -rf node_modules
npm install
# Rebuild your assets if necessary
npm run dev

This step ensures that Jest and Babel are using the exact versions of React you expect, preventing conflicts where older dependency paths might be referenced.

Conclusion

The ReferenceError: React is not defined error in a complex setup like Laravel Mix and Jest is rarely about a simple missing import in the test file itself. It is almost always an environmental issue related to how the module system (Babel/Webpack) interacts with the test runner (Jest). By meticulously checking your Babel configuration, refining your Jest root paths, and ensuring a clean dependency installation, you can resolve this conflict.

Remember, maintaining a robust front-end pipeline is key to successful development, whether you are building applications on Laravel or any other framework. For deep dives into modern JavaScript tooling and build processes, exploring resources from laravelcompany.com can provide excellent context on how these systems integrate effectively. Happy testing!