-
Notifications
You must be signed in to change notification settings - Fork 468
feature/use-original-setTimeout-when-used-in-a-test #305
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
Merged
kentcdodds
merged 14 commits into
testing-library:master
from
LaurensBosscher:feature/use-original-setTimeout-when-used-in-a-test
Jul 11, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d76ca27
feature/use-original-setTimeout-when-used-in-a-test
22f915c
also utilize the original clearTimeout, added tests for getSetTimeout…
cf2fb03
added extra check to getSetTimeout and getClearTimeout to make sure t…
ce83f8f
simplified getSetTimeout and getClearTimeout
0e1c612
Merge branch 'master' into feature/use-original-setTimeout-when-used-…
LaurensBosscher 060eafd
updated tests for the Node environment, removed dependency injection …
9d7f087
updated test name
a13396a
Update src/helpers.js
LaurensBosscher 1b3c31a
Merge branch 'master' into feature/use-original-setTimeout-when-used-…
LaurensBosscher 8eccbb4
Update src/helpers.js
LaurensBosscher 0293c37
updated with feedback from Kent
39357b6
do some fancy magic stuff
f79a2d5
no longer needed
f53c214
fix coverage
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {render} from './helpers/test-utils' | ||
|
||
// Because we're using fake timers here and I don't want these tests to run | ||
// for the actual length of the test (because it's waiting for a timeout error) | ||
// we'll mock the setTimeout, clearTimeout, and setImmediate to be the ones | ||
// that jest will mock for us. | ||
jest.mock('../helpers', () => { | ||
const actualHelpers = jest.requireActual('../helpers') | ||
return { | ||
...actualHelpers, | ||
setTimeout, | ||
clearTimeout, | ||
setImmediate, | ||
} | ||
}) | ||
|
||
jest.useFakeTimers() | ||
|
||
// Because of the way jest mocking works here's the order of things (and no, the order of the code above doesn't make a difference): | ||
// 1. Just mocks '../helpers' and setTimeout/clearTimeout/setImmediate are set to their "correct" values | ||
// 2. We tell Jest to use fake timers | ||
// 3. We reset the modules and we mock '../helpers' again so now setTimeout/clearTimeout/setImmediate are set to their mocked values | ||
// We're only doing this because want to mock those values so this test doesn't take 4501ms to run. | ||
jest.resetModules() | ||
|
||
const { | ||
waitForElement, | ||
waitForDomChange, | ||
waitForElementToBeRemoved, | ||
} = require('../') | ||
|
||
test('waitForElementToBeRemoved: times out after 4500ms by default', () => { | ||
const {container} = render(`<div></div>`) | ||
const promise = expect( | ||
waitForElementToBeRemoved(() => container), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"Timed out in waitForElementToBeRemoved."`, | ||
) | ||
jest.advanceTimersByTime(4501) | ||
return promise | ||
}) | ||
|
||
test('waitForElement: can time out', async () => { | ||
const promise = waitForElement(() => {}) | ||
jest.advanceTimersByTime(4600) | ||
await expect(promise).rejects.toThrow(/timed out/i) | ||
}) | ||
|
||
test('waitForElement: can specify our own timeout time', async () => { | ||
const promise = waitForElement(() => {}, {timeout: 4700}) | ||
const handler = jest.fn() | ||
promise.then(handler, handler) | ||
// advance beyond the default | ||
jest.advanceTimersByTime(4600) | ||
// promise was neither rejected nor resolved | ||
expect(handler).toHaveBeenCalledTimes(0) | ||
|
||
// advance beyond our specified timeout | ||
jest.advanceTimersByTime(150) | ||
|
||
// timed out | ||
await expect(promise).rejects.toThrow(/timed out/i) | ||
}) | ||
|
||
test('waitForDomChange: can time out', async () => { | ||
const promise = waitForDomChange() | ||
jest.advanceTimersByTime(4600) | ||
await expect(promise).rejects.toThrow(/timed out/i) | ||
}) | ||
|
||
test('waitForDomChange: can specify our own timeout time', async () => { | ||
const promise = waitForDomChange({timeout: 4700}) | ||
const handler = jest.fn() | ||
promise.then(handler, handler) | ||
// advance beyond the default | ||
jest.advanceTimersByTime(4600) | ||
// promise was neither rejected nor resolved | ||
expect(handler).toHaveBeenCalledTimes(0) | ||
|
||
// advance beyond our specified timeout | ||
jest.advanceTimersByTime(150) | ||
|
||
// timed out | ||
await expect(promise).rejects.toThrow(/timed out/i) | ||
}) |
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
39 changes: 0 additions & 39 deletions
39
src/__tests__/wait-for-element-to-be-removed.fake-timers.js
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,20 @@ | ||
import MutationObserver from '@sheerun/mutationobserver-shim' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplifying this quite a bit by not using getters and instead just creating variables. Similar to how wait-for-expect does it. |
||
|
||
const globalObj = typeof window === 'undefined' ? global : window | ||
|
||
// we only run our tests in node, and setImmediate is supported in node. | ||
// istanbul ignore next | ||
function setImmediatePolyfill(fn) { | ||
return globalObj.setTimeout(fn, 0) | ||
} | ||
|
||
// istanbul ignore next | ||
const { | ||
setTimeout, | ||
clearTimeout, | ||
setImmediate = setImmediatePolyfill, | ||
} = globalObj | ||
|
||
function newMutationObserver(onMutation) { | ||
const MutationObserverConstructor = | ||
typeof window !== 'undefined' && | ||
|
@@ -18,20 +33,10 @@ function getDocument() { | |
return window.document | ||
} | ||
|
||
/* | ||
* There are browsers for which `setImmediate` is not available. This | ||
* serves as a polyfill of sorts, adopting `setTimeout` as the closest | ||
* equivalent | ||
*/ | ||
function getSetImmediate() { | ||
/* istanbul ignore else */ | ||
if (typeof setImmediate === 'function') { | ||
return setImmediate | ||
} else { | ||
return function setImmediate(fn) { | ||
return setTimeout(fn, 0) | ||
} | ||
} | ||
export { | ||
getDocument, | ||
newMutationObserver, | ||
setImmediate, | ||
setTimeout, | ||
clearTimeout, | ||
} | ||
|
||
export {getDocument, newMutationObserver, getSetImmediate} |
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've moved all the fake timers tests to this file. The only reason we're using fake timers is for testing timeouts and we don't want this test to take forever.