|
1 | | -import { expect, test, vi } from 'vitest' |
| 1 | +import { chai, expect, test, vi } from 'vitest' |
2 | 2 |
|
3 | 3 | test('simple usage', async () => { |
4 | 4 | await expect.poll(() => false).toBe(false) |
@@ -106,3 +106,44 @@ test('toBeDefined', async () => { |
106 | 106 | }), |
107 | 107 | })) |
108 | 108 | }) |
| 109 | + |
| 110 | +test('should set _isLastPollAttempt flag on last call', async () => { |
| 111 | + const fn = vi.fn(function (this: object) { |
| 112 | + return chai.util.flag(this, '_isLastPollAttempt') |
| 113 | + }) |
| 114 | + await expect(async () => { |
| 115 | + await expect.poll(fn, { interval: 100, timeout: 500 }).toBe(false) |
| 116 | + }).rejects.toThrowError() |
| 117 | + fn.mock.results.forEach((result, index) => { |
| 118 | + const isLastCall = index === fn.mock.results.length - 1 |
| 119 | + expect(result.value).toBe(isLastCall ? true : undefined) |
| 120 | + }) |
| 121 | +}) |
| 122 | + |
| 123 | +test('should handle success on last attempt', async () => { |
| 124 | + const fn = vi.fn(function (this: object) { |
| 125 | + if (chai.util.flag(this, '_isLastPollAttempt')) { |
| 126 | + return 1 |
| 127 | + } |
| 128 | + return undefined |
| 129 | + }) |
| 130 | + await expect.poll(fn, { interval: 100, timeout: 500 }).toBe(1) |
| 131 | +}) |
| 132 | + |
| 133 | +test('should handle failure on last attempt', async () => { |
| 134 | + const fn = vi.fn(function (this: object) { |
| 135 | + if (chai.util.flag(this, '_isLastPollAttempt')) { |
| 136 | + return 3 |
| 137 | + } |
| 138 | + return 2 |
| 139 | + }) |
| 140 | + await expect(async () => { |
| 141 | + await expect.poll(fn, { interval: 10, timeout: 100 }).toBe(1) |
| 142 | + }).rejects.toThrowError(expect.objectContaining({ |
| 143 | + message: 'Matcher did not succeed in 100ms', |
| 144 | + cause: expect.objectContaining({ |
| 145 | + // makes sure cause message reflects the last attempt value |
| 146 | + message: 'expected 3 to be 1 // Object.is equality', |
| 147 | + }), |
| 148 | + })) |
| 149 | +}) |
0 commit comments