Skip to content

test: enable modern jest timers test #938

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/__tests__/fake-timers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import {waitFor, waitForElementToBeRemoved} from '..'
import {render} from './helpers/test-utils'

async function runWaitFor({time = 300} = {}, options) {
async function runWaitFor({time = 300, advanceTimers = true} = {}, options) {
const response = 'data'
const doAsyncThing = () =>
new Promise(r => setTimeout(() => r(response), time))
let result
doAsyncThing().then(r => (result = r))

if (advanceTimers) {
jest.advanceTimersByTime(time)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't waitFor take care of this?

@timdeschryver I think this is fixed by #966 and #962 without needing to change these particular tests. Can we close this PR in favor of #962?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes of course.
Thanks for getting back to this PR, because I forgot about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't waitFor take care of this?

It could've been a bug in waitFor then, the reason why I added this was because the timers weren't advancing on their own. So, I thought that I had to do that manually.

}
await waitFor(() => expect(result).toBe(response), options)
}

afterEach(() => {
jest.useRealTimers()
})

test('real timers', async () => {
// the only difference when not using fake timers is this test will
// have to wait the full length of the timeout
await runWaitFor()
await runWaitFor({advanceTimers: false})
})

test('legacy', async () => {
Expand All @@ -23,7 +29,7 @@ test('legacy', async () => {
})

test('modern', async () => {
jest.useFakeTimers()
jest.useFakeTimers('modern')
await runWaitFor()
})

Expand Down