rstudio / shinytest

Automated testing for shiny apps
https://rstudio.github.io/shinytest/
Other
225 stars 55 forks source link

exportTestValues() fails when a test value has a reactive dependency #429

Open djvill opened 2 years ago

djvill commented 2 years ago

I have a test that is failing with an uninformative error, when a value to be exported in exportTestValues() has a reactive dependency that hasn't been triggered. At worst, there should be a more informative error message. Ideally, there'd be a default value (or a condition object) that fills in for the value to be exported.

Here's a minimal reprex. y() is the reactive value that depends on a click event, so the test errors out if the click hasn't been triggered.

app.R

shinyApp(
  ui = basicPage(
    actionButton("inc", "Increment x")
  ), 
  server=function(input, output, session) {
    vals <- reactiveValues(x = 1)
    y <- eventReactive(input$inc, { vals$x + 1 })

    observeEvent(input$inc, {
      vals$x <<- vals$x + 1
    })

    exportTestValues(
      x = vals$x,
      y = y()
    )
  }
)

clickfirst.R (triggers click before trying to export y())

app <- ShinyDriver$new("../../")
app$snapshotInit("clickfirst")
app$setInputs(inc = "click")
app$snapshot()

dontclick.R (fails to trigger click before trying to export y())

app <- ShinyDriver$new("../../")
app$snapshotInit("dontclick")
app$snapshot()

Console output after running testApp()

> testApp("reprex/")
Running clickfirst.R dontclick.R Query failed (500)----------------------
<html>

<head lang = "en">
  <title>An error has occurred</title>
</head>

<body>

<h1>An error has occurred!</h1>
<p></p>

</body>
</html>

----------------------------------------
Error in httr_get(url) : Unable request data from server

Session info

R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    
system code page: 65001

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] shiny_1.7.1     shinytest_1.5.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3     jquerylib_0.1.4  bslib_0.3.1      pillar_1.7.0    
 [5] compiler_4.2.1   later_1.3.0      base64enc_0.1-3  tools_4.2.1     
 [9] testthat_3.1.4   digest_0.6.29    jsonlite_1.8.0   lifecycle_1.0.1 
[13] tibble_3.1.7     debugme_1.1.0    png_0.1-7        pkgconfig_2.0.3 
[17] rlang_1.0.2      DBI_1.1.2        cli_3.3.0        curl_4.3.2      
[21] fastmap_1.1.0    withr_2.5.0      httr_1.4.3       webdriver_1.0.6 
[25] dplyr_1.0.9      sass_0.4.1       generics_0.1.2   vctrs_0.4.1     
[29] tidyselect_1.1.2 showimage_1.0.0  glue_1.6.2       parsedate_1.3.0 
[33] R6_2.5.1         rematch_1.0.1    processx_3.5.3   fansi_1.0.3     
[37] pingr_2.0.1      tidyr_1.2.0      purrr_0.3.4      callr_3.7.0     
[41] magrittr_2.0.3   promises_1.2.0.1 ps_1.7.0         ellipsis_0.3.2  
[45] htmltools_0.5.2  rsconnect_0.8.25 assertthat_0.2.1 mime_0.12       
[49] xtable_1.8-4     httpuv_1.6.5     utf8_1.2.2       crayon_1.5.1    
[53] brio_1.1.3