tests fail in devtools::check when Shiny app is structured as a package

Hello,

I have a Shiny app structured as an R package with several tests. The tests pass when I run devtools::test() but fail when I run devtools::check() with the error message

"Error ('test-examplemodule.R:2:3'): exampleModuleServer works
Error in eval(code, test_env): object 'exampleModuleServer' not found"

My code and an explanation of what I tried are below. If someone could help and let me know what I am doing wrong that would be much appreciated.

My Code

R/App.R

myApp <- function(...){
  ui <- shiny::fluidPage(
    # exampleModuleUI is defined in R/example-module.R
    shiny::h2("Modules example"),
    exampleModuleUI("examplemodule1", "Click counter #1"),
    exampleModuleUI("examplemodule2", "Click counter #2")
  )
  
  server <- function(input, output, session) {
    # exampleModuleServer is defined in R/example-module.R
    exampleModuleServer("examplemodule1")
    exampleModuleServer("examplemodule2")
  }
  
  shiny::shinyApp(ui, server)
}

R/example-module.R

exampleModuleUI <- function(id, label = "Counter") {
  # All uses of Shiny input/output IDs in the UI must be namespaced,
  # as in ns("x").
  ns <- shiny::NS(id)
  shiny::tagList(
    shiny::actionButton(ns("button"), label = label),
    shiny::verbatimTextOutput(ns("out"))
  )
}

exampleModuleServer <- function(id) {
  # moduleServer() wraps a function to create the server component of a
  # module.
  shiny::moduleServer(
    id,
    function(input, output, session) {
      count <- shiny::reactiveVal(0)
      shiny::observeEvent(input$button, {
        count(count() + 1)
      })
      output$out <- shiny::renderText({
        count()
      })
      count
    }
  )
}

tests/testthat/test-examplemodule.R

testthat::test_that("exampleModuleServer works", {
  shiny::testServer(exampleModuleServer, {
    # Set initial value of a button
    session$setInputs(button = 0)
  
    # Check the value of the reactiveVal `count()`
    expect_equal(count(), 1)
    # Check the value of the renderText()
    expect_equal(output$out, "1")
  
    # Simulate a click
    session$setInputs(button = 1)
  
    expect_equal(count(), 2)
    expect_equal(output$out, "2")
  })
})

tests/testthat/setup-shintytest2.R

# Load application support files into testing environment
shinytest2::load_app_env()

DESCRIPTION

Package: shinyTemplate
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R: 
    person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
           comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Config/testthat/edition: 3
Imports: 
    shiny
Suggests:
    shinytest2,
    testthat

What I've Tried

The app works as expected with

devtools::load_all()
myApp()

The tests pass when I run devtools::test() but I get a warning:

devtools::test()

:information_source: Testing shinyTemplate
:heavy_check_mark: | F W S OK | Context
:heavy_check_mark: | 4 | examplemodule

══ Results ════════════════════════════════════════════════════════════════════════════════════════════════
[ FAIL 0 | WARN 0 | SKIP 0 | PASS 4 ]
Warning message:
In warn_if_app_dir_is_package(appDir) :
Loading R/ subdirectory for Shiny application, but this directory appears to contain an R package. Sourcing files in R/ may cause unexpected behavior. See ?loadSupport for more details.

When I run devtools::check the tests fail

devtools::check()

── R CMD check results ────────────────────────────────────────────────────── shinyTemplate 0.0.0.9000 ────
Duration: 6s

❯ checking tests ...
See below...

── Test failures ──────────────────────────────────────────────────────────────────────────── testthat ────

shinytest2::test_app()
Loading required package: testthat
Loading required package: shiny
:heavy_check_mark: | F W S OK | Context

⠏ | 0 | examplemodule
:heavy_multiplication_x: | 1 0 | examplemodule
────────────────────────────────────────────────────────────────────────────────
Error ('test-examplemodule.R:2:3'): exampleModuleServer works
Error in eval(code, test_env): object 'exampleModuleServer' not found
Backtrace:

  1. └─shiny::testServer(...) at test-examplemodule.R:2:3
  2. └─shiny:::isModuleServer(app)
    ────────────────────────────────────────────────────────────────────────────────

══ Results ═════════════════════════════════════════════════════════════════════
── Failed tests ────────────────────────────────────────────────────────────────
Error ('test-examplemodule.R:2:3'): exampleModuleServer works
Error in eval(code, test_env): object 'exampleModuleServer' not found
Backtrace:

  1. └─shiny::testServer(...) at test-examplemodule.R:2:3
  2. └─shiny:::isModuleServer(app)

[ FAIL 1 | WARN 0 | SKIP 0 | PASS 0 ]
Error: Test failures
Execution halted

1 error :heavy_multiplication_x: | 0 warnings :heavy_check_mark: | 0 notes :heavy_check_mark:

Any help you could give would be much appreciated. Thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.