Skip to content

watch([], cb, { immediate: true }) // cb not runing as expected #14898

Description

@wsi18n

Vue version

3.5.35

Link to minimal reproduction

Steps to reproduce

import { expect, it, vi } from 'vitest'
import { watch } from 'vue' // vue@2.7
import { watch as watch3 } from 'vue3' // "vue3": "npm:vue@^3.5.35"

it('watch([], cb, { immediate: true })', async () => {
  const cb = vi.fn()
  watch([], cb, { immediate: true })
  const cb2 = vi.fn()
  watch3([], cb2, { immediate: true })

  expect(cb2).toHaveBeenCalledTimes(1) // but get 0
  expect(cb).toHaveBeenCalledTimes(1) // but get 0
})

A: 为什么要 watch 空数组
A: Why watch an empty array?

Q: 封装公共函数时,可以传入不定个数的参数, 比如:
Q: When encapsulating a public function, you can pass a variable number of arguments, for example:

function foo<T extends WatchSource[]>(...args: T) {
  return function wrapper(...args: T) {
     watch(args, cb, { immediate: true })
     ....
  }
}

一般情况下传参,一切正常;但未传参时,cb不执行,不符合语义和直觉
Under normal circumstances with arguments passed, everything works fine; but when no arguments are passed, the callback doesn't execute, which goes against intuition.

What is expected?

expect(cb2).toHaveBeenCalledTimes(1)
expect(cb).toHaveBeenCalledTimes(1)

What is actually happening?

0

System Info

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions