How to unit test Shiny widgets?

Let's say I am building a Shiny widget with an input object on the server side. An example usage of the API:

ui <- fluidPage(
  myWidget("some_id")
)

server <- function(input, output, session) {
  observe(print(input$some_id))
}

If I am putting this widget into a package, how should I test it? I could build an example app like this and use shinytest, but that seems a bit heavyweight to use for CI / testing on CRAN / during development / etc. Again, the focus is not an app but a widget. Do I need a headless browser? Some particular type of mocking?

Do I scaffold a fake Shiny session around the functions for testing with testthat? I have been exploring some examples from the Shiny repo, but am having trouble wrapping my head around the best way to approach.

2 Likes

If the intention is to test both the R and JavaScript portions of the widget, then I don't think you have a choice but to use something that uses a headless browser (like shinytest). Personally yes I'd write a little app and hook it up to shinytest for CI; but don't do observe(print(input$some_id)), instead have a verbatimTextOutput/renderPrint so shinytest can verify the output.

2 Likes