Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest): stricter regex for 'server-only' in default config #77588

Merged
merged 1 commit into from
Mar 28, 2025

Conversation

sbougerel
Copy link
Contributor

@sbougerel sbougerel commented Mar 28, 2025

NextJest default moduleNameMapper configuration for server-only matches any string that contains this substring, causing mocks of matching dependencies to overwrite each others and the empty mock for import 'server-only';. This changes makes the regex for the module name more strict to ensure only the full string server-only is a match.

Why?

On my project, we thought it'd be a good idea to store our server-only files under the directory @/app/lib/server-only. Unfortunately, this made testing with Jest difficult due to nextJest's default configuration for server-only. Given a test file with:

jest.mock('@/app/lib/server-only/one, () => ({
  fnOne: jest.fn(),
}));
jest.mock('@/app/lib/server-only/two, () => ({
  fnTwo: jest.fn(),
}));

The mocks for the dependency @/app/lib/server-only/two would overwrite the mock of @/app/lib/server-only/one (that was overwriting the empty mock for server-only) and thus fnOne would be undefined.

In our project, we've currently fixed the issue by patching the configuration generated by nextJest using the stricter regexp in this PR:

// ...At the end of jest.config.ts
module.exports = async () => {
  const jestConfig = await createJestConfig(config)();
  if (
    typeof jestConfig.moduleNameMapper !== 'undefined' &&
    'server-only' in jestConfig.moduleNameMapper
  ) {
    const value = jestConfig.moduleNameMapper['server-only'];
    jestConfig.moduleNameMapper['^server-only$'] = value;
    delete jestConfig.moduleNameMapper['server-only'];
  }
  return jestConfig;
};

Verifications

I've checked that pnpm build and pnpm test-unit were unaffected. This is the result of pnpm test-unit:

Test Suites: 199 passed, 199 total
Tests:       2 skipped, 1312 passed, 1314 total
Snapshots:   188 passed, 188 total
Time:        11.697 s
Ran all test suites matching /test\/unit\/|packages\/next\/|packages\/font/i.

Jest default configuration for 'server-only' matches any string that
contains this path, causing mocks for matching path to overwrite each
others as well as the empty mock of 'server-only'.
@huozhi huozhi added the CI approved Approve running CI for fork label Mar 28, 2025
@huozhi huozhi merged commit c5d4011 into vercel:canary Mar 28, 2025
131 of 132 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI approved Approve running CI for fork type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants