-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] Fix tests failing on subsequent runs in watchmode (#18076)
- Loading branch information
Showing
10 changed files
with
137 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-env mocha */ | ||
import { render as enzymeRender } from 'enzyme'; | ||
import { stub } from 'sinon'; | ||
|
||
/** | ||
* | ||
* @param {object} [options] | ||
* @param {boolean} [options.expectUseLayoutEffectWarning] | ||
*/ | ||
export default function createServerRender(options = {}) { | ||
const { expectUseLayoutEffectWarning } = options; | ||
|
||
beforeEach(() => { | ||
stub(console, 'error').callsFake((message, ...args) => { | ||
const isUseLayoutEffectWarning = /Warning: useLayoutEffect does nothing on the server/.test( | ||
message, | ||
); | ||
|
||
if (!expectUseLayoutEffectWarning || !isUseLayoutEffectWarning) { | ||
// callThrough | ||
// eslint-disable-next-line no-console | ||
console.info(message, ...args); | ||
throw new Error(message, ...args); | ||
} | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
console.error.restore(); | ||
}); | ||
|
||
return function render(node) { | ||
return enzymeRender(node); | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,8 @@ | ||
import React from 'react'; | ||
import enzyme from 'enzyme/build/index'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import consoleError from './consoleError'; | ||
import { useIsSsr } from '@material-ui/core/test-utils/RenderMode'; | ||
import './initMatchers'; | ||
|
||
consoleError(); | ||
|
||
const useActualLayoutEffect = React.useLayoutEffect; | ||
|
||
/* eslint-disable react-hooks/rules-of-hooks */ | ||
/** | ||
* during tests we are in a jsdom environment but will also call ReactDOM.renderToString | ||
* which triggers useLayoutEffect server warnings. On an actual server there's | ||
* no jsdom | ||
*/ | ||
// eslint-disable-next-line camelcase | ||
React.useLayoutEffect = function unstable_useIsomorphicLayoutEffect(fn, deps) { | ||
const isSsr = useIsSsr(); | ||
// RulesOfHooks violation but this the context needs to be invariant anyway | ||
const useEffect = isSsr ? React.useEffect : useActualLayoutEffect; | ||
return useEffect(fn, deps); | ||
}; | ||
/* eslint-enable react-hooks/rules-of-hooks */ | ||
|
||
enzyme.configure({ adapter: new Adapter() }); |