Skip to content

Commit

Permalink
Merge branch 'main' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanBobi committed Apr 4, 2024
2 parents 37ccada + bd04cf9 commit cb57f99
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 20 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Expand Up @@ -1626,6 +1626,16 @@
"code",
"bug"
]
},
{
"login": "KevinBon",
"name": "Kevin BON",
"avatar_url": "https://avatars.githubusercontent.com/u/1910927?v=4",
"profile": "https://github.com/KevinBon",
"contributions": [
"code",
"bug"
]
}
],
"repoHost": "https://github.com",
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -346,6 +346,7 @@ Thanks goes to these people ([emoji key][emojis]):
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jlp-craigmorten"><img src="https://avatars.githubusercontent.com/u/124147726?v=4?s=100" width="100px;" alt="Craig Morten"/><br /><sub><b>Craig Morten</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=jlp-craigmorten" title="Code">馃捇</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://naor.dev"><img src="https://avatars.githubusercontent.com/u/6171622?v=4?s=100" width="100px;" alt="Naor Peled"/><br /><sub><b>Naor Peled</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=naorpeled" title="Code">馃捇</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://everlong.org/"><img src="https://avatars.githubusercontent.com/u/454175?v=4?s=100" width="100px;" alt="Julien Wajsberg"/><br /><sub><b>Julien Wajsberg</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=julienw" title="Code">馃捇</a> <a href="https://github.com/testing-library/dom-testing-library/issues?q=author%3Ajulienw" title="Bug reports">馃悰</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/KevinBon"><img src="https://avatars.githubusercontent.com/u/1910927?v=4?s=100" width="100px;" alt="Kevin BON"/><br /><sub><b>Kevin BON</b></sub></a><br /><a href="https://github.com/testing-library/dom-testing-library/commits?author=KevinBon" title="Code">馃捇</a> <a href="https://github.com/testing-library/dom-testing-library/issues?q=author%3AKevinBon" title="Bug reports">馃悰</a></td>
</tr>
</tbody>
</table>
Expand Down
121 changes: 105 additions & 16 deletions src/__tests__/wait-for.js
Expand Up @@ -292,12 +292,11 @@ test('the real timers => fake timers error shows the original stack trace when c

test('does not work after it resolves', async () => {
jest.useFakeTimers('modern')
let context = 'initial'
const contextStack = []
configure({
// @testing-library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
unstable_advanceTimersWrapper: callback => {
const originalContext = context
context = 'act'
contextStack.push('act:start')
try {
const result = callback()
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false-positive
Expand All @@ -307,54 +306,144 @@ test('does not work after it resolves', async () => {
then: (resolve, reject) => {
thenable.then(
returnValue => {
context = originalContext
contextStack.push('act:end')
resolve(returnValue)
},
error => {
context = originalContext
contextStack.push('act:end')
reject(error)
},
)
},
}
} else {
context = originalContext
contextStack.push('act:end')
return undefined
}
} catch {
context = originalContext
contextStack.push('act:end')
return undefined
}
},
asyncWrapper: async callback => {
const originalContext = context
context = 'no-act'
contextStack.push('no-act:start')
try {
await callback()
} finally {
context = originalContext
contextStack.push('no-act:end')
}
},
})

let data = null
let timeoutResolved = false
setTimeout(() => {
data = 'resolved'
contextStack.push('timeout')
timeoutResolved = true
}, 100)

contextStack.push('waitFor:start')
await waitFor(
() => {
contextStack.push('callback')
// eslint-disable-next-line jest/no-conditional-in-test -- false-positive
if (data === null) {
if (!timeoutResolved) {
throw new Error('not found')
}
},
{interval: 50},
)

expect(context).toEqual('initial')
contextStack.push('waitFor:end')

expect(contextStack).toMatchInlineSnapshot(`
[
waitFor:start,
no-act:start,
callback,
act:start,
act:end,
callback,
act:start,
timeout,
act:end,
callback,
no-act:end,
waitFor:end,
]
`)

await Promise.resolve()

expect(context).toEqual('initial')
// The context call stack should not change
expect(contextStack).toMatchInlineSnapshot(`
[
waitFor:start,
no-act:start,
callback,
act:start,
act:end,
callback,
act:start,
timeout,
act:end,
callback,
no-act:end,
waitFor:end,
]
`)
})

test(`when fake timer is installed, on waitFor timeout, it doesn't call the callback afterward`, async () => {
jest.useFakeTimers('modern')

configure({
// @testing-library/react usage to ensure `IS_REACT_ACT_ENVIRONMENT` is set when acting.
unstable_advanceTimersWrapper: callback => {
try {
const result = callback()
// eslint-disable-next-line jest/no-if, jest/no-conditional-in-test -- false-positive
if (typeof result?.then === 'function') {
const thenable = result
return {
then: (resolve, reject) => {
thenable.then(
returnValue => {
resolve(returnValue)
},
error => {
reject(error)
},
)
},
}
} else {
return undefined
}
} catch {
return undefined
}
},
asyncWrapper: async callback => {
try {
await callback()
} finally {
/* eslint no-empty: "off" */
}
},
})

const callback = jest.fn(() => {
throw new Error('We want to timeout')
})
const interval = 50

await expect(() =>
waitFor(callback, {interval, timeout: 100}),
).rejects.toThrow()
expect(callback).toHaveBeenCalledWith()

callback.mockClear()

await jest.advanceTimersByTimeAsync(interval)

expect(callback).not.toHaveBeenCalledWith()
})
8 changes: 4 additions & 4 deletions src/wait-for.js
Expand Up @@ -81,15 +81,15 @@ function waitFor(
jest.advanceTimersByTime(interval)
})

// Could have timed-out
if (finished) {
break
}
// It's really important that checkCallback is run *before* we flush
// in-flight promises. To be honest, I'm not sure why, and I can't quite
// think of a way to reproduce the problem in a test, but I spent
// an entire day banging my head against a wall on this.
checkCallback()

if (finished) {
break
}
}
} else {
try {
Expand Down

0 comments on commit cb57f99

Please sign in to comment.