Repository: vercel/next.js
File: packages/next/src/build/jest/jest.ts (TypeScript)

Code before:
          ...(transpiled
            ? [
                `/node_modules/(?!.pnpm)(?!(${transpiled})/)`,
                `/node_modules/.pnpm/(?!(${transpiled.replace(
                  /\//g,
                  '\\+'
                )})@)`,
              ]
            : ['/node_modules/']),
          // CSS modules are mocked so they don't need to be transformed
          '^.+\\.module\\.(css|sass|scss)$',

          // Custom config can append to transformIgnorePatterns but not modify it
          // This is to ensure `node_modules` and .module.css/sass/scss are always excluded
          ...(resolvedJestConfig.transformIgnorePatterns || []),
        ],
        watchPathIgnorePatterns: [
          // Don't re-run tests when the Next.js build output changes
          '/.next/',
          ...(resolvedJestConfig.watchPathIgnorePatterns || []),
        ],
        globals: {
          // Expose Next.js image config to tests so Image components can access it
          __NEXT_IMAGE_OPTS: nextConfig?.images,
          ...(resolvedJestConfig.globals || {}),
        },
      }
    }
  }
}


Review comment (suggestion):
Lets avoid changing globals and instead override `process.env.__NEXT_IMAGE_OPTS` so that jest behaves the same as production builds.

That way none of the other files need to change to read a new global.

Code after:
          // custom config comes last to ensure the above rules are matched,
          // fixes the case where @pages/(.*) -> src/pages/$! doesn't break
          // CSS/image mocks
          ...(resolvedJestConfig.moduleNameMapper || {}),
        },
        testPathIgnorePatterns: [
          // Don't look for tests in node_modules
          '/node_modules/',
          // Don't look for tests in the Next.js build output
          '/.next/',
          // Custom config can append to testPathIgnorePatterns but not modify it
          // This is to ensure `.next` and `node_modules` are always excluded
          ...(resolvedJestConfig.testPathIgnorePatterns || []),
        ],

        transform: {
          // Use SWC to compile tests
          '^.+\\.(js|jsx|ts|tsx|mjs)$': [
            require.resolve('../swc/jest-transformer'),
            jestTransformerConfig,
          ],
          // Allow for appending/overriding the default transforms
          ...(resolvedJestConfig.transform || {}),
        },

        transformIgnorePatterns: [
          // To match Next.js behavior node_modules is not transformed, only `transpiledPackages`
          ...(transpiled
            ? [
                `/node_modules/(?!.pnpm)(?!(${transpiled})/)`,
                // For pnpm: standard packages use "next@version", but file: refs
                // use "file+..+next-repo-...". Don't ignore when the path leads
                // to a transpiled package (e.g. .../node_modules/next/).
                `/node_modules[\\\\/]\\.pnpm[\\\\/](?!(${transpiled.replace(
                  /\//g,
                  '\\+'
                )})@)(?!.*node_modules[\\\\/](${transpiledPathRegex})[\\\\/])`,
              ]
            : ['/node_modules/']),
          // CSS modules are mocked so they don't need to be transformed
          '^.+\\.module\\.(css|sass|scss)$',

          // Custom config can append to transformIgnorePatterns but not modify it
          // This is to ensure `node_modules` and .module.css/sass/scss are always excluded
          ...(resolvedJestConfig.transformIgnorePatterns || []),
        ],
        watchPathIgnorePatterns: [
          // Don't re-run tests when the Next.js build output changes
          '/.next/',
          ...(resolvedJestConfig.watchPathIgnorePatterns || []),
        ],