Bird
Raised Fist0
Vueframework~20 mins

Testing async behavior in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Async Vue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What will this Vue 3 component display after mounting?

Consider this Vue 3 component using the Composition API. It fetches data asynchronously on mount.

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const message = ref('Loading...');

    onMounted(async () => {
      await new Promise(resolve => setTimeout(resolve, 100));
      message.value = 'Data loaded';
    });

    return { message };
  }
}

What text will be shown inside the component immediately after it mounts?

AError: message is not defined
BData loaded
Cundefined
DLoading...
Attempts:
2 left
💡 Hint

Think about when the async code finishes compared to the initial render.

❓ state_output
intermediate
2:00remaining
What is the value of count after this Vue 3 async update?

Given this Vue 3 component snippet:

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const count = ref(0);

    onMounted(() => {
      setTimeout(() => {
        count.value += 1;
      }, 50);
      count.value += 1;
    });

    return { count };
  }
}

What is the value of count.value after 100ms?

A2
B1
C0
DNaN
Attempts:
2 left
💡 Hint

Count how many times count.value is incremented and when.

📝 Syntax
advanced
2:30remaining
Which option correctly handles async data fetching in Vue 3 setup?

Which of these Vue 3 setup functions correctly fetches data asynchronously and updates a reactive variable?

A
setup() {
  const data = ref(null);
  fetchData().then(response => {
    data.value = response;
  });
  return { data };
}
B
setup() {
  const data = ref(null);
  onMounted(async () => {
    data.value = await fetchData();
  });
  return { data };
}
C
setup: async () => {
  const data = ref(null);
  data.value = await fetchData();
  return { data };
}
D
setup() {
  const data = null;
  data = await fetchData();
  return { data };
}
Attempts:
2 left
💡 Hint

Remember that setup cannot be async and reactive variables need to be refs.

🔧 Debug
advanced
2:30remaining
Why does this Vue 3 async update not trigger re-render?

Look at this Vue 3 setup code:

import { reactive, onMounted } from 'vue';

export default {
  setup() {
    const state = reactive({ count: 0 });

    onMounted(() => {
      setTimeout(() => {
        state.count = state.count + 1;
      }, 100);
    });

    return { state };
  }
}

After 100ms, the UI does not update to show the new count. What is the most likely reason?

AThe UI is not bound to state.count in the template, so no update happens.
BThe component did not return the reactive object properly from setup.
CsetTimeout does not work inside onMounted lifecycle hook.
DThe reactive object was mutated incorrectly; should use ref instead of reactive.
Attempts:
2 left
💡 Hint

Check if the template uses the reactive property to display the count.

🧠 Conceptual
expert
3:00remaining
What error occurs if you make Vue 3 setup() async and why?

What happens if you declare the setup function as async in Vue 3, like this?

export default {
  async setup() {
    const data = await fetchData();
    return { data };
  }
}

Choose the correct outcome and explanation.

AVue ignores the async keyword and treats setup as synchronous, causing data to be undefined.
BThe component works fine; Vue waits for the Promise to resolve before rendering.
CVue throws a runtime error because setup must return a plain object, not a Promise.
DVue logs a warning but still renders the component with empty data.
Attempts:
2 left
💡 Hint

Think about what Vue expects from the setup function return value.

Practice

(1/5)
1. In Vue testing, why do we use await nextTick() after triggering an async update?
easy
A. To wait for Vue to finish updating the DOM after state changes
B. To pause the test indefinitely
C. To reset the component to its initial state
D. To skip the async operation and test synchronously

Solution

  1. Step 1: Understand Vue's DOM update cycle

    Vue batches DOM updates and applies them asynchronously after state changes.
  2. Step 2: Purpose of await nextTick()

    Using await nextTick() waits for Vue to complete these DOM updates before assertions.
  3. Final Answer:

    To wait for Vue to finish updating the DOM after state changes -> Option A
  4. Quick Check:

    nextTick waits for DOM update [OK]
Hint: Remember nextTick waits for DOM updates after async changes [OK]
Common Mistakes:
  • Thinking nextTick pauses test indefinitely
  • Assuming nextTick resets component state
  • Believing nextTick skips async operations
2. Which of the following is the correct way to write an async test function in Vue using Jest?
easy
A. test('async test', () => { someAsyncFunction().then(); });
B. test('async test', () => { await someAsyncFunction(); });
C. test('async test', async () => { await someAsyncFunction(); });
D. test('async test', () => { someAsyncFunction(); });

Solution

  1. Step 1: Identify async test syntax

    Tests using async/await must declare the test function as async to use await inside.
  2. Step 2: Check each option

    test('async test', async () => { await someAsyncFunction(); }); correctly declares the test function async and awaits the async function.
  3. Final Answer:

    test('async test', async () => { await someAsyncFunction(); }); -> Option C
  4. Quick Check:

    Async test functions need async keyword [OK]
Hint: Always add async before test callback to use await [OK]
Common Mistakes:
  • Omitting async keyword before test function
  • Using await outside async function
  • Not waiting for promises to resolve
3. Given this Vue test snippet, what will be the value of wrapper.text() after the test runs?
const wrapper = mount(MyComponent);
await wrapper.find('button').trigger('click');
await nextTick();

Assuming the button click triggers an async state update that changes displayed text from 'Off' to 'On'.
medium
A. 'Off'
B. 'On'
C. '' (empty string)
D. Throws an error

Solution

  1. Step 1: Understand the click triggers async state change

    The button click changes the component's state asynchronously from 'Off' to 'On'.
  2. Step 2: Use await nextTick() to wait for DOM update

    After triggering click, awaiting nextTick ensures DOM reflects the new state.
  3. Final Answer:

    'On' -> Option B
  4. Quick Check:

    Async update + nextTick = updated DOM text [OK]
Hint: Wait nextTick after async event to see updated DOM [OK]
Common Mistakes:
  • Checking DOM before nextTick causes stale text
  • Assuming immediate synchronous update
  • Forgetting to await async events
4. What is wrong with this Vue test code snippet?
test('updates message after async call', async () => {
  const wrapper = mount(MyComponent);
  wrapper.vm.fetchData();
  expect(wrapper.text()).toContain('Loaded');
});
medium
A. Missing await before fetchData call
B. Missing await nextTick() after fetchData
C. Using expect before async update completes
D. All of the above

Solution

  1. Step 1: Identify missing awaits

    The test calls async fetchData without awaiting it, so the update may not finish.
  2. Step 2: Recognize missing nextTick and premature expect

    Without awaiting nextTick, DOM updates may not be applied before expect runs.
  3. Final Answer:

    All of the above -> Option D
  4. Quick Check:

    Await async calls and nextTick before assertions [OK]
Hint: Always await async calls and nextTick before assertions [OK]
Common Mistakes:
  • Not awaiting async methods
  • Not waiting for DOM updates
  • Running assertions too early
5. You have a Vue component that fetches user data asynchronously on mount and displays the user's name. Which test approach correctly verifies the name is shown after the async fetch?
test('displays user name after fetch', async () => {
  const wrapper = mount(UserComponent);
  // What should you do here?
});
hard
A. await flushPromises(); await nextTick(); expect(wrapper.text()).toContain('Alice');
B. expect(wrapper.text()).toContain('Alice');
C. await wrapper.vm.fetchUser(); expect(wrapper.text()).toContain('Alice');
D. wrapper.vm.fetchUser(); expect(wrapper.text()).toContain('Alice');

Solution

  1. Step 1: Wait for all promises to resolve

    Using flushPromises() waits for all pending promises like fetch to finish.
  2. Step 2: Wait for Vue DOM updates

    After promises resolve, await nextTick() ensures DOM updates are applied.
  3. Final Answer:

    await flushPromises(); await nextTick(); expect(wrapper.text()).toContain('Alice'); -> Option A
  4. Quick Check:

    Flush promises + nextTick = reliable async test [OK]
Hint: Flush promises then nextTick before checking DOM [OK]
Common Mistakes:
  • Checking DOM before promises resolve
  • Not waiting for nextTick after async updates
  • Calling async methods without awaiting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard