rshinygolem

Run `testServer()` w/ `golem::get_golem_options()`


I have set up a R Shiny app using the golem R-package. In the app_server() I use some globally set golem options as follows:

app_server <- function(input, output, session) {

  myServer(
    id = "test",
    global_variable = golem::get_golem_options()$global_variable,
    other_variable = other_variable
  )

  ...

}

Now the default test testServer() in test-golem-recommended.R throws an error because within myServer() it can't find the golem options or the global_variable is NULL (which also kind of makes sense):

testServer(app_server, {

  # Set and test an input
  session$setInputs(x = 2)
  expect_equal(input$x, 2)

})

However, is it possible to call app_server() so that the golem options are loaded first?


Solution

  • I dealt with such globals options in testing by adding a tests/testthat/setup.R file with the following:

    global_variable <- "my global variable value"
    
    shinyOptions("golem_options" = list("global_variable" = global_variable))
    
    

    It makes it available no matter how your tests are executed.

    See related doc here: https://testthat.r-lib.org/articles/special-files.html#helper-files