jestjsbabel-jest

Testing code using one of Jest globals' names as a top-level identifier


I have tests failing with

  ● ESLint config › should ban correct imports for JS files                                                                                             

    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:

    .../eslint.config.js:600
    const jest =
          ^

    SyntaxError: Identifier 'jest' has already been declared

I can see it happens because a module imported by the test declares a const jest. Of course, I could just rename it, but it seems strange to have to change the code under test for this. So I wanted to know if this can be done without renaming and without breaking other tests, just by changing Jest configuration or the particular failing test.

I'm aware of injectGlobals, but it would break other tests; I don't think it can be set just for one test file, or is there a way to do that?


Solution

  • As far as I'm aware you can't set injectGlobals for a specific test file but you could split the suite into different projects, which get their own top-level configuration:

    // jest.config.js (assuming "type": "commonjs")
    
    /** @type {import("jest").Config} */
    module.exports = {
        /* ...shared config goes here */
        projects: [
            {
                displayName: "globals",
                injectGlobals: true,
                /* ...select tests that should get globals */
            },
            {
                displayName: "no globals",
                injectGlobals: false,
                /* ...select tests that shouldn't */
            }
        ],
    };
    

    Then only the test files in the no globals project have to use @jest/globals, and those in the globals project continue to get those names injected as usual.