fix(jest): stricter regex for 'server-only' in default config #77588
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
NextJest default
moduleNameMapper
configuration forserver-only
matches any string that contains this substring, causing mocks of matching dependencies to overwrite each others and the empty mock forimport 'server-only';
. This changes makes the regex for the module name more strict to ensure only the full stringserver-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 tonextJest
's default configuration forserver-only
. Given a test file with: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 forserver-only
) and thusfnOne
would beundefined
.In our project, we've currently fixed the issue by patching the configuration generated by
nextJest
using the stricter regexp in this PR:Verifications
I've checked that
pnpm build
andpnpm test-unit
were unaffected. This is the result ofpnpm test-unit
: