apiComputed.spec.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import {
  2. computed,
  3. getCurrentInstance,
  4. h,
  5. nodeOps,
  6. render,
  7. } from '@vue/runtime-test'
  8. describe('api: computed', () => {
  9. test('should warn if getCurrentInstance is called inside computed getter', () => {
  10. const Comp = {
  11. setup() {
  12. const c = computed(() => {
  13. getCurrentInstance()
  14. return 1
  15. })
  16. return () => c.value
  17. },
  18. }
  19. render(h(Comp), nodeOps.createElement('div'))
  20. expect(
  21. 'getCurrentInstance() called inside a computed getter',
  22. ).toHaveBeenWarned()
  23. })
  24. test('should warn if getCurrentInstance is called inside computed getter (object syntax)', () => {
  25. const Comp = {
  26. setup() {
  27. const c = computed({
  28. get: () => {
  29. getCurrentInstance()
  30. return 1
  31. },
  32. set: () => {},
  33. })
  34. return () => c.value
  35. },
  36. }
  37. render(h(Comp), nodeOps.createElement('div'))
  38. expect(
  39. 'getCurrentInstance() called inside a computed getter',
  40. ).toHaveBeenWarned()
  41. })
  42. })