|
| 1 | +import * as chai from 'chai' |
| 2 | +import type { ExpectStatic } from '@vitest/expect' |
| 3 | +import { getSafeTimers } from '@vitest/utils' |
| 4 | + |
| 5 | +// these matchers are not supported because they don't make sense with poll |
| 6 | +const unsupported = [ |
| 7 | + // .poll is meant to retry matchers until they succeed, and |
| 8 | + // snapshots will always succeed as long as the poll method doesn't thow an error |
| 9 | + // in this case using the `vi.waitFor` method is more appropriate |
| 10 | + 'matchSnapshot', |
| 11 | + 'toMatchSnapshot', |
| 12 | + 'toMatchInlineSnapshot', |
| 13 | + 'toThrowErrorMatchingSnapshot', |
| 14 | + 'toThrowErrorMatchingInlineSnapshot', |
| 15 | + // toThrow will never succeed because we call the poll callback until it doesn't throw |
| 16 | + 'throws', |
| 17 | + 'Throw', |
| 18 | + 'throw', |
| 19 | + 'toThrow', |
| 20 | + 'toThrowError', |
| 21 | + // these are not supported because you can call them without `.poll`, |
| 22 | + // we throw an error inside the rejects/resolves methods to prevent this |
| 23 | + // rejects, |
| 24 | + // resolves |
| 25 | +] |
| 26 | + |
| 27 | +export function createExpectPoll(expect: ExpectStatic): ExpectStatic['poll'] { |
| 28 | + return function poll(fn, options = {}) { |
| 29 | + const { interval = 50, timeout = 1000, message } = options |
| 30 | + // @ts-expect-error private poll access |
| 31 | + const assertion = expect(null, message).withContext({ poll: true }) as Assertion |
| 32 | + const proxy: any = new Proxy(assertion, { |
| 33 | + get(target, key, receiver) { |
| 34 | + const result = Reflect.get(target, key, receiver) |
| 35 | + |
| 36 | + if (typeof result !== 'function') |
| 37 | + return result instanceof chai.Assertion ? proxy : result |
| 38 | + |
| 39 | + if (key === 'assert') |
| 40 | + return result |
| 41 | + |
| 42 | + if (typeof key === 'string' && unsupported.includes(key)) |
| 43 | + throw new SyntaxError(`expect.poll() is not supported in combination with .${key}(). Use vi.waitFor() if your assertion condition is unstable.`) |
| 44 | + |
| 45 | + return function (this: any, ...args: any[]) { |
| 46 | + const STACK_TRACE_ERROR = new Error('STACK_TRACE_ERROR') |
| 47 | + return new Promise((resolve, reject) => { |
| 48 | + let intervalId: any |
| 49 | + let lastError: any |
| 50 | + const { setTimeout, clearTimeout } = getSafeTimers() |
| 51 | + const timeoutId = setTimeout(() => { |
| 52 | + clearTimeout(intervalId) |
| 53 | + reject(copyStackTrace(new Error(`Matcher did not succeed in ${timeout}ms`, { cause: lastError }), STACK_TRACE_ERROR)) |
| 54 | + }, timeout) |
| 55 | + const check = async () => { |
| 56 | + try { |
| 57 | + chai.util.flag(this, 'object', await fn()) |
| 58 | + resolve(await result.call(this, ...args)) |
| 59 | + clearTimeout(intervalId) |
| 60 | + clearTimeout(timeoutId) |
| 61 | + } |
| 62 | + catch (err) { |
| 63 | + lastError = err |
| 64 | + intervalId = setTimeout(check, interval) |
| 65 | + } |
| 66 | + } |
| 67 | + check() |
| 68 | + }) |
| 69 | + } |
| 70 | + }, |
| 71 | + }) |
| 72 | + return proxy |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function copyStackTrace(target: Error, source: Error) { |
| 77 | + if (source.stack !== undefined) |
| 78 | + target.stack = source.stack.replace(source.message, target.message) |
| 79 | + return target |
| 80 | +} |
0 commit comments