
Security News
Frontier AI Is Now Critical Infrastructure
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.
@sebspark/expect-eventually
Advanced tools
Adds a chainable `.eventually()` to vitest's `expect` for polling-based assertions in e2e tests.
@sebspark/expect-eventuallyAdds a chainable .eventually() to vitest's expect for polling-based assertions in e2e tests.
This is not a replacement for fake timers in unit tests. It is designed for e2e tests where you trigger a real side effect — publishing a message to PubSub, making an HTTP call, writing to a database — and then need to wait for the result to propagate before asserting.
In these scenarios you don't know exactly how long it will take, but you don't want to add an arbitrary sleep either. .eventually() polls the assertion on a fixed interval and resolves as soon as it passes, failing only if the timeout is exceeded.
yarn add --exact --dev @sebspark/expect-eventually
Two steps are needed: one for the runtime patch, one for the types.
Register it as a vitest setup file so the prototype patch runs before every test:
// vitest.config.ts
export default defineConfig({
test: {
setupFiles: ['./setup/expect-eventually.ts'],
},
})
// setup/expect-eventually.ts
import '@sebspark/expect-eventually'
To make TypeScript aware of the .eventually() augmentation across all test files, either add it to compilerOptions.types in your tsconfig.json:
{
"compilerOptions": {
"types": ["@sebspark/expect-eventually"]
}
}
Or import it in any .d.ts file already included in your project (e.g. vitest.d.ts):
import '@sebspark/expect-eventually'
Useful when you control both ends of an async flow and have a spy on the receiving end.
import '@sebspark/expect-eventually'
import { test, expect, vi } from 'vitest'
test('processes the published message', async () => {
const onMessage = vi.fn()
subscriber.on('message', onMessage)
await pubsub.publish('my-topic', { hello: 'world' })
await expect(onMessage).eventually().toHaveBeenCalledWith(
expect.objectContaining({ hello: 'world' })
)
})
When the value you want to assert is not an object reference but a plain value that changes over time, wrap it in a getter function. .eventually() will call the getter on each attempt.
test('updates the record in the database', async () => {
await api.post('/orders', { item: 'book' })
await expect(() => db.orders.findFirst()).eventually().toMatchObject({
item: 'book',
status: 'pending',
})
})
The full vitest assertion chain is supported, including .not.
test('removes the item from the queue', async () => {
await expect(() => queue.length()).eventually().not.toEqual(0)
})
Pass an optional config object to control the polling behaviour.
await expect(fn).eventually({ timeout: 5000, interval: 100 }).toHaveBeenCalled()
| Option | Default | Description |
|---|---|---|
timeout | 1000 | Maximum time in milliseconds to keep retrying |
interval | 50 | Time in milliseconds between each retry attempt |
If the assertion does not pass within timeout ms, the promise rejects with the last assertion error.
FAQs
Adds a chainable `.eventually()` to vitest's `expect` for polling-based assertions in e2e tests.
The npm package @sebspark/expect-eventually receives a total of 148 weekly downloads. As such, @sebspark/expect-eventually popularity was classified as not popular.
We found that @sebspark/expect-eventually demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Fable shutdown shows how quickly model access can become a business continuity risk for AI-dependent engineering teams.

Security News
AI agents are pulling packages into environments no scanner is watching, creating exposure before security teams can see it.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.