feat(test-utils): add chaiEnabled flag to setupVitest to make chai optional#1274
Draft
feat(test-utils): add chaiEnabled flag to setupVitest to make chai optional#1274
chaiEnabled flag to setupVitest to make chai optional#1274Conversation
…ports Agent-Logs-Url: https://github.com/mui/mui-public/sessions/e3a43c6b-ef61-4376-a5eb-e4c96ba9329c Co-authored-by: bernardobelchior <12778398+bernardobelchior@users.noreply.github.com>
Bundle size report
Check out the code infra dashboard for more information about this PR. |
Copilot
AI
changed the title
[WIP] Add chaiEnabled flag to setupVitest function
feat(test-utils): add Apr 1, 2026
chaiEnabled flag to setupVitest to make chai optional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
setupVitestunconditionally importedchai,chai-dom,./chaiPlugin, and./chaiTypesat module evaluation time, makingchaia hard dependency even for setups that don't use it.Changes
setupVitest.tschaiEnabled?: booleanoption (default:true) to thesetupVitestparameter typeimport()inside an async IIFE in the one-time init block, gated onchaiEnabled;chai,chai-dom,./chaiPlugin, and./chaiTypesare loaded in parallel viaPromise.alltry/catchwith actionable error messages pointing to the missing package and thechaiEnabled: falseescape hatchIS_REACT_ACT_ENVIRONMENTremain unconditional — only chai-specific setup is gatedUsage
Original prompt
Summary
Add a
chaiEnabledflag to thesetupVitestfunction inpackages/test-utils/src/setupVitest.tsthat allows consumers to opt out of chai entirely. WhenchaiEnabledisfalse, no chai-related packages (chai,chai-dom,./chaiPlugin,./chaiTypes) should be imported — not even as side effects — so thatchaiis not a required dependency for setups that don't use it.Implement this using dynamic imports (
import(...)) with proper error handling so the chai dependency is never loaded when it's disabled.Current file
packages/test-utils/src/setupVitest.ts(blob SHA:0eb6a70b6f30f054b2a0acb9641d5ca90981dded):Required changes
1. Add
chaiEnabledflag to the options parameterAdd
chaiEnabled?: booleanto the parameter type, defaulting totrue:2. Remove all static chai imports
Remove the following static imports from the top of the file (they must not be imported unconditionally):
3. Replace the static chai usage with dynamic imports inside the
isInitializedblockThe existing chai setup code (which only runs once, inside the
if (isInitialized)guard) must become async and use dynamicimport(), gated onchaiEnabled. The function signature itself can remain synchronous — make thesetupVitestfunction returnvoidstill. The one-time initialization section should launch an async IIFE or be handled in a way that doesn't break the existing structure.The dynamic imports should use
try/catchand throw a helpful error if a chai package fails to load (e...This pull request was created from Copilot chat.