Skip to content

Negation isNot is not always properly considered in waitUntil #1982

@dprevost-LMI

Description

@dprevost-LMI

When using expect().not for matcher negation, the isNot may not be processed correctly.

  • When using wait : 0
  • When the condition function result is false on the first call, but could be true later on.
onst waitUntil = async (
    condition: () => Promise<boolean>,
    isNot = false,
    { wait = DEFAULT_OPTIONS.wait, interval = DEFAULT_OPTIONS.interval } = {}
): Promise<boolean> => {
    // single attempt
    if (wait === 0) {
        return await condition() // Missing isNot check here
    }

    let error: Error | undefined

    // wait for condition to be truthy
    try {
        const start = Date.now()
        while (true) {
            if (Date.now() - start > wait) {
                throw new Error('timeout')
            }

            try {
                // should not use isNot, should finish the condition until it is true and use it outside the while loop
                const result = isNot !== (await condition()) after
                error = undefined
                if (result) {
                    break
                }
                await sleep(interval)
            } catch (err) {
                error = err
                await sleep(interval)
            }
        }

        if (error) {
            throw error
        }

        return !isNot
    } catch {
        if (error) {
            throw error
        }

        return isNot
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions