Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing testServer() tests for modules using ExtendedTask #4117

Open
karawoo opened this issue Aug 13, 2024 · 3 comments
Open

Writing testServer() tests for modules using ExtendedTask #4117

karawoo opened this issue Aug 13, 2024 · 3 comments

Comments

@karawoo
Copy link

karawoo commented Aug 13, 2024

I'm adding ExtendedTasks to a module that has testServer tests for some of the reactives. I'm curious if there's any way to test the outputs of an ExtendedTask in testServer? I realize I may be after contradictory things here and this may not be possible.

The example below doesn't work, which makes sense since expect_equal() is called before the ExtendedTask returns a result. I thought maybe setting the future plan to "sequential" would make it work, but it didn't.

library("shiny")
library("promises")
library("testthat")

future::plan("multisession")

server <- function(input, output, session) {
  sum_values <- ExtendedTask$new(function(x, y) {
    future_promise({
      Sys.sleep(5)
      x + y
    })
  })
  observeEvent(input$button, {
    sum_values$invoke(input$x, input$y)
  })
}

testServer(server, {
  session$setInputs(
    x = 1,
    y = 2,
    button = 1
  )
  expect_equal(sum_values$result(), 3)
})

I also tried putting the following before the expect_equal() just to check on the task's status and it seems to stay in "running" for the whole minute.

i <- 1
while (sum_values$status() %in% c("initial", "running") && i < 60) {
  Sys.sleep(1)
  print(paste(i, sum_values$status()))
  i <- i + 1
}
@stla
Copy link

stla commented Aug 16, 2024

I don't know, but don't you need a bind_task_button?

@karawoo
Copy link
Author

karawoo commented Aug 16, 2024

I don't think so? bind_task_button() disables the button in the UI while the ExtendedTask is processing, which is not necessary for this minimal reprex since there is no UI.

@stla
Copy link

stla commented Aug 16, 2024

Sorry for not being helpful. The only thing I know is that there's a way to test ExtendedTask with the shinytest2 package. But as I understand, this requires the bslib button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants