@@ -1730,6 +1730,53 @@ describe('useMutableSource', () => {
1730
1730
param => param . version ,
1731
1731
) ;
1732
1732
1733
+ let mutatedValueInRender = 0 ;
1734
+ function MutateDuringRead ( ) {
1735
+ const value = useMutableSource (
1736
+ mutableSource ,
1737
+ defaultGetSnapshot ,
1738
+ defaultSubscribe ,
1739
+ ) ;
1740
+ Scheduler . unstable_yieldValue ( 'MutateDuringRead:' + value ) ;
1741
+ // Note that mutating an exeternal value during render is a side effect and is not supported.
1742
+ source . value = mutatedValueInRender ++ ;
1743
+ return null ;
1744
+ }
1745
+
1746
+ expect ( ( ) => {
1747
+ expect ( ( ) => {
1748
+ act ( ( ) => {
1749
+ ReactNoop . render ( < MutateDuringRead /> ) ;
1750
+ } ) ;
1751
+ } ) . toThrow (
1752
+ 'Cannot read from mutable source during the current render without tearing. This may be a bug in React. Please file an issue.' ,
1753
+ ) ;
1754
+ } ) . toWarnDev ( [
1755
+ // Warns twice because of the retry-on-error render pass. Should
1756
+ // consider only warning during the first attempt, not during the
1757
+ // retry. Or maybe vice versa.
1758
+ 'A mutable source was mutated while the MutateDuringRead component was rendering. This is not supported. ' +
1759
+ 'Move any mutations into event handlers or effects.\n' +
1760
+ ' in MutateDuringRead (at **)' ,
1761
+ 'A mutable source was mutated while the MutateDuringRead component was rendering. This is not supported. ' +
1762
+ 'Move any mutations into event handlers or effects.\n' +
1763
+ ' in MutateDuringRead (at **)' ,
1764
+ ] ) ;
1765
+
1766
+ expect ( Scheduler ) . toHaveYielded ( [
1767
+ 'MutateDuringRead:initial' ,
1768
+ 'MutateDuringRead:0' ,
1769
+ ] ) ;
1770
+ } ) ;
1771
+
1772
+ // @gate experimental
1773
+ it ( 'should throw if a mutable source is mutated during render (legacy mode)' , ( ) => {
1774
+ const source = createSource ( 'initial' ) ;
1775
+ const mutableSource = createMutableSource (
1776
+ source ,
1777
+ param => param . version ,
1778
+ ) ;
1779
+
1733
1780
function MutateDuringRead ( ) {
1734
1781
const value = useMutableSource (
1735
1782
mutableSource ,
0 commit comments