2

As I was setting up my tests for the code, I got stuck that I can't fully understand.

My vitest log asks me to put my button click inside an act(), but it breaks by doing so. I don't fully understand why nor what is the possible solution.

function:

const handleSendJobsAssigned = async (event: React.MouseEvent) => {
setAssingingJobsLoading(true)
let counter = 0
let failed = false

try {
  for (const job of assignedJobs) {
    await api.put(url, {
      status: "test",
      assignee: 1
    })
    removeAssignedJob(1)
    counter += 1
  }
} catch (e) {
  failed = true
} finally {
  showToast(counter, failed)
  setAssingingJobsLoading(false)
  clearJobs()
  refreshSelectedTab()
}
}

not working 1:

await act( async () => {
  fireEvent.click(assignButton);

  //on click function
  await vi.waitFor(() => {
    expect(assignButton).toBeDisabled();
    expect(cancelButton).toBeDisabled();
  })

  //after click function
  await vi.waitFor(() => {
    expect(assignButton).toBeEnabled();
    expect(cancelButton).toBeEnabled();
  });
})

not working 2:

await act( async () => {
  fireEvent.click(assignButton);
})

//on click function
await vi.waitFor(() => {
  expect(assignButton).toBeDisabled();
  expect(cancelButton).toBeDisabled();
})

//after click function
await vi.waitFor(() => {
  expect(assignButton).toBeEnabled();
  expect(cancelButton).toBeEnabled();
});

working but with log error on vitest log:

fireEvent.click(assignButton);

//on click function
await vi.waitFor(() => {
  expect(assignButton).toBeDisabled();
  expect(cancelButton).toBeDisabled();
})

//after click function
await vi.waitFor(() => {
  expect(assignButton).toBeEnabled();
  expect(cancelButton).toBeEnabled();
});

Is there a workaround?

1
  • Have you tried adding css: true to your vitest config file under test? I've got the exact same problem. I don't even get the default value set on my useState variables. Commented May 8 at 5:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.