How to test modules within a golem framework shiny package?

723 views Asked by At

I am trying to develop unit tests for my shiny app, which was made using golem framework and shiny v1.5.0.

Golem comes with a recommended test file, which covers very basic UI testing. I am, however, way more concerned with testing the server-side, for all modules. So just like you would make tests for each function in a regular R package, I want to test each module in my shiny app.

I know that there's a testModule function in development, but it is not included within the latest shiny CRAN version (which is precisely the version I am using). There's only a testServer function, but all the examples I found seem to define the module server side within the test file. I couldn't find an example of module testing when the module is part of a package.

So what I'd like to do is basically a test that lives inside tests/testthat/test-my_module.R and would look like this:


test_that("The module receives its input", {
    shiny::testServer(
        app = mypackage::my_module_server,
        args = list(),
        session = MockShinySession$new(), {
        session$setInputs(some_input = 100)
        expect_equal(output$some_output, 50)
    })
})

However, this throws an error:

Error in UseMethod("as.shiny.appobj", x) : 
  método não aplicável para 'as.shiny.appobj' aplicado a um objeto de classe "function"

which basically says the specified method does not work when applied to a function class object.

Am I missing something here?

Would appreciate some discussion on how you do tests when developing a shiny app as a package.

1

There are 1 answers

1
samuelmacedo83 On BEST ANSWER

I was stuck with this too. I solved this way for my package (sistec).

aria_server <- sistec::aria_server() 
server <- function(id) {
  shiny::moduleServer(id, aria_server)
}

testServer(server, {
  # comparison$x is FALSE
  testthat::expect_false(comparison$x)
})

You could try this in yours:

mypackage_server <- mypackage::my_module_server
server <- function(id) {
  moduleServer(id, mypackage)
}

test_that("The module receives its input", {
    shiny::testServer(server, {
      session$setInputs(some_input = 100)
      expect_equal(output$some_output, 50)
    })
})